「Tep3-onk-2」を編集中

移動先: 案内検索

警告: ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。ログインまたはアカウントを作成すれば、あなたの編集はその利用者名とともに表示されるほか、その他の利点もあります。

この編集を取り消せます。 下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 編集中の文章
1行目: 1行目:
 
[[category:memo|{{PAGENAME}}]]
 
[[category:memo|{{PAGENAME}}]]
 
{{Font color}}
 
{{Font color}}
== tep5 準備 ==
+
== 準備 ==
 
プロジェクトディレクトリ作成 移動 [[rails前バージョン確認|バージョン確認]]
 
プロジェクトディレクトリ作成 移動 [[rails前バージョン確認|バージョン確認]]
  
340行目: 340行目:
  
 
==== 2022-12 -14 othersテーブル ====
 
==== 2022-12 -14 othersテーブル ====
09-15以降の増分を取る nusiがfalseのもの
+
09-15以降の増分を取る
 
  % psql -d update_dump -U jq -c "select id, user_id, jiin_id, ie_id, name, yomi, nusi, each_order, memo, generation, created_at, updated_at from clients_old where created_at  > '2022-09-15' and nusi = false" -A -F, > ../others_add.csv
 
  % psql -d update_dump -U jq -c "select id, user_id, jiin_id, ie_id, name, yomi, nusi, each_order, memo, generation, created_at, updated_at from clients_old where created_at  > '2022-09-15' and nusi = false" -A -F, > ../others_add.csv
 
nusiがtrueのもの 両方必要
 
  % psql -d update_dump -U jq -c "select id, user_id, jiin_id, ie_id, name, yomi, nusi, each_order, memo, generation, created_at, updated_at from clients_old where created_at  > '2022-09-15' and nusi = true" -A -F, > ../others_nusi_add.csv
 
  
 
コピーコマンドで追加ができる
 
コピーコマンドで追加ができる
350行目: 347行目:
 
   
 
   
 
  =# \COPY others FROM '../others_add.csv' DELIMITER ',' CSV HEADER;
 
  =# \COPY others FROM '../others_add.csv' DELIMITER ',' CSV HEADER;
 
<nowiki>=# \COPY others FROM '../others_nusi_add.csv' DELIMITER ',' CSV HEADER;
 
COPY 8</nowiki>
 
  
 
ここまでのバックアップを取る
 
ここまでのバックアップを取る
621行目: 615行目:
 
INSERT 0 918</nowiki>
 
INSERT 0 918</nowiki>
  
generate
+
==== generate ====
 
  % rails generate scaffold Place user:references note:references client:references zip:string ken:string city:string etc:string flag:boolean memo:text
 
  % rails generate scaffold Place user:references note:references client:references zip:string ken:string city:string etc:string flag:boolean memo:text
  
901行目: 895行目:
  
 
=== 仕訳帳テーブル ===
 
=== 仕訳帳テーブル ===
==== 12-16 17:18 ====
+
==== 12-16 ====
 
確認
 
確認
 
  <nowiki>latest_dump=# select count(*) from journals;
 
  <nowiki>latest_dump=# select count(*) from journals;
908行目: 902行目:
 
  14885</nowiki>
 
  14885</nowiki>
  
最新をテーブルダンプする
 
% pg_dump --username=jq --table journals latest_dump > ../update_journals.sql
 
  
update_dumpデータベースにリストアする
 
<nowiki>% psql --username=jq update_dump < ../update_journals.sql
 
SET
 
SET
 
SET
 
SET
 
SET
 
set_config
 
------------
 
 
(1 row)
 
  
SET
 
SET
 
SET
 
SET
 
SET
 
SET
 
CREATE TABLE
 
ALTER TABLE
 
CREATE SEQUENCE
 
ALTER TABLE
 
ALTER SEQUENCE
 
ALTER TABLE
 
COPY 14885</nowiki>
 
  
  update_dump=# ALTER TABLE journals RENAME COLUMN client_id TO other_id;
+
==== 以前 ====
  ALTER TABLE
+
確認
 +
  <nowiki>=# select count(*) from journals;
 +
  count
 +
-------
 +
14709
 +
(1 row)</nowiki>
  
  update_dump=# ALTER TABLE journals ADD COLUMN client_id integer;
+
  % rails generate scaffold Journal user:references note:references account:references client_id:integer other_id:integer journal_number:integer date:date schedule_id:integer debit:string debit_money:integer description_id:integer etc:string credit:string credit_money:integer debit_s:integer credit_s:integer
ALTER TABLE
 
  
  update_dump=# ALTER TABLE journals RENAME COLUMN jiin_id TO note_id;
+
  % rails db:migrate
ALTER TABLE
 
  
  =# update journals set account_id = 99 where account_id is null;
+
リストアして作業データベースにコピーする
UPDATE 10379
+
  <nowiki>% pg_dump --username=jq --table journals heroku_dump > ../new_journals.sql
 +
% psql --username=jq update_heroku < ../new_journals.sql</nowiki>
  
  % pg_dump -t journals update_dump > ../journals_latest.sql
+
テーブルの変更
 +
  =# ALTER TABLE journals RENAME COLUMN jiin_id TO note_id;
  
  <nowiki>tep5_development=# delete from journals ;
+
新テーブルのclient_idに合わせる
DELETE 14719
+
  <nowiki>update_heroku=# update journals SET client_id = client_id + 20000 where note_id = 2;
tep5_development=# TRUNCATE TABLE journals RESTART IDENTITY;
+
UPDATE 3663
TRUNCATE TABLE
+
update_heroku=# update journals SET client_id = client_id + 30000 where note_id = 3;
tep5_development=# select * from journals_id_seq ;
+
UPDATE 1815
last_value | log_cnt | is_called
+
update_heroku=# update journals SET client_id = client_id + 40000 where note_id = 4;
------------+---------+-----------
+
UPDATE 719</nowiki>
          1 |      0 | f</nowiki>
 
  
  <nowiki>% psql --username=jq tep5_development < ../journals_latest.sql
+
リストア コピー
 +
  <nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table journals update_heroku > ../new_journals.sql
 +
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_journals.sql              
 
SET
 
SET
 
SET
 
SET
983行目: 957行目:
 
ALTER SEQUENCE
 
