3,728
回編集
差分
Tep3-onk
,→db:rollback
[[category:memo|{{PAGENAME}}]]
{{Font color}}
== generate ==
% rails generate scaffold Other user:references note:references client:references name:string yomi:string client_name:boolean line:integer memo:text generation:string
% rails generate scaffold List user:references note:references content:string line:integer
$ rails generate scaffold Post content:text user:references picture:string
$ rails db:migrate:up VERSION=20200504051127
% rails db:migrate:reset
== postgres ==
COPY 3066</nowiki>
=== database heroku_dump table dump ===
ALTER TABLE users RENAME TO users_old;
ALTER TABLE clients RENAME TO clients_old;
<nowiki>=# select count(*) from clients_old ;
count
-------
5067
(1 row)</nowiki>
% pg_dump --username=jq --table jq_clients clients_old heroku_dump > old_otherclients_old.sql
<nowiki>% psql --username=jq tep5_development < old_otherclients_old.sql
SET
SET
SET
SET
ALTER TABLE
ALTER SEQUENCEALTER TABLECOPY 30665067 setval -------- 5351(1 row) ERROR: relation "clients_pkey" already existsERROR: relation "index_clients_on_jiin_id" already existsERROR: relation "index_clients_on_user_id" already existsERROR: relation "index_clients_on_user_id_and_jiin_id_and_name" already existsERROR: relation "index_clients_on_yomi_and_ie_id" already existsERROR: there is no unique constraint matching given keys for referenced table "users_old"ERROR: constraint "fk_rails_de6906508b" for relation "clients_old" already exists</nowiki>
=== ie_id 重複修正 ===
2167 | 2
</nowiki>
ie_idを変更する
=# update clients_old SET ie_id = ie_id + 20000 where jiin_id = 2;
------
=# update clients_old SET ie_id = ie_id + 40000 where jiin_id = 4;
=# select * from clients_old where ie_id = 2090;
=# update clients_old set ie_id = 6000 where ie_id = 2090 and jiin_id > 1;
== 親テーブルie_id 子テーブルie_id 差分削除 ==
% diff aa_clients_id_ie_id.csv bb_clients_old_ie_id.csv > diff.csv
=== insert ===
id, name, yomi, ie_id, nusi, user_id, jiin_id, created_at, updated_at, memo, each_order, generation
from
clients_old;INSERT 0 4747</nowiki>
=== insert エラー ===
% rails db:migrate:up VERSION=
{{font color||yellow|後で本番環境も変更}}
不要
=# delete from clients_old where id in (4028,3911,3912,2998,3007,4359);
主重複
<nowiki> ie_id | count
-------+-------
2282 | 2
3703 | 2
(2 rows)
=# update clients_old set nusi = false where id = 1346;
=# update clients_old set nusi = false where id = 3703;</nowiki>
== {{font color||yellow|移動本番でも}} ==
update clients_old set nusi = true where id = 523;
delete from clients_old where id = 845;
<nowiki>update clients_old set ie_id = 1264 where id = 515;
update clients_old set ie_id = 1264 where id = 1463;
update clients_old set nusi = false where id = 515;</nowiki>
delete from clients_old where id = 217;
update clients_old set nusi = false where id = 218;
update clients_old set ie_id = 204 where id = 218;
delete from clients_old where id = 811;
update clients_old set nusi = true where id = 1792;
delete from clients_old where id = 810;
delete from clients_old where ie_id = 22653;
<nowiki>delete from clients_old where ie_id = 22653;
DELETE 3
tep5_development=# insert into others
(
id, name, yomi, client_id, client_name, user_id, note_id, created_at, updated_at, memo, line, generation
)
select
id, name, yomi, ie_id, nusi, user_id, jiin_id, created_at, updated_at, memo, each_order, generation
from
clients_old;
INSERT 0 4747</nowiki>
=== 指定行を作る ===
;
UPDATE 3066</nowiki>
=== 作業テーブル削除 ===
<nowiki>tep5_development=# drop table clients_old ;
DROP TABLE
tep5_development=# drop table jq_clients ;
DROP TABLE
tep5_development=# drop table users_old ;
DROP TABLE</nowiki>
== ローカル ==
== 同期 ==
除外するファイルをまとめる
<nowiki>exclude-file.txt
$ bundle install --without test development
{{font color||yellow|database.yml username:などを確認する (2箇所)}} {{font color||yellow|migrationファイルがダブったときは削除}} $ rails db:migrate:status RAILS_ENV=production
$ rails db:create RAILS_ENV=production
または
$ rails db:reset RAILS_ENV=production
または
$ rails db:drop RAILS_ENV=production
または
=# drop database mydb;
エラーのとき
== 参考 ==
<nowiki>=# delete from clients_old where id ie_id in (97721,
22,
23,
27,
30,
34,
35,
41,
43,
45,
48,
49,
52,
53,
62,
64,
66,
68,
69,
79,
80,
97,
98,
100,
104,