読者です 読者をやめる 読者になる 読者になる

Rails チュートリアルでrails testした時に、「ActiveRecord::PendingMigrationError」が発生した時の対処

Railsチュートリアルをやっていてちょっとはまったところがあったので、同じことで困っている人のためにメモ。

【発生箇所】
Rails チュートリアルの第6章 6.3.1 ハッシュ化されたパスワード付近。

【現象】
6.3.1 ハッシュ化されたパスワードで、最初にusersテーブルにpassword_digestカラムを
追加するために、マイグレーションした後で、rails testコマンドを実行すると、以下のエラーが出る。

[vagrant@localhost sample_app]$ rails test
Running via Spring preloader in process 13972
/home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:573:in `check_pending!': (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

bin/rails db:migrate RAILS_ENV=test

from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:586:in `load_schema_if_pending!'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:592:in `block in maintain_test_schema!'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:823:in `suppress_messages'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:597:in `method_missing'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:592:in `maintain_test_schema!'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_help.rb:15:in `'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/rails_tutorial_since_20170315/sample_app/test/test_helper.rb:3:in `'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/rails_tutorial_since_20170315/sample_app/test/controllers/static_pages_controller_test.rb:1:in `'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_unit/test_requirer.rb:10:in `each'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_unit/minitest_plugin.rb:86:in `plugin_rails_init'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest.rb:80:in `block in init_plugins'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest.rb:78:in `each'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest.rb:78:in `init_plugins'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest.rb:129:in `run'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/test_unit/minitest_plugin.rb:73:in `run'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/gems/2.3.0/gems/minitest-5.10.1/lib/minitest.rb:62:in `block in autorun'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/vagrant/.rbenv/versions/2.3.2/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `

'


【対処法】
まず、データベースをリセットするために以下のコマンドを実行。

[vagrant@localhost sample_app]$ rails db:migrate:reset
Dropped database 'db/development.sqlite3'
Dropped database 'db/test.sqlite3'
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'
== 20170403115448 CreateUsers: migrating ======================================

    • create_table(:users)

-> 0.0036s
== 20170403115448 CreateUsers: migrated (0.0037s) =============================

== 20170408025509 AddIndexToUsersEmail: migrating =============================

    • add_index(:users, :email, {:unique=>true})

-> 0.0039s
== 20170408025509 AddIndexToUsersEmail: migrated (0.0040s) ====================

== 20170408091417 AddPasswordDigestToUsers: migrating =========================

    • add_column(:users, :password_digest, :string)

-> 0.0023s
== 20170408091417 AddPasswordDigestToUsers: migrated (0.0023s) ================


次に、先ほどのエラーメッセージに表示されていた以下のコマンドを実行。

[vagrant@localhost sample_app]$ bin/rails db:migrate RAILS_ENV=test
== 20170403115448 CreateUsers: migrating ======================================

    • create_table(:users)

-> 0.0023s
== 20170403115448 CreateUsers: migrated (0.0024s) =============================

== 20170408025509 AddIndexToUsersEmail: migrating =============================

    • add_index(:users, :email, {:unique=>true})

-> 0.0021s
== 20170408025509 AddIndexToUsersEmail: migrated (0.0025s) ====================

== 20170408091417 AddPasswordDigestToUsers: migrating =========================

    • add_column(:users, :password_digest, :string)

-> 0.0018s
== 20170408091417 AddPasswordDigestToUsers: migrated (0.0019s) ================

こうすると、rails testが実行できるようになる。

以上