ALTER SEQUENCE
 
ALTER TABLE
 
ALTER TABLE
COPY 14885
+
COPY 14709
 
  setval  
 
  setval  
 
--------
 
--------
   15199
+
   15023
 
(1 row)
 
(1 row)
  
 
ERROR:  multiple primary keys for table "journals" are not allowed
 
ERROR:  multiple primary keys for table "journals" are not allowed
ERROR:  relation "index_journals_on_account_id" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_credit_and_credit_money_and_account_id" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_credit_s" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_debit_and_client_id_and_debit_money" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_debit_s" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_description_id_and_schedule_id" already exists
+
CREATE INDEX
ERROR:  relation "index_journals_on_jiin_id" already exists
+
CREATE INDEX
 
ERROR:  relation "index_journals_on_user_id" already exists
 
ERROR:  relation "index_journals_on_user_id" already exists
ERROR:  relation "index_journals_on_user_id_and_jiin_id_and_journal_number" already exists</nowiki>
+
CREATE INDEX
 +
ERROR:  relation "public.jiins" does not exist
 +
ALTER TABLE</nowiki>
  
ここまでのバックアップを取る
+
ダンプ ここまで形を整えたデータベースをバックアップしとく
  % pg_dump -Fc tep5_development > ../202212170834.dump
+
  % pg_dump tep5_development > ../202209270816_tep5.sql
 +
 
 +
==== 替え玉 ====
 +
update_heroku=# insert into accounts(account_cd, content, bs_pl, debit_credit, created_at, updated_at) VALUES ('9999', '替え玉', 1, 1, '2022-09-28', '2022-09-28');
 +
tep5_development=# insert into accounts(account_cd, content, bs_pl, debit_credit, created_at, updated_at) VALUES ('9999', '替え玉', 1, 1, '2022-09-28', '2022-09-28');
  
==== 以前 ====
 
 
確認
 
確認
  <nowiki>=# select count(*) from journals;
+
  <nowiki>tep5_development=# select * from accounts order by id desc limit 1;
 +
id | account_cd | content | bs_pl | debit_credit |    created_at      |    updated_at     
 +
----+------------+---------+-------+--------------+---------------------+---------------------
 +
99 | 9999      | 替え玉  |    1 |            1 | 2022-09-28 00:00:00 | 2022-09-28 00:00:00
 +
(1 row)</nowiki>
 +
 
 +
<nowiki>update_heroku=# select count(*) from journals where account_id is null;
 
  count  
 
  count  
 
-------
 
-------
  14709
+
  10379
 
(1 row)</nowiki>
 
(1 row)</nowiki>
  
  % rails generate scaffold Journal user:references note:references account:references client_id:integer other_id:integer journal_number:integer date:date schedule_id:integer debit:string debit_money:integer description_id:integer etc:string credit:string credit_money:integer debit_s:integer credit_s:integer
+
替え玉挿入
 +
<nowiki>update_heroku=# update journals set account_id = 99 where account_id is null;
 +
UPDATE 10379</nowiki>
 +
 
 +
=== 摘要テーブル 変更あり ===
 +
確認
 +
<nowiki>heroku_dump=# select count(*) from descriptions;
 +
count
 +
-------
 +
  640
 +
(1 row)</nowiki>
 +
 
 +
  % rails generate scaffold Description user:references note:references account:references content:string money:integer desc_line:integer
 +
 
 +
(UUID削除 index追加)
  
 
  % rails db:migrate
 
  % rails db:migrate
  
 
リストアして作業データベースにコピーする
 
リストアして作業データベースにコピーする
  <nowiki>% pg_dump --username=jq --table journals heroku_dump > ../new_journals.sql
+
  % pg_dump --username=jq --table descriptions heroku_dump > ../new_descriptions.sql
% psql --username=jq update_heroku < ../new_journals.sql</nowiki>
+
% psql --username=jq update_heroku < ../new_descriptions.sql
  
 
テーブルの変更
 
テーブルの変更
  =# ALTER TABLE journals RENAME COLUMN jiin_id TO note_id;
+
  =# ALTER TABLE descriptions RENAME COLUMN jiin_id TO note_id;
  
新テーブルのclient_idに合わせる
+
リストア コピー
<nowiki>update_heroku=# update journals SET client_id = client_id + 20000 where note_id = 2;
+
  % pg_dump --username=jq --table descriptions update_heroku > ../new_descriptions.sql
UPDATE 3663
+
<nowiki>% psql --username=jq tep5_development < ../new_descriptions.sql
update_heroku=# update journals SET client_id = client_id + 30000 where note_id = 3;
 
UPDATE 1815
 
update_heroku=# update journals SET client_id = client_id + 40000 where note_id = 4;
 
UPDATE 719</nowiki>
 
 
 
リストア コピー
 
  <nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table journals update_heroku > ../new_journals.sql
 
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_journals.sql              
 
 
SET
 
SET
 
SET
 
SET
1,049行目: 1,040行目:
 
SET
 
SET
 
SET
 
SET
ERROR:  relation "journals" already exists
+
ERROR:  relation "descriptions" already exists
 
ALTER TABLE
 
ALTER TABLE
ERROR:  relation "journals_id_seq" already exists
+
ERROR:  relation "descriptions_id_seq" already exists
 
ALTER TABLE
 
ALTER TABLE
 
ALTER SEQUENCE
 
ALTER SEQUENCE
 
ALTER TABLE
 
ALTER TABLE
COPY 14709
+
COPY 640
 
  setval  
 
  setval  
 
--------
 
--------
  15023
+
    871
 
(1 row)
 
(1 row)
  
ERROR:  multiple primary keys for table "journals" are not allowed
+
ERROR:  multiple primary keys for table "descriptions" are not allowed
 +
ERROR:  relation "index_descriptions_on_account_id" already exists
 
CREATE INDEX
 
CREATE INDEX
 +
ERROR:  relation "index_descriptions_on_user_id" already exists
 
CREATE INDEX
 
CREATE INDEX
CREATE INDEX
+
ALTER TABLE
CREATE INDEX
+
ERROR:  relation "public.jiins" does not exist</nowiki>
CREATE INDEX
+
 
CREATE INDEX
 
CREATE INDEX
 
ERROR:  relation "index_journals_on_user_id" already exists
 
