Tep3-onk

2022年9月15日 (木) 07:04時点におけるShudo (トーク | 投稿記録)による版 (table dump)

目次

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

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 < old_other.sql
SET
SET
SET
SET
SET
 set_config 
------------
 
(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
COPY 3066

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

=# select * from clients_old where ie_id = 2090;
=# update clients_old set ie_id = 6000 where ie_id = 2090 and jiin_id > 1;

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 エラー

% 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);

指定行を作る

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

ローカル

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">  // テーマを選ぶ


同期

% rsync -r ~/rails_app_tepnote/4tep/ host:/home/tep/

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

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箇所)

$ rails db:create RAILS_ENV=production
または
$ rails db:reset RAILS_ENV=production

エラーのとき

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 id in (977,
22,
23,
24,
26,
27,
30,
533,
266,
34,
35,
36,
39,
41,
43,
459,
45,
989,
48,
49,
51,
52,
53,
990,
697,
62,
297,
64,
66,
67,
68,
69,
978,
704,
793,
79,
80,
82,
87,
90,
92,
93,
94,
95,
96,
97,
98,
517,
100,
974,
269,
104,
1793)