
環境
- ruby 2.4.1
- Rails 5.1.4
背景
Rspecでテストしてたら、下記エラーが出た。
Failure/Error: @category_tag1 = create(:category_tag, name: 'パズル')
ActiveRecord::RecordInvalid:
Validation failed: Name has already been taken
テスト前に下記を実行してるのに、なぜデータがあるんだ?と気になったので調べてみた。
bundle exec rake db:reset --trace bundle exec rake db:migrate
結論
db:resetはdb:seedも実行してくれるdb:migrate:resetの場合はdb:seedは実行されない
db:reset と db:migrate:reset の違い
| コマンド | db:reset | db:migrate:reset |
|---|---|---|
| db:drop | ✔️ | ✔️ |
| db:create | ✔️ | ✔️ |
| db:migrate | ✔️ | |
| db:schema:load | ✔️ | |
| db:seed | ✔️ |
db:migrate と db:schema:load の違い
> bundle exec rake -T | grep db:migrate rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) > bundle exec rake -T | grep db:schema:load rake db:schema:load # Loads a schema.rb file into the database
なるほど。よくわからん。
まーリファレンスとかみる限り
| コマンド | 内容 |
|---|---|
| db:migrate | 1. マイグレーションファイルから schema.rb を作成2. schema.rb からテーブルを作成 |
| db:schema:load | 1. schema.rb からテーブルを作成 |
って感じっぽいですね。
以下詳細
db:reset
> bundle exec rake db:reset RAILS_ENV=test --trace
** Invoke db:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:check_protected_environments
** Execute db:drop
** Invoke db:drop:_unsafe (first_time)
** Invoke db:load_config
** Execute db:drop:_unsafe
Dropped database '/Users/naichi/unity-room/db/test.sqlite3'
** Invoke db:setup (first_time)
** Invoke db:schema:load_if_ruby (first_time)
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
Created database '/Users/naichi/unity-room/db/test.sqlite3'
** Invoke environment
** Execute db:schema:load_if_ruby
** Invoke db:schema:load (first_time)
** Invoke environment
** Invoke db:load_config
** Invoke db:check_protected_environments
** Execute db:schema:load
-- create_table("assets", {:force=>:cascade})
-> 0.0062s
-- create_table("comments", {:force=>:cascade})
-> 0.0019s
〜〜〜〜 ずらずらとcreate_table 〜〜〜〜
** Invoke db:structure:load_if_sql (first_time)
** Invoke db:create
** Invoke environment
** Execute db:structure:load_if_sql
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:abort_if_pending_migrations
** Execute db:setup
** Execute db:reset
db:seed実行されてますね!
db:migrate:reset
ついでに似たコマンドのこちらも確認
> bundle exec rake db:migrate:reset RAILS_ENV=test --trace
** Invoke db:migrate:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config
** Execute db:check_protected_environments
** Execute db:drop
** Invoke db:drop:_unsafe (first_time)
** Invoke db:load_config
** Execute db:drop:_unsafe
Dropped database '/Users/naichi/unity-room/db/test.sqlite3'
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
Created database '/Users/naichi/unity-room/db/test.sqlite3'
** Invoke db:migrate (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:migrate
== 20151028162154 CreateUsers: migrating ======================================
-- adapter_name()
-> 0.0001s
-- adapter_name()
-> 0.0000s
-- create_table(:users, {:id=>:integer})
-> 0.0025s
== 20151028162154 CreateUsers: migrated (0.0031s) =============================
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
** Execute db:migrate:reset