CREATE INDEX
 
ERROR:  relation "public.jiins" does not exist
 
ALTER TABLE</nowiki>
 
  
 
ダンプ ここまで形を整えたデータベースをバックアップしとく
 
ダンプ ここまで形を整えたデータベースをバックアップしとく
 
  % pg_dump tep5_development > ../202209270816_tep5.sql
 
  % pg_dump tep5_development > ../202209270816_tep5.sql
  
==== 替え玉 ====
+
=== 戒名テーブル 変更あり ===
update_heroku=# insert into accounts(account_cd, content, bs_pl, debit_credit, created_at, updated_at) VALUES ('9999', '替え玉', 1, 1, '2022-09-28', '2022-09-28');
 
tep5_development=# insert into accounts(account_cd, content, bs_pl, debit_credit, created_at, updated_at) VALUES ('9999', '替え玉', 1, 1, '2022-09-28', '2022-09-28');
 
 
 
 
確認
 
確認
  <nowiki>tep5_development=# select * from accounts order by id desc limit 1;
+
  <nowiki>heroku_dump=# select count(*) from kaimyous;
id | account_cd | content | bs_pl | debit_credit |    created_at      |    updated_at     
 
----+------------+---------+-------+--------------+---------------------+---------------------
 
99 | 9999      | 替え玉  |    1 |            1 | 2022-09-28 00:00:00 | 2022-09-28 00:00:00
 
(1 row)</nowiki>
 
 
 
<nowiki>update_heroku=# select count(*) from journals where account_id is null;
 
 
  count  
 
  count  
 
-------
 
-------
10379
+
  3375
 
(1 row)</nowiki>
 
(1 row)</nowiki>
  
替え玉挿入
+
  rails generate scaffold Kaimyou user:references note:references client:references other:references content:string birth:date death:date g_age:string relationship:string memo:text
  <nowiki>update_heroku=# update journals set account_id = 99 where account_id is null;
 
UPDATE 10379</nowiki>
 
  
=== 摘要テーブル 変更あり ===
+
(UUID削除 index追加)
確認
 
<nowiki>heroku_dump=# select count(*) from descriptions;
 
count
 
-------
 
  640
 
(1 row)</nowiki>
 
 
 
% rails generate scaffold Description user:references note:references account:references content:string money:integer desc_line:integer
 
 
 
(UUID削除 index追加)
 
  
 
  % rails db:migrate
 
  % rails db:migrate
  
 
リストアして作業データベースにコピーする
 
リストアして作業データベースにコピーする
  % pg_dump --username=jq --table descriptions heroku_dump > ../new_descriptions.sql
+
  % pg_dump --username=jq --table kaimyous heroku_dump > ../new_kaimyous.sql
  % psql --username=jq update_heroku < ../new_descriptions.sql
+
  % psql --username=jq update_heroku < ../new_kaimyous.sql
  
 
テーブルの変更
 
テーブルの変更
  =# ALTER TABLE descriptions RENAME COLUMN jiin_id TO note_id;
+
  <nowiki>update_heroku=# ALTER TABLE kaimyous RENAME COLUMN jiin_id TO note_id;
 
+
ALTER TABLE
リストア コピー
+
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN client_id TO other_id;
% pg_dump --username=jq --table descriptions update_heroku > ../new_descriptions.sql
+
ALTER TABLE
<nowiki>% psql --username=jq tep5_development < ../new_descriptions.sql
+
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN ie_id TO client_id;
SET
+
ALTER TABLE
SET
+
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN date TO death;
SET
 
SET
 
SET
 
set_config
 
------------
 
 
(1 row)
 
 
 
SET
 
SET
 
SET
 
SET
 
SET
 
SET
 
ERROR:  relation "descriptions" already exists
 
 
ALTER TABLE
 
ALTER TABLE
ERROR:  relation "descriptions_id_seq" already exists
+
update_heroku=# ALTER TABLE kaimyous ADD COLUMN birth date;
 
ALTER TABLE
 
ALTER TABLE
ALTER SEQUENCE
+
update_heroku=# ALTER TABLE kaimyous DROP COLUMN ie_id_v;
 
ALTER TABLE
 
ALTER TABLE
COPY 640
+
update_heroku=# ALTER TABLE kaimyous ADD COLUMN memo text;
setval
+
ALTER TABLE</nowiki>
--------
 
    871
 
(1 row)
 
  
ERROR:  multiple primary keys for table "descriptions" are not allowed
+
新テーブルのclient_id other_idに合わせる other_idは一意なので変更しない
ERROR: relation "index_descriptions_on_account_id" already exists
+
  <nowiki>update_heroku=# update kaimyous SET client_id = client_id + 20000 where note_id = 2;
CREATE INDEX
+
UPDATE 587
ERROR:  relation "index_descriptions_on_user_id" already exists
+
update_heroku=# update kaimyous SET client_id = client_id + 30000 where note_id = 3;
CREATE INDEX
+
UPDATE 662
ALTER TABLE
+
update_heroku=# update kaimyous SET client_id = client_id + 40000 where note_id = 4;
ERROR:  relation "public.jiins" does not exist</nowiki>
+
UPDATE 256</nowiki>
  
  
ダンプ ここまで形を整えたデータベースをバックアップしとく
+
エラー clientsテーブルのidとkaimyousテーブルのclient_idを比較する
  % pg_dump tep5_development > ../202209270816_tep5.sql
+
  <nowiki>update_heroku=# SELECT id FROM clients
 
+
EXCEPT
=== 戒名テーブル 変更あり ===
+
SELECT client_id FROM kaimyous
==== 12-17 11:14 ====
+
UNION ALL
確認
+
SELECT client_id FROM kaimyous
<nowiki>latest_dump=# select count(*) from kaimyous;
+
EXCEPT
count
+
SELECT id FROM clients order by id;
 +
  id 
 
-------
 
-------
  3445</nowiki>
+
    21
 
+
    27
増分を書き出す
+
    35
% psql -d latest_dump -U jq -c "select id, user_id, jiin_id, ie_id, client_id, content, date, g_age, relationship, ie_id_v, created_at, updated_at from kaimyous where created_at >= '2022-09-13' order by jiin_id" -A -F, > ../kaimyous_add.csv
+
    38
 
+
    39
