Ruby-on-rails – asked to run ‘rake db:migrate RAILS_ENV=test’

rails-migrationsruby-on-railsruby-on-rails-4

On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec:

/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in
`check_pending!': Migrations are pending; run 'rake db:migrate
RAILS_ENV=test' to resolve this issue.
(ActiveRecord::PendingMigrationError)

That doesn't seem right. No one migrates their test database, do they? They db:test:prepare it, which—to be fair—I've forgotten to do. So I run rake db:test:prepare and run my rspec command again…and see the same error.

If I actually rake db:migrate RAILS_ENV=test, the error does in fact go away.

What's going on? Is this new in Rails 4?

Best Answer

As of Rails 4.1, the rake db:test:* tasks are deprecated. Instead, your (test|spec)_helper.rb should include:

ActiveRecord::Migration.maintain_test_schema!

This means that your test database will get the correct schema every time your tests run, whether you run them from a Rake task or not.

Related Topic