Tep3-onk

目次

generate編集

$ rails generate scaffold User name:string email:string password_digest:string remember_digest:string admin:boolean activation_digest:string activated:boolean activated_at:datetime reset_digest:string reset_sent_at:datetime
$ 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 scaffold Note content:string user:references kind:integer meeting_time:integer role:integer line:integer
% rails generate scaffold Client user:references note:references content:string list:integer line:integer memo:text
% 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 generate model Relationship follower_id:integer followed_id:integer
$ rails db:migrate RAILS_ENV=production
$ bin/dev
$ rails generate controller StaticPages home help about contact

エラー

ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.

config/envitonments/production.rb

config.assets.compile = true

db:rollback編集

% rails db:migrate:status
% rails db:rollback STEP=3
% rails db:migrate
$ rails db:migrate:down VERSION=20200504051127
$ rails db:migrate:up VERSION=20200504051127
% rails db:migrate:reset

postgres編集

$ heroku pg:backups:capture
$ heroku pg:backups:download
$ createdb mydb
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
=# select count(*) from clients;
 count 
-------
  4209
(1 row)

=# select distinct on (jiin_id) * from clients;

テーブルをコピーしてデータを加工する

=# CREATE TABLE jq_clients AS SELECT * FROM clients where jiin_id = 1;
SELECT 3066
=# ALTER TABLE jq_clients ADD COLUMN userid uuid;
=# update jq_clients SET userid = '44f47a1e-2ccf-4213-bac5-aa62c5f3c141';
UPDATE 3066

=# ALTER TABLE jq_clients ADD COLUMN jiinid uuid;
=# update jq_clients SET jiinid = 'd23ae9b5-5a74-4e95-9933-0e710b13401a';
UPDATE 3066
=# update jq_clients SET ie_id = null;
UPDATE 3066
=# COPY (SELECT userid,jiinid,ie_id,name,yomi,nusi,each_order,memo,generation,created_at,updated_at FROM jq_clients WHERE jiin_id = 1) TO '/Users/jq/rails_app_tepnote/jq_clients.csv' WITH CSV HEADER;
COPY 3066

database heroku_dump table dump編集

ALTER TABLE users RENAME TO users_old;
 ALTER TABLE clients RENAME TO clients_old;
=# select count(*) from clients_old ;
 count 
-------
  5067
(1 row)
% pg_dump --username=jq --table clients_old heroku_dump > clients_old.sql
% psql --username=jq tep5_development < clients_old.sql     
SET
SET
SET
SET
SET
 set_config 
------------
 
(1 row)

SET
SET
SET
SET
SET
SET
ERROR:  relation "clients_old" already exists
ALTER TABLE
ERROR:  relation "clients_id_seq" already exists
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
COPY 5067
 setval 
--------
   5351
(1 row)

ERROR:  relation "clients_pkey" already exists
ERROR:  relation "index_clients_on_jiin_id" already exists
ERROR:  relation "index_clients_on_user_id" already exists
ERROR:  relation "index_clients_on_user_id_and_jiin_id_and_name" already exists
ERROR:  relation "index_clients_on_yomi_and_ie_id" already exists
ERROR:  there is no unique constraint matching given keys for referenced table "users_old"
ERROR:  constraint "fk_rails_de6906508b" for relation "clients_old" already exists

ie_id 重複修正編集

重複チェック

=# SELECT ie_id, COUNT(ie_id)
FROM clients_old
where nusi = true GROUP BY ie_id
HAVING COUNT(ie_id) > 1
ORDER BY ie_id
;
 ie_id | count 
-------+-------
  2090 |     2
  2129 |     2
  2167 |     2

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編集

=# insert into users
(
    id, name, email, created_at, updated_at, password_digest, remember_digest, activation_digest, activated, activated_at, reset_digest, reset_sent_at
)
select
    id, name, email, created_at, updated_at, password_digest, remember_digest, activation_digest, activated, activated_at, reset_digest, reset_sent_at
from
    users_old;
INSERT 0 17
=# insert into notes
(
    id, content, user_id, created_at, updated_at, kind              
)
select
    id, jiin_name, user_id, created_at, updated_at, kind              
from
    jiins;
INSERT 0 14

=# insert into clients
(
    id, user_id, note_id, content, line, created_at, updated_at
)
select
    ie_id, user_id, jiin_id, name, table_order,  created_at, updated_at
from
    clients_old
where
    nusi = true;
INSERT 0 1293

=# 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

insert エラー編集

% rails db:migrate:status
% rails db:rollback STEP=2
% rails db:migrate:down VERSION=
t.references :client, null: false, foreign_key: true  -->  t.references :client, foreign_key: true
% rails db:migrate:up VERSION=

後で本番環境も変更

不要

=# delete from clients_old where id in (4028,3911,3912,2998,3007,4359);

主重複

 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;

移動本番でも編集

update clients_old set nusi = true where id = 523;
delete from clients_old where id = 845;
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;
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;
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

指定行を作る編集

=# INSERT INTO others (user_id, note_id, client_id, created_at, updated_at)
 SELECT 
     '44f47a1e-2ccf-4213-bac5-aa62c5f3c141', 'd23ae9b5-5a74-4e95-9933-0e710b13401a', '6d7d3f62-ebc8-4de1-bc46-06287a19d8d1', '2022-09-12 19:11:46.986872', '2022-09-12 19:11:46.986872'
 FROM                         
     generate_series(1,3066) ;
INSERT 0 3066

update編集