ie_id(client_id)は修正する ie_id_vはmemoに対応するために書き出した
+
    44
 
+
    46
コピーで追加
+
    56
<nowiki>=# \COPY kaimyous FROM '../kaimyous_add.csv' DELIMITER ',' CSV HEADER;
+
    57
COPY 70</nowiki>
+
    63
 
+
    71
==== 以前 ====
+
    73
確認
+
    78
<nowiki>heroku_dump=# select count(*) from kaimyous;
+
    89
count
+
  101
-------
+
  106
  3375
+
  111
(1 row)</nowiki>
+
  126
 
+
  127
rails generate scaffold Kaimyou user:references note:references client:references other:references content:string birth:date death:date g_age:string relationship:string memo:text
+
  135
 
+
  144
(UUID削除 index追加)
+
  147
 
+
  156
% rails db:migrate
+
  173
 
+
  174
リストアして作業データベースにコピーする
+
  176
% pg_dump --username=jq --table kaimyous heroku_dump > ../new_kaimyous.sql
+
  186
% psql --username=jq update_heroku < ../new_kaimyous.sql
+
  188
 
+
  189
テーブルの変更
+
  230
<nowiki>update_heroku=# ALTER TABLE kaimyous RENAME COLUMN jiin_id TO note_id;
+
  310
ALTER TABLE
+
  373
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN client_id TO other_id;
+
  404
ALTER TABLE
+
  473
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN ie_id TO client_id;
+
  701
ALTER TABLE
+
  804
update_heroku=# ALTER TABLE kaimyous RENAME COLUMN date TO death;
+
  872
ALTER TABLE
+
  936
update_heroku=# ALTER TABLE kaimyous ADD COLUMN birth date;
+
  953
ALTER TABLE
+
  972
update_heroku=# ALTER TABLE kaimyous DROP COLUMN ie_id_v;
+
  998
ALTER TABLE
+
  1590
update_heroku=# ALTER TABLE kaimyous ADD COLUMN memo text;
+
  1671
ALTER TABLE</nowiki>
+
  1777
 +
  2249
 +
  2290
 +
22653
 +
34681
 +
(48 rows)</nowiki>
  
新テーブルのclient_id other_idに合わせる other_idは一意なので変更しない
+
書き出す
  <nowiki>update_heroku=# update kaimyous SET client_id = client_id + 20000 where note_id = 2;
+
  <nowiki>% psql update_heroku -c "SELECT id FROM clients
UPDATE 587
 
update_heroku=# update kaimyous SET client_id = client_id + 30000 where note_id = 3;
 
UPDATE 662
 
update_heroku=# update kaimyous SET client_id = client_id + 40000 where note_id = 4;
 
UPDATE 256</nowiki>
 
 
 
 
 
エラー clientsテーブルのidとkaimyousテーブルのclient_idを比較する
 
<nowiki>update_heroku=# SELECT id FROM clients
 
 
EXCEPT
 
EXCEPT
SELECT client_id FROM kaimyous  
+
SELECT client_id FROM kaimyous
 
UNION ALL
 
UNION ALL
SELECT client_id FROM kaimyous  
+
SELECT client_id FROM kaimyous
 
EXCEPT
 
EXCEPT
SELECT id FROM clients order by id;
+
SELECT id FROM clients order by id;" > ../hikaku/clients_kaimyous_client_id.csv</nowiki>
   id    
+
 
-------
+
,挿入 改行削除
    21
+
:%s/\n//g
    27
+
 
    35
+
削除する戒名を書き出す
    38
+
<nowiki>% psql update_heroku -c "select * from kaimyous where client_id in (21,    27,    35,    38,    39,    44,    46,    56,    57,    63,    71,    73,    78,    89,  101,  106,  111,   126,   127,  135,  144,  147,  156,  173,  174,  176,  186,  188,  189,  230,  310,  373,  404,  473,  701,  804,  872,  936,  953,  972,  998,  1590,  1671,  1777,  2249,  2290, 22653, 34681);" > ../tmp/delete_kaimyou.csv</nowiki>
    39
+
 
    44
+
削除
    46
+
<nowiki>update_heroku=# delete from kaimyous where client_id in (21,    27,    35,    38,    39,    44,    46,    56,    57,    63,    71,    73,    78,    89101106111126127135144147156,  173,  174,  176,  186,  188,  189,  230,  310,  373,  404,  473,  701,  804,  872,  936,  953,  972,  998,  1590,  1671,  1777,  2249,  2290, 22653, 34681);
    56
+
DELETE 81</nowiki>
    57
+
 
    63
+
ダンプ リストア
    71
+
<nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table kaimyous update_heroku > ../new_kaimyous.sql
    73
+
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_kaimyous.sql
    78
+
SET
    89
+
SET
  101
+
SET
  106
+
SET
  111
+
SET
  126
+
set_config
  127
+
------------
  135
+
  144
+
(1 row)
  147
+
 
  156
+
SET
  173
+
SET
  174
+
SET
  176
+
SET
  186
+
SET
  188
+
SET
  189
+
ERROR:  relation "kaimyous" already exists
  230
+
ALTER TABLE
  310
+
ERROR: relation "kaimyous_id_seq" already exists
  373
+
ALTER TABLE
  404
+
ALTER SEQUENCE
  473
+
ALTER TABLE
  701
+
COPY 3294
  804
+
  setval
  872
+
--------
  936
+
  4230
  953
+
(1 row)
  972
+
 
  998
+
ERROR:  multiple primary keys for table "kaimyous" are not allowed
  1590
+
ERROR:  relation "index_kaimyous_on_client_id" already exists
  1671
+
ERROR:  relation "index_kaimyous_on_client_id_and_content" already exists
  1777
+
ERROR:  relation "index_kaimyous_on_date" already exists
  2249
+
ERROR:  relation "index_kaimyous_on_ie_id" already exists
  2290
+
ERROR:  relation "index_kaimyous_on_jiin_id" already exists
  22653
+
ERROR:  relation "index_kaimyous_on_user_id" already exists
  34681
+
ERROR:  relation "index_kaimyous_on_user_id_and_jiin_id" already exists
(48 rows)</nowiki>
+
ERROR:  relation "public.jiins" does not exist</nowiki>
 +
 
 +
