差分

Tep3-onk-2

7,510 バイト追加, 2023年6月27日 (火) 09:43
あとから外部キー制約を追加する手順
[[category:memo|{{PAGENAME}}]]
{{Font color}}
== tep5 準備 ==
プロジェクトディレクトリ作成 移動 [[rails前バージョン確認|バージョン確認]]
==== 2022-12 -14 othersテーブル ====
09-15以降の増分を取る15以降の増分を取る nusiがfalseのもの
% 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
コピーコマンドで追加ができる
=# \COPY others FROM '../others_add.csv' DELIMITER ',' CSV HEADER;
 
<nowiki>=# \COPY others FROM '../others_nusi_add.csv' DELIMITER ',' CSV HEADER;
COPY 8</nowiki>
ここまでのバックアップを取る
INSERT 0 918</nowiki>
==== generate ====
% rails generate scaffold Place user:references note:references client:references zip:string ken:string city:string etc:string flag:boolean memo:text
=== 仕訳帳テーブル ===
==== 12-16 17:18 ====
確認
<nowiki>latest_dump=# select count(*) from journals;
tep5_development=# TRUNCATE TABLE journals RESTART IDENTITY;
TRUNCATE TABLE
tep5_development=# select * from journals
journals journals_id_seq
tep5_development=# select * from journals_id_seq ;
last_value | log_cnt | is_called
1 | 0 | f</nowiki>
==== 以前 ====確認 <nowiki>% psql --username=# select count(*) from journals;jq tep5_development < ../journals_latest.sqlSETSETSETSETSET count set_config ------------ 14709(1 row)</nowiki>
SETSETSETSETSETSETERROR: % rails generate scaffold Journal userrelation "journals" already existsALTER TABLEERROR: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 relation "journals_id_seq" already existsALTER TABLE % rails db:migrateALTER SEQUENCEALTER TABLEリストアして作業データベースにコピーするCOPY 14885 <nowiki>% pg_dump setval ----username=jq --table journals heroku_dump > ../new_journals.sql% psql --username=jq update_heroku < ../new_journals.sql</nowiki> 15199(1 row)
テーブルの変更ERROR: multiple primary keys for table "journals" are not allowedERROR: relation "index_journals_on_account_id" already existsERROR: relation "index_journals_on_credit_and_credit_money_and_account_id" already existsERROR: =# ALTER TABLE journals RENAME COLUMN jiin_id TO note_id;relation "index_journals_on_credit_s" already existsERROR: relation "index_journals_on_debit_and_client_id_and_debit_money" already existsERROR: relation "index_journals_on_debit_s" already existsERROR: relation "index_journals_on_description_id_and_schedule_id" already existsERROR: relation "index_journals_on_jiin_id" already existsERROR: relation "index_journals_on_user_id" already existsERROR: relation "index_journals_on_user_id_and_jiin_id_and_journal_number" already exists</nowiki>
ここまでのバックアップを取る % pg_dump -Fc tep5_development > ../202212170834.dump ==== 以前 ====確認 <nowiki>=# select count(*) from journals; count ------- 14709(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  % rails db:migrate リストアして作業データベースにコピーする <nowiki>% pg_dump --username=jq --table journals heroku_dump > ../new_journals.sql% psql --username=jq update_heroku < ../new_journals.sql</nowiki> テーブルの変更 =# ALTER TABLE journals RENAME COLUMN jiin_id TO note_id; 新テーブルのclient_idに合わせる <nowiki>update_heroku=# update journals SET client_id = client_id + 20000 where note_id = 2;UPDATE 3663update_heroku=# update journals SET client_id = client_id + 30000 where note_id = 3;UPDATE 1815update_heroku=# update journals SET client_id = client_id + 40000 where note_id = 4;UPDATE 719</nowiki>
リストア コピー
=== 戒名テーブル 変更あり ===
==== 12-17 11:14 ====
確認
<nowiki>latest_dump=# select count(*) from kaimyous; count ------- 3445</nowiki> 増分を書き出す % 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  ie_id(client_id)は修正する ie_id_vはmemoに対応するために書き出した コピーで追加 <nowiki>=# \COPY kaimyous FROM '../kaimyous_add.csv' DELIMITER ',' CSV HEADER;COPY 70</nowiki> ==== 以前 ====確認 <nowiki>heroku_dump=# select count(*) from kaimyous;
count
-------
バックアップを取る -Fc カスタム形式 圧縮ファイル形式
% pg_dump -Fc tep5_development > ../202212141027.dump
 
== house_note ==
旧データベースからダンプ
% pg_dump --username=jq --table house_notes latest_dump > ../hns.sql
 
サーバーへ転送
% rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/hns.sql jq:/home/jq/dump_data
 
リストア
$ psql update_dump < ../hns.sql
 
clientsのidとhouse_noteのidをあわせる
<nowiki>tep5_production=# update house_notes SET ie_id = ie_id + 20000 where jiin_id = 2;
UPDATE 30
tep5_production=# update house_notes SET ie_id = ie_id + 30000 where jiin_id = 3;
UPDATE 114
tep5_production=# update house_notes SET ie_id = ie_id + 40000 where jiin_id = 4;
UPDATE 25</nowiki>
 
セレクトしてアップデートする
tep5_production=# UPDATE clients as Cl SET memo = (SELECT content FROM house_notes as Hn WHERE Hn.ie_id = Cl.id);
 
削除
=# drop table house_notes ;
== UUIDを有効にする ==
$ rails generate scaffold Post content:text user:references picture:string
 
$ rails generate scaffold Post user:references title:string content:text notification_time:datetime reminder_type:integer post_type:integer picture:string
$ rails generate model Relationship follower_id:integer followed_id:integer
config/envitonments/production.rb
config.assets.compile = true
 
=== カラム追加 ===
<nowiki>% rails generate migration add_memo_to_schedules memo:text
invoke active_record
create db/migrate/20230311013341_add_memo_to_schedules.rb
% rails db:migrate
== 20230311013341 AddMemoToSchedules: migrating ===============================
-- add_column(:schedules, :memo, :text)
-> 0.0056s
== 20230311013341 AddMemoToSchedules: migrated (0.0057s) ======================</nowiki>
 
=== 外部キー制約に変更 ===
rails generate migration AddScheduleReferenceToJournal schedule:references
== サーバーへ転送 ==
database.yml</nowiki>
すべて
% rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/ jq:/home/jq/tep5/
appのみ % rsync -r --delete --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/app/ jq:/home/jq/tep5/app/ データベース % rsync ~/rails_app_tepnote/202210031034_tep5202212170834.sql dump jq:/home/jq/dump_data
== サーバー側 ==
リストア
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U jq -d tep5_ production ../dump_data/202212141027.dump
 
$ bundle install --without test development
データベース削除
~$ dropdb tep5_production
または
postgres=# DROP DATABASE tep5_production;
$ 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