Why am I asked to run 'rake db:migrate RAILS_ENV=test'?

Ruby on-RailsRails MigrationsRuby on-Rails-4

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

Looks like rake test:prepare works, not sure what db:test:prepare now does.

Solution 3 - Ruby on-Rails

You can also try

rake db:migrate RAILS_ENV=test

which works as

db:test:prepare

does:)

Solution 4 - Ruby on-Rails

I still have trouble sometimes in sorting this problem out when I just follow one person's answer so I have thrown a couple together to get better results. Here are the steps I take, not sure which ones are unnecessary, but it works in the end.

  1. add ActiveRecord::Migration.maintain_test_schema! to the top of the test_helper.rb file.
  2. rake test:prepare
  3. rake db:migrate
  4. rake db:migrate RAILS_ENV=test

Then when I run bundle exec rake test I get clean results every time with no pending migrations. (This is what I do right after generating the scaffold the first time). Someone feel free to correct me if you know for sure that one of these steps is absolutely not necessary, but this is how I make sure it works every time.

Solution 5 - Ruby on-Rails

I've found I have this problem when using chruby to manage my ruby versions. Rails calls bin/rails db:test:prepare via the system command. This doesn't take advantage of chrubys $PATH env var, so it runs as whatever the system ruby is, and fails because of missing gems typically. Unfortunately, I don't currently have a good solution for this.

Solution 6 - Ruby on-Rails

You can try to set variable BEFORE command, like this. This statement solved my problem:

RAILS_ENV=test rake db:migrate

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeejaView Question on Stackoverflow
Solution 1 - Ruby on-RailsPeejaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsKrisView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmeejoeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSilasOtokoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJesse BrownView Answer on Stackoverflow
Solution 6 - Ruby on-RailsUnkasView Answer on Stackoverflow