ダンプ ここまで形を整えたデータベースをバックアップしとく
 +
% pg_dump tep5_development > ../202210031034_tep5.sql
  
書き出す
+
=== 通帳テーブル ===
  <nowiki>% psql update_heroku -c "SELECT id FROM clients
+
確認
EXCEPT
+
  <nowiki>heroku_dump=# select count(*) from bankbooks;
SELECT client_id FROM kaimyous
+
count
UNION ALL
+
-------
SELECT client_id FROM kaimyous
+
    36</nowiki>
EXCEPT
 
SELECT id FROM clients order by id;" > ../hikaku/clients_kaimyous_client_id.csv</nowiki>
 
  
,挿入 改行削除
+
  % rails generate scaffold Bankbook user:references note:references content:string memo:text
  :%s/\n//g
 
  
削除する戒名を書き出す
+
やり直し
  <nowiki>% psql update_heroku -c "select * from kaimyous where client_id in (21,    27,    35,    38,    39,    44,    46,    56,    57,    63,    71,    73,    78,    89,  101,  106,  111,  126,  127,  135,  144,  147,  156,  173,  174,  176,  186,  188,  189,  230,  310,  373,  404,  473,  701,  804,  872,  936,  953,  972,  998, 1590,  1671,  1777,  2249,  2290, 22653, 34681);" > ../tmp/delete_kaimyou.csv</nowiki>
+
  % rails destroy scaffold Bankbook user:references note:references content:string memo:text
 +
  % rails generate scaffold Bankbook user:references note:references content:string bank_line:integer memo:text
  
削除
+
(UUID削除 index追加)
  <nowiki>update_heroku=# delete from kaimyous where client_id in (21,    27,    35,    38,    39,    44,    46,    56,    57,    63,    71,    73,    78,    89,  101,  106,  111,  126,  127,  135,  144,  147,  156,  173,  174,  176,  186,  188,  189,  230,  310,  373,  404,  473,  701,  804,  872,  936,  953,  972,  998,  1590,  1671,  1777,  2249,  2290, 22653, 34681);
+
  % rails db:migrate
DELETE 81</nowiki>
 
  
ダンプ リストア
+
リストアして作業データベースにコピーする
  <nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table kaimyous update_heroku > ../new_kaimyous.sql
+
% pg_dump --username=jq --table bankbooks heroku_dump > ../new_bankbooks.sql
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_kaimyous.sql
+
% psql --username=jq update_heroku < ../new_bankbooks.sql
 +
 
 +
テーブルの変更
 +
update_heroku=# ALTER TABLE bankbooks RENAME COLUMN jiin_id TO note_id;
 +
ALTER TABLE
 +
 
 +
ダンプ リストア
 +
  <nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table bankbooks update_heroku > ../new_bankbooks.sql
 +
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_bankbooks.sql
 
SET
 
SET
 
SET
 
SET
1,320行目: 1,273行目:
 
SET
 
SET
 
SET
 
SET
ERROR:  relation "kaimyous" already exists
+
ERROR:  relation "bankbooks" already exists
 
ALTER TABLE
 
ALTER TABLE
ERROR:  relation "kaimyous_id_seq" already exists
+
ERROR:  relation "bankbooks_id_seq" already exists
 
ALTER TABLE
 
ALTER TABLE
 
ALTER SEQUENCE
 
ALTER SEQUENCE
 
ALTER TABLE
 
ALTER TABLE
COPY 3294
+
COPY 36
 
  setval  
 
  setval  
 
--------
 
--------
  4230
+
    68
 
(1 row)
 
(1 row)
  
ERROR:  multiple primary keys for table "kaimyous" are not allowed
+
ERROR:  multiple primary keys for table "bankbooks" are not allowed
ERROR:  relation "index_kaimyous_on_client_id" already exists
+
CREATE INDEX
ERROR:  relation "index_kaimyous_on_client_id_and_content" already exists
+
ERROR:  relation "index_bankbooks_on_user_id" already exists
ERROR:  relation "index_kaimyous_on_date" already exists
+
CREATE INDEX
ERROR:  relation "index_kaimyous_on_ie_id" already exists
 
ERROR:  relation "index_kaimyous_on_jiin_id" already exists
 
ERROR:  relation "index_kaimyous_on_user_id" already exists
 
ERROR:  relation "index_kaimyous_on_user_id_and_jiin_id" already exists
 
 
ERROR:  relation "public.jiins" does not exist</nowiki>
 
ERROR:  relation "public.jiins" does not exist</nowiki>
  
 
ダンプ ここまで形を整えたデータベースをバックアップしとく
 
ダンプ ここまで形を整えたデータベースをバックアップしとく
  % pg_dump tep5_development > ../202210031034_tep5.sql
+
  % pg_dump tep5_development > ../202211021454_tep5.sql
  
=== 通帳テーブル ===
+
=== relationshipsテーブル ===
 
確認
 
確認
  <nowiki>heroku_dump=# select count(*) from bankbooks;
+
  <nowiki>latest_dump=# select count(*) from relationships;
 
  count  
 
  count  
 
-------
 
-------
     36</nowiki>
+
     40</nowiki>
  
  % rails generate scaffold Bankbook user:references note:references content:string memo:text
+
relationshipsテーブル ダンプ
 +
  % pg_dump --username=jq --table relationships latest_dump > ../new_relationships.sql
  
やり直し
+
update_dumpデータベースにリストアしてロール列を削除する
  % rails destroy scaffold Bankbook user:references note:references content:string memo:text
+
  % psql --username=jq update_dump < ../new_relationships.sql
  % rails generate scaffold Bankbook user:references note:references content:string bank_line:integer memo:text
+
  =# ALTER TABLE relationships DROP COLUMN role;
  
(UUID削除 index追加)
+
修正したテーブルをダンプする
  % rails db:migrate
+
  % pg_dump --username=jq --table relationships update_dump > ../relationships_dump.sql
  
リストアして作業データベースにコピーする
+
リストアする
% pg_dump --username=jq --table bankbooks heroku_dump > ../new_bankbooks.sql
+
  % psql -d tep5_development < ../relationships_dump.sql
  % psql --username=jq update_heroku < ../new_bankbooks.sql
 
  
