masayuki14 / worklog

Record working log by issues.
MIT License
0 stars 0 forks source link

create table automation #6

Closed masayuki14 closed 6 years ago

masayuki14 commented 6 years ago

create table を自動化したい。

masayuki14 commented 6 years ago
show create table [table_name]

はpostgresでどうやるんだろう。

masayuki14 commented 6 years ago

https://qiita.com/tamano/items/be43de7bb733ad38362c

masayuki14 commented 6 years ago

テーブル情報の詳細

\d [table_name]
masayuki14 commented 6 years ago

詳細な情報

$ pg_dump interest_tier3_dev -Upostgres -s -t tripadvisor_businesses
masayuki14 commented 6 years ago
create table sample(
  id bigint,
  name text
);

テーブル作ってみる。

masayuki14 commented 6 years ago

https://www.postgresql.jp/document/9.6/html/ddl-constraints.html#ddl-constraints-primary-keys 主キーはカラム定義の時にやればいい。

masayuki14 commented 6 years ago
create table sample2 (
  id bigint PRIMARY KEY,
  name text
);
masayuki14 commented 6 years ago

Insertはいつもの感じ set column = value はできないぽい

# insert into sample values(1, 'name');
# insert into sample (id) values (3);

# select * from sample;
 id | name
----+------
  1 | name
  3 |
(2 rows)
masayuki14 commented 6 years ago

primary key 制約

# insert into sample2 (name) values ('summer');
ERROR:  null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, summer).
masayuki14 commented 6 years ago
# insert into sample2  values (4, 'summer wars');
INSERT 0 1

# insert into sample2  values (4, 'summer wars 2');
ERROR:  duplicate key value violates unique constraint "sample2_pkey"
DETAIL:  Key (id)=(4) already exists.
masayuki14 commented 6 years ago

既存のテーブルに PRIMARY KEY を設定するには

masayuki14 commented 6 years ago

制約の追加 https://www.postgresql.jp/document/9.6/html/ddl-alter.html#ddl-alter-adding-a-constraint

alter table add [key] でいけるぽいな

# \d sample;
    Table "public.sample"
 Column |  Type  | Modifiers
--------+--------+-----------
 id     | bigint |
 name   | text   |

# alter table sample add primary key (id);
ALTER TABLE

# \d sample;
    Table "public.sample"
 Column |  Type  | Modifiers
--------+--------+-----------
 id     | bigint | not null
 name   | text   |
Indexes:
    "sample_pkey" PRIMARY KEY, btree (id)
masayuki14 commented 6 years ago
# alter table data_table add primary key (place_id_d);
ERROR:  could not create unique index "data_table_pkey"
DETAIL:  Key (place_id=(1867744) is duplicated.

重複あってエラー

# truncate table data_table

truncate も同じ。消した後なちゃんとPrimaryKey追加できた。

masayuki14 commented 6 years ago

PK 設定したので mode: mergeembluk うごかす。

masayuki14 commented 6 years ago

embluk run config.yml 2回動かしてもエラーでない 😄

masayuki14 commented 6 years ago

データ型 date time timestamp 3つ

masayuki14 commented 6 years ago
interest_tier3_dev=# select * From sample;
 id | name | created_at | created_date |  created_time   |     created_timestamp
----+------+------------+--------------+-----------------+----------------------------
  1 | name |            |              |                 |
  3 |      |            | 2018-02-23   | 05:26:04.908167 | 2018-02-23 05:26:10.739471
(2 rows)

interest_tier3_dev=# \d sample
                    Table "public.sample"
      Column       |            Type             | Modifiers
-------------------+-----------------------------+-----------
 id                | bigint                      | not null
 name              | text                        |
 created_at        | time without time zone      |
 created_date      | date                        |
 created_time      | time without time zone      |
 created_timestamp | timestamp without time zone |
Indexes:
    "sample_pkey" PRIMARY KEY, btree (id)
masayuki14 commented 6 years ago

日付は timestamp 型でいこう。

masayuki14 commented 6 years ago

トリガーを試してみたい。こればっかりは GUI ツール使ったほうが良さそうだ。

masayuki14 commented 6 years ago

ツール。

masayuki14 commented 6 years ago

pgadmin で Trigger Function を登録してみた。 GUIがないとどうにも。

masayuki14 commented 6 years ago

function名 : trip_insert_trigger

masayuki14 commented 6 years ago

SQL で確認する。

# select prosrc  from pg_proc where proname = 'trip_insert_trigger' limit 1 ;
                 prosrc
----------------------------------------
                                       +
 -- RETURNS trigger                    +
                                       +
 BEGIN                                 +
   NEW.created_at := CURRENT_TIMESTAMP;+
   NEW.updated_at := CURRENT_TIMESTAMP;+
   RETURN NEW;                         +
 END;                                  +

(1 row)

登録されている

masayuki14 commented 6 years ago

https://docs.docker.com/engine/reference/builder/#entrypoint

docker-compose.mlworking_dir entrypoint を指定できるぜ!

masayuki14 commented 6 years ago

docker-compose.yml のポスグレのところに entrypoint: で create table のシェル実行するようにしたけど、ポスグレの起動でエラーが出てる。

masayuki14 commented 6 years ago

https://qiita.com/hirosemi/items/199a3c753130c12d52f1

ポスグレの公式Imageでは /docker-entrypoint.sh が実行されるらしい。 だから entrypoint を上書きしちゃうといけない。

masayuki14 commented 6 years ago

中を除く

        for f in /docker-entrypoint-initdb.d/*; do
            case "$f" in
                *.sh)     echo "$0: running $f"; . "$f" ;;
                *.sql)    echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
                *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
                *)        echo "$0: ignoring $f" ;;
            esac
            echo
        done
masayuki14 commented 6 years ago

シェルにしなくても、SQLを /docker-entrypoint-initdb.d に置けば起動時に実行してくれる。

masayuki14 commented 6 years ago

docker-entrypoint-initdb.d はDBのユーザーとかの設定と同じで初めて実行した時しか動かないので注意。