Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 912
Peeja Avatar asked Jun 17 '13 14:06

Peeja


People also ask

What does rake test do?

Basically it handles cloning the database so you don't have to run the migrations against test to update the test database. rake db:test:prepare is now deprecated. above url is dead, this would be the new one github.com/rails/rails/blob/4-1-stable/activerecord/…

What does db Migrate do?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

Why do we need migration in Rails?

Migrations are a convenient way to alter your database schema over time in a consistent way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database.


1 Answers

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.

like image 185
Peeja Avatar answered Sep 21 '22 16:09

Peeja