テーブルの変更
+
バックアップを取る -Fc カスタム形式 圧縮ファイル形式
  update_heroku=# ALTER TABLE bankbooks RENAME COLUMN jiin_id TO note_id;
+
  % pg_dump -Fc tep5_development > ../202212141027.dump
ALTER TABLE
 
  
ダンプ リストア
+
== UUIDを有効にする ==
<nowiki>jq@MacBook-Pro tep5 % pg_dump --username=jq --table bankbooks update_heroku > ../new_bankbooks.sql
+
=== 設定 ===
jq@MacBook-Pro tep5 % psql --username=jq tep5_development < ../new_bankbooks.sql
+
  % rails generate migration enable_extension_for_uuid
SET
 
SET
 
SET
 
SET
 
SET
 
  set_config
 
------------
 
 
(1 row)
 
  
SET
+
migrate編集
SET
+
<nowiki>class EnableExtensionForUuid < ActiveRecord::Migration[7.0]
SET
+
  def change                 
SET
+
    enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
SET
+
  end
SET
+
end</nowiki>
ERROR: relation "bankbooks" already exists
 
ALTER TABLE
 
ERROR: relation "bankbooks_id_seq" already exists
 
ALTER TABLE
 
ALTER SEQUENCE
 
ALTER TABLE
 
COPY 36
 
setval
 
--------
 
    68
 
(1 row)
 
  
ERROR:  multiple primary keys for table "bankbooks" are not allowed
+
  % rails db:migrate
CREATE INDEX
 
ERROR: relation "index_bankbooks_on_user_id" already exists
 
CREATE INDEX
 
ERROR: relation "public.jiins" does not exist</nowiki>
 
  
ダンプ ここまで形を整えたデータベースをバックアップしとく
+
デフォルトのキーをuuidにする
  % pg_dump tep5_development > ../202211021454_tep5.sql
+
  <nowiki>config/initializers/generators.rb 作成
  
=== relationshipsテーブル ===
+
Rails.application.config.generators do |g|
確認
+
  g.orm :active_record, primary_key_type: :uuid
<nowiki>latest_dump=# select count(*) from relationships;
+
end</nowiki>
count
 
-------
 
    40</nowiki>
 
  
relationshipsテーブル ダンプ
+
=== 途中からUUIDに変更する場合 ===
  % pg_dump --username=jq --table relationships latest_dump > ../new_relationships.sql
+
migrationファイル追加
 +
  <nowiki> % rails generate migration enable_extension_for_uuid
  
update_dumpデータベースにリストアしてロール列を削除する
+
% rails generate migration change_user_id_type_to_uuid
% psql --username=jq update_dump < ../new_relationships.sql
 
=# ALTER TABLE relationships DROP COLUMN role;
 
  
修正したテーブルをダンプする
+
% rails destroy migration change_user_id_type_to_uuid
% pg_dump --username=jq --table relationships update_dump > ../relationships_dump.sql
 
  
リストアする
+
% rails generate migration change_users_id_type_to_uuid
% psql -d tep5_development < ../relationships_dump.sql
 
  
バックアップを取る -Fc カスタム形式 圧縮ファイル形式
+
% rails generate migration change_notes_id_type_to_uuid
% pg_dump -Fc tep5_development > ../202212141027.dump
 
  
== house_note ==
+
% rails generate migration change_lists_id_type_to_uuid
旧データベースからダンプ
 
% pg_dump --username=jq --table house_notes latest_dump > ../hns.sql
 
  
サーバーへ転送
+
% rails generate migration change_clients_id_type_to_uuid
% rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/hns.sql jq:/home/jq/dump_data
 
  
リストア
+
% rails generate migration change_others_id_type_to_uuid</nowiki>
$ psql update_dump < ../hns.sql
 
  
clientsのidとhouse_noteのidをあわせる
+
=== UUIDに変更したデータベースをDUMPする ===
  <nowiki>tep5_production=# update house_notes SET ie_id = ie_id + 20000 where jiin_id = 2;
+
==== 修正した旧データのテーブルも一緒にDUMPしとく ====
UPDATE 30
+
==== 不要なテーブルを削除し新しいUUIDのデータベースをDUMPする ====
tep5_production=# update house_notes SET ie_id = ie_id + 30000 where jiin_id = 3;
+
  <nowiki>tep5_development=# drop table clients_old ;
UPDATE 114
+
DROP TABLE
tep5_production=# update house_notes SET ie_id = ie_id + 40000 where jiin_id = 4;
+
tep5_development=# drop table jiins ;
UPDATE 25</nowiki>
+
DROP TABLE
 +
tep5_development=# drop table users_old ;</nowiki>
  
セレクトしてアップデートする
+
  % pg_dump tep5_development > tep5_uuid.sql
  tep5_production=# UPDATE clients as Cl SET memo = (SELECT content FROM house_notes as Hn WHERE Hn.ie_id = Cl.id);
 
  
削除
+
== generate ==
=# drop table house_notes ;
 
  
== UUIDを有効にする ==
+
  $ rails generate scaffold User name:string email:string password_digest:string remember_digest:string role:integer activation_digest:string activated:boolean activated_at:datetime reset_digest:string reset_sent_at:datetime
=== 設定 ===
 
  % rails generate migration enable_extension_for_uuid
 
  
migrate編集
+
{{font color||yellow|migrationファイルを確認 indexを書く 以下同じ}}
<nowiki>class EnableExtensionForUuid < ActiveRecord::Migration[7.0]
 
  def change                 
 
    enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
 
  end
 
end</nowiki>
 
  
  % rails db:migrate
+
  % rails generate scaffold Note user:references content:string kind:integer meeting_time:integer role:integer note_line:integer
  
デフォルトのキーをuuidにする
+
  % rails generate scaffold List user:references note:references content:string list_line:integer
  <nowiki>config/initializers/generators.rb 作成
 
  
Rails.application.config.generators do |g|
+
% rails generate scaffold Client user:references note:references list:references content:string client_line:integer memo:text
  g.orm :active_record, primary_key_type: :uuid
 
end</nowiki>
 
  
=== 途中からUUIDに変更する場合 ===
+
  % rails generate scaffold Other user:references note:references client:references name:string kana:string client_name:boolean other_line:integer memo:text generation:string
migrationファイル追加
 
  <nowiki> % rails generate migration enable_extension_for_uuid
 
  
% rails generate migration change_user_id_type_to_uuid
+
% rails generate scaffold Category user:references note:references content:string category_line:integer
  
% rails destroy migration change_user_id_type_to_uuid
+
% rails generate scaffold Relationship_category user:references note:references client_id:integer category_id:integer
  
% rails generate migration change_users_id_type_to_uuid
+
% rails generate model Relationship follower_id:integer followed_id:integer
  
% rails generate migration change_notes_id_type_to_uuid
+
$ rails generate controller Relationships
  
% rails generate migration change_lists_id_type_to_uuid
+
やり直し
 +
% rails destroy scaffold User .................
  
% rails generate migration change_clients_id_type_to_uuid
+
$ rails generate scaffold Post content:text user:references picture:string
  
% rails generate migration change_others_id_type_to_uuid</nowiki>
+
$ rails generate model Relationship follower_id:integer followed_id:integer
  
=== UUIDに変更したデータベースをDUMPする ===
+
  $ rails db:migrate RAILS_ENV=production
==== 修正した旧データのテーブルも一緒にDUMPしとく ====
 
==== 不要なテーブルを削除し新しいUUIDのデータベースをDUMPする ====
 
  <nowiki>tep5_development=# drop table clients_old ;
 
DROP TABLE
 
tep5_development=# drop table jiins ;
 
DROP TABLE
 
tep5_development=# drop table users_old ;</nowiki>
 
  
  % pg_dump tep5_development > tep5_uuid.sql
+
  $ bin/dev
  
== generate ==
+
$ rails generate controller StaticPages home help about contact
  
  $ rails generate scaffold User name:string email:string password_digest:string remember_digest:string role:integer activation_digest:string activated:boolean activated_at:datetime reset_digest:string reset_sent_at:datetime
+
エラー
 +
  ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.
  
{{font color||yellow|migrationファイルを確認 indexを書く 以下同じ}}
+
config/envitonments/production.rb
 +
config.assets.compile = true
  
  % rails generate scaffold Note user:references content:string kind:integer meeting_time:integer role:integer note_line:integer
+
== サーバーへ転送 ==
 +
除外するファイルをまとめる
 +
  <nowiki>exclude-file.txt
 +
    production.rb
 +
    database.yml</nowiki>
  
  % rails generate scaffold List user:references note:references content:string list_line:integer
+
  % rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/ jq:/home/jq/tep5/
  
  % rails generate scaffold Client user:references note:references list:references content:string client_line:integer memo:text
+
  % rsync ~/rails_app_tepnote/202210031034_tep5.sql jq:/home/jq/dump_data
  
  % rails generate scaffold Other user:references note:references client:references name:string kana:string client_name:boolean other_line:integer memo:text generation:string
+
== サーバー側 ==
 +
  $ bundle install  --without test development
  
% rails generate scaffold Category user:references note:references content:string category_line:integer
+
{{font color||yellow|database.yml username:などを確認する (2箇所)}}
  
  % rails generate scaffold Relationship_category user:references note:references client_id:integer category_id:integer
+
  /config/environments/production.rb
  
  % rails generate model Relationship follower_id:integer followed_id:integer
+
データベース削除
 +
  postgres=# DROP DATABASE tep5_production;
  
  $ rails generate controller Relationships
+
データベース作成
 +
  $ createdb tep5_production
  
やり直し
+
リストア
  % rails destroy scaffold User .................
+
  <nowiki>$ psql tep5_production < ../dump_data/tep5_uuid_dump.sql
 
+
SET
$ rails generate scaffold Post content:text user:references picture:string
+
SET
 
+
SET
  $ rails generate scaffold Post user:references title:string content:text notification_time:datetime reminder_type:integer post_type:integer picture:string
+
SET
 
+
SET
  $ rails generate model Relationship follower_id:integer followed_id:integer
+
  set_config
 +
------------
 +
   
 +
(1 row)
  
$ rails db:migrate RAILS_ENV=production
+
SET
 
+
SET
$ bin/dev
+
SET
 
+
SET
$ rails generate controller StaticPages home help about contact
+
CREATE EXTENSION
 
+
COMMENT
エラー
+
SET
ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.
+
SET
 
+
CREATE TABLE
config/envitonments/production.rb
+
ALTER TABLE
config.assets.compile = true
+
CREATE TABLE
 
+
ALTER TABLE
=== カラム追加 ===
+
CREATE TABLE
<nowiki>% rails generate migration add_memo_to_schedules memo:text
+
ALTER TABLE
      invoke  active_record
+
CREATE TABLE
      create    db/migrate/20230311013341_add_memo_to_schedules.rb
+
ALTER TABLE
% rails db:migrate
+
CREATE TABLE
== 20230311013341 AddMemoToSchedules: migrating ===============================
+
ALTER TABLE
-- add_column(:schedules, :memo, :text)
+
CREATE TABLE
  -> 0.0056s
+
ALTER TABLE
== 20230311013341 AddMemoToSchedules: migrated (0.0057s) ======================</nowiki>
+
CREATE TABLE
 
+
ALTER TABLE
=== 外部キー制約に変更 ===
+
COPY 1
rails generate migration AddScheduleReferenceToJournal schedule:references
+
COPY 1295
 
+
COPY 1
== サーバーへ転送 ==
+
COPY 14
除外するファイルをまとめる
+
COPY 4747
<nowiki>exclude-file.txt
+
COPY 11
    production.rb
+
COPY 17
    database.yml</nowiki>
+
ALTER TABLE
 
+
ALTER TABLE
すべて
+
ALTER TABLE
% rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/ jq:/home/jq/tep5/
+
ALTER TABLE
 
+
ALTER TABLE
appのみ
+
ALTER TABLE
% rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/app/ jq:/home/jq/tep5/app/
+
ALTER TABLE
 
+
CREATE INDEX
データベース
+
CREATE INDEX
% rsync ~/rails_app_tepnote/202212170834.dump jq:/home/jq/dump_data
+
CREATE INDEX
 
+
CREATE INDEX
== サーバー側 ==
+
CREATE INDEX
リストア
+
CREATE INDEX
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U jq -d tep5_ production  ../dump_data/202212141027.dump
+
CREATE INDEX
 
+
CREATE INDEX
$ bundle install  --without test development
+
CREATE INDEX
 