=# UPDATE others set 
name = atab.name, yomi = atab.yomi, client_name = atab.nusi, line = atab.each_order, memo = atab.memo, generation = atab.generation, created_at = atab.created_at, updated_at = atab.updated_at
FROM (SELECT name, yomi, nusi, each_order, memo, generation, created_at, updated_at FROM jq_clients) atab
;
UPDATE 3066

作業テーブル削除編集

tep5_development=# drop table clients_old ;
DROP TABLE
tep5_development=# drop table jq_clients ;
DROP TABLE
tep5_development=# drop table users_old ;
DROP TABLE

ローカル編集

rubyの確認編集

プロジェクトディレクトリに移動

% cd rails_app_tepnote/3tep

カレントディレクトリでrubyのバージョン確認

% rbenv version            
2.7.1 (set by /Users/jq/.rbenv/version)

使用できるrubyのバージョン確認

% rbenv versions
  system
  2.6.6
* 2.7.1 (set by /Users/jq/.rbenv/version)
  2.7.4
  2.7.5
  3.0.0
  3.1.2

カレントディレクトリで使う rubyのバージョンを指定する

% rbenv local 3.1.2

確認 2.7.1から3.1.2に変わった

% rbenv version    
3.1.2 (set by /Users/jq/rails_app_tepnote/3tep/.ruby-version)

% rbenv versions
  system
  2.6.6
  2.7.1
  2.7.4
  2.7.5
  3.0.0
* 3.1.2 (set by /Users/jq/rails_app_tepnote/3tep/.ruby-version)

railsの確認編集

カレントディレクトリで確認 無かったのでインストールする

% rails -v
rbenv: rails: command not found
% gem install rails
% rails -v         
Rails 7.0.3.1

bundlerの確認編集

% gem search ^bundler$ --all
bundler (2.3.21, 2.3.20, 2.3.19, 2.3.18, 2.3.17,.......
% gem list bundler

*** LOCAL GEMS ***

bundler (2.2.17, default: 2.1.4)

バージョンを指定してインストール

% gem install bundler -v 2.3.21

確認

% gem list bundler             

*** LOCAL GEMS ***

bundler (2.3.21, 2.2.17, default: 2.1.4
% bundle -v
Bundler version 2.3.21

postgresqlの確認編集

% psql --version
psql (PostgreSQL) 14.5 (Homebrew)

new編集

% rails new tep30 -d postgresql --css tailwind または rails new tep30 -d postgresql --css bootstrap
% cd tep30
% bundle install
% rails db:create
% rails db:migrate
% rails s

daisyui編集

tailwind css UIコンポーネント インストール

% yarn add daisyui

コピー編集

% scp -r --exclude-from='exclude-file.txt' ~/rails_app_tepnote/3tep/tep30 xxx:/home/xxx

ホスト編集

確認編集

$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
$ rails -v
Rails 7.0.3.1
$ bundle -v
Bundler version 2.3.21
$ psql --version

new編集

$ rails new tep30 -d postgresql --css bootstrap または rails new tep30 --css tailwind -d postgresql

database.yml username:などを確認する (2箇所)

$ bundle install --without test development
$ rails db:create RAILS_ENV=production
$ rails db:migrate RAILS_ENV=production

daisyuiをインストール

$ yarn add daisyui

config/tailwind.config.js

plugins: [
    require('@tailwindcss/forms'),
    require('@tailwindcss/aspect-ratio'),
    require('@tailwindcss/typography'),
    require('daisyui'),    // ← 追記する
  ],

app/views/layouts/application.html.erb

<html data-theme="light">  // テーマを選ぶ


同期編集

除外するファイルをまとめる

exclude-file.txt
    production.rb
    database.yml
% rsync -r --exclude-from='exclude-file.txt' ~/rails_app_tepnote/tep5/ jq:/home/jq/tep5/


$ bundle install  --without test development

database.yml username:などを確認する (2箇所)

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;

エラーのとき

ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`

master.key credentials.yml.enc 削除して作り直す

$ EDITOR="vi" bin/rails credentials:edit
$ rails db:migrate RAILS_ENV=production

エラー

ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.

config/envitonments/production.rb

config.assets.compile = true

daisyuiをインストール

$ yarn add daisyui

参考編集

=# delete from clients_old where ie_id in (21,
22,
23,
24,
26,
27,
30,
32,
33,
34,
35,
36,
39,
41,
43,
44,
45,
46,
48,
49,
51,
52,
53,
56,
57,
62,
63,
64,
66,
67,
68,
69,
71,
73,
78,
79,
80,
82,
87,
90,
92,
93,
94,
95,
96,
97,
98,
99,
100,
101,
102,
104,
105,
106,
108,
109,
110,
111,
113,
115,
116,
117,
118,
121,
122,
123,
124,
126,
127,
128,
129,
131,
132,
133,
135,
136,
138,
139,
140,
141,
144,
145,
147,
148,
150,
151,
152,
153,
157,
158,
159,
161,
162,
166,
170,
171,
173,
174,
176,
177,
179,
180,
182,
183,
184,
185,
186,
188,
189,
190,
191,
192,
194,
197,
199,
201,
202,
205,
222,
228,
230,
231,
232,
249,
264,
272,
310,
355,
372,
373,
375,
404,
461,
472,
473,
490,
491,
531,
550,
561,
590,
649,
673,
674,
676,
679,
683,
686,
689,
691,
694,
701,
741,
754,
800,
802,
804,
805,
808,
814,
815,
821,
848,
872,
875,
876,
921,
923,
936,
953,
972,
993,
998,
1009,
1010,
1013,
2072,
5158,
6053)
DELETE 307