Bastien Vandamme
Bastien Vandamme

Reputation: 18485

Why Rails ask me to run rake db:migrate RAILS_ENV=test again and again?

Could you help me with this issue:

When executing

root@myproject-dev:~/projects/myproject-dev$ rake db:migrate:status

database: myproject-db

 Status   Migration ID    Migration Name
--------------------------------------------------
   (...)
   up     20151104094107  Add need to something
   up     20151111082356  ********** NO FILE **********
   up     20151113024714  Add approved something
   up     20151116075224  Add something
   up     20151130041605  Change column name

root@myproject-dev:~/projects/myproject-dev$ bundle exec rspec

RSpec is shutting down and will print the summary report... Interrupt again to force quit.
/home/xxx/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:389:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

        bin/rake db:migrate RAILS_ENV=test

Status show me everything is up and the rake db:migrate RAILS_ENV=test has already been performed correctly. If I execute it again I get errors (of course).

Why Rails ask me to run rake db:migrate RAILS_ENV=test again and again ?

Upvotes: 1

Views: 6045

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

There are 3+ default Rails environments:

  • development
  • production
  • test

Each of them is supposed to have different settings, including different database to operate on. This is done to separate environments and prevent accidental data corruption/damage when running in inappropriate environment.

E. g. in test environment, every execution of rake test clears the database to prevent side effects. rspec automatically sets test environment for you.

So, in your case, you should migrate your test database:

RAILS_ENV=test rake db:migrate:status
RAILS_ENV=test rake db:migrate

Hope it helps.

Upvotes: 3

Related Questions