+
CREATE INDEX
{{font color||yellow|database.yml username:などを確認する (2箇所)}}
+
ALTER TABLE
 
+
ALTER TABLE
/config/environments/production.rb
+
ALTER TABLE
 
+
ALTER TABLE
データベース削除
+
ALTER TABLE
~$ dropdb tep5_production
+
ALTER TABLE
または
+
ALTER TABLE
postgres=# DROP DATABASE tep5_production;
+
ALTER TABLE
 +
ALTER TABLE</nowiki>
  
データベース作成
+
  $ rails db:migrate RAILS_ENV=production
$ createdb tep5_production
 
 
 
リストア
 
<nowiki>$ psql tep5_production < ../dump_data/tep5_uuid_dump.sql
 
SET
 
SET
 
SET
 
SET
 
SET
 
set_config
 
------------
 
 
(1 row)
 
 
 
SET
 
SET
 
SET
 
SET
 
CREATE EXTENSION
 
COMMENT
 
SET
 
SET
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
CREATE TABLE
 
ALTER TABLE
 
COPY 1
 
COPY 1295
 
COPY 1
 
COPY 14
 
COPY 4747
 
COPY 11
 
COPY 17
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
CREATE INDEX
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE
 
ALTER TABLE</nowiki>
 
 
 
  $ rails db:migrate RAILS_ENV=production
 
 
 
=== シーケンス操作 ===
 
確認
 
<nowiki>=# select * from others_id_seq ;
 
last_value | log_cnt | is_called
 
------------+---------+-----------
 
        14 |      30 | t</nowiki>
 
 
 
最大値確認
 
<nowiki>=# select max(id) from others;
 
max 
 
------
 
5428</nowiki>
 
 
 
変更
 
<nowiki>=# SELECT setval('others_id_seq', 5428, true);
 
setval
 
--------
 
  5428</nowiki>
 
 
 
=== バックアップ ===
 
dump
 
$ pg_dump -Fc tep5_production > ~/dump_data/202212180817.dump
 
 
 
ローカル側からコピーして二箇所で保存する
 
% scp jq:/home/jq/dump_data/202212180817.dump /Users/jq/rails_app_tepnote
 
 
 
== データベース変更 ==
 
=== まるごと ===
 
ダンプ 本番データベースバックアップ データのみ
 
$ pg_dump -a tep5_production > ~/dump_data/202212270953_data_only.sql
 
 
 
ローカルからコピー
 
% scp jq:/home/jq/dump_data/202212270953_data_only.sql /Users/jq/rails_app_tepnote
 
 
 
ローカルデータベース削除
 
% rails db:drop
 
 
 
データベース作成
 
% rails db:create
 
 
 
マイグレーション変更
 
 
 
マイグレーション
 
% rails db:migrate
 
 
 
リストア
 
% psql --username=jq tep5_development < ../202212270953_data_only.sql
 
 
 
=== カラム追加 boolean ===
 
% rails generate migration add_payment_to_descriptions payment:boolean
 
 
 
add_column :descriptions, :payment, :boolean, default: false
 
 
 
$ rails db:migrate
 
 
 
=== あとから外部キー制約を追加する手順 ===
 
親テーブルと子テーブルのIDを比較する 不整合があればテーブルを修正する
 
<nowiki>=# SELECT id FROM others
 
EXCEPT
 
SELECT other_id FROM journals
 
UNION ALL
 
SELECT other_id FROM journals
 
EXCEPT
 
SELECT id FROM others order by id;
 
  id 
 
------
 
 2435</nowiki>
 
 
 
外部キー制約を追加するmigration ファイルをつくる
 
<nowiki> % rails generate migration AddClientReferenceToJournal client:references
 
      invoke  active_record
 
      create    db/migrate/20230509044227_add_client_reference_to_journal.rb
 
% rails generate migration AddOtherReferenceToJournal other:references
 
      invoke  active_record
 
      create    db/migrate/20230509044353_add_other_reference_to_journal.rb</nowiki>
 
 
 
rails db:migrateでエラー
 
 
 
すでにother_idカラムが存在するのでadd_referenceメソッドが実行できない
 
 
 
直接データベースを操作してother_idカラム名をhoge_idカラム名に変更する
 
 
 
ALTER TABLE journals RENAME COLUMN other_id TO hoge_id;
 
 
 
rails db:migrate
 
 
 
hoge_idカラムの値をother_idカラムにコピーする
 
UPDATE journals SET other_id = hoge_id;
 
 
 
ローカルならrails consoleを使用して
 
 
 
Journal.update_all('other_id = hoge_id')
 
 
 
hoge_idを削除する
 
ALTER TABLE journals DROP COLUMN hoge_id;
 
 
 
=== エラー ===
 
例えば待避したhoge_idを戻すとき次のようなエラーがでる
 
<nowiki>=# UPDATE journals SET schedule_id = hoge_id;
 
ERROR:  insert or update on table "journals" violates foreign key constraint "fk_rails_9d4d4268db"
 
DETAIL:  Key (schedule_id)=(970) is not present in table "schedules".</nowiki>
 
journalsテーブルのschedule_idがschedulesテーブルにないのでそれを拾い出して削除する
 
<nowiki>=# delete from journals where hoge_id in (
 
SELECT id FROM schedules
 
EXCEPT
 
SELECT hoge_id FROM journals
 
UNION ALL
 
SELECT hoge_id FROM journals
 
EXCEPT
 
SELECT id FROM schedules);</nowiki>
 
 
 
== gemアップデート ==
 
確認
 
% gem -v
 
アップデート対象Gemを一覧表示
 
% bundle outdated
 
 
 
== 備忘 ==
 
現在UUIDコメントアウト中 config/initializers/generators.rb
 

wikipokpokへの投稿はすべて、クリエイティブ・コモンズ 表示-継承 (詳細はWikipokpok:著作権を参照)のもとで公開したと見なされることにご注意ください。 自分が書いたものが他の人に容赦なく編集され、自由に配布されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください。 著作権保護されている作品は、許諾なしに投稿しないでください!

このページを編集するには、下記の確認用の質問に回答してください (詳細):

取り消し | 編集の仕方 (新しいウィンドウで開きます)

このページで使用されているテンプレート: