24

I appear to have a circular issue in regards to Ruby on Rails migration procedure. I am following the introduction article and I have reached the point when I need to create my first table.

I have ran the following,

[tims@web2 working_ror]# rails generate model Homepage first_name:string  last_name:string email:string message:text
  invoke  active_record
  create    db/migrate/20131119203948_create_homepages.rb
  create    app/models/homepage.rb
  invoke    test_unit
  createtest    /models/homepage_test.rb
  createtest    /fixtures/homepages.yml

I then proceeded with the migration,

[tims@web2 working_ror]# rake db:migrate
==  CreateHomepages: migrating ================================================
-- create_table(:homepages)
   -> 0.0493s
==  CreateHomepages: migrated (0.0494s) =======================================

, however, when I run my application I see the following message,

Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.

but, IF I run the above,

[tims@web2 working_ror]# rake db:migrate RAILS_ENV=development
[tims@web2 working_ror]# 

and the message continues ...

I have spent considerable amount of time researching forums in-which the closest I could find was to drop and re-build everything, which have done the following.

rake db:drop rake db:create rake db:migrate

and the results are the same.

user3010587
  • 301
  • 1
  • 3
  • 8
  • What's the output of `rails -v` and `cat Gemfile | grep rails`? – Eric Andres Nov 19 '13 at 21:07
  • 6
    Just to be sure: Do you restart the app after run migrations? – Aguardientico Nov 19 '13 at 21:08
  • What's the output of rails -v, Rails 4.0.1 and cat Gemfile | grep rails?, # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.1' gem 'sass-rails', '~> 4.0.0' gem 'coffee-rails', '~> 4.0.0' gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # bundle exec rake doc:rails generates the API under doc/api. – user3010587 Nov 19 '13 at 21:15
  • My understanding of how the development server would not require a restart - was wrong. – user3010587 Nov 19 '13 at 21:21
  • I solved this for a quick one , see my answer in the following : http://stackoverflow.com/a/33054787/4902373 – Haider Raza Oct 10 '15 at 14:04

6 Answers6

30

You need to do

bundle exec rake test:prepare 

or

bundle exec rake db:test:prepare

and then

bundle exec rake db:migrate

before running the specs

Cheers

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

Community
  • 1
  • 1
igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
  • Run into this issue when deploying to a CI server, which used `db:test:prepare`. Using `test:prepare` did the job. Not sure why. On OS X both variations worked, on linux (at the CI server), only `test:prepare`. Both with rails 5 and ruby 2.3.1. – Kjell Nov 07 '16 at 15:39
  • I had to purge the test db first for it to work db:test:purge – gmm Jul 04 '18 at 16:43
9

you can do

bundle exec rake test:prepare 

In Rails 4.1+, they deprecated db:test:prepare You can now just use:

ActiveRecord::Migration.maintain_test_schema!

If you need to do it manually

rake db:schema:load RAILS_ENV=test

and then

bundle exec rake db:migrate
Ahmed Ali
  • 671
  • 7
  • 10
5

try In RAILS_ROOT/config/environments/development.rb Set the following setting to false:

config.active_record.migration_error = false#:page_load

1

One weird trick that you can use when your migrations are screwed (file deleted, manually renamed, etc.)

  1. Fire up your favourite DB admin tool (eg. PGAdmin3) and browse to the database in question.
  2. Look for a table called schema_migrations and browse its content. It should have a single column called version. This field is used by Rails to check whether migrations are up to date.
  3. Make sure that your migration timestamps corresponds with the data in this column. If you have deleted an older migration, delete the corresponding timestamp.
fylooi
  • 3,840
  • 14
  • 24
0

Check to make sure that table doesn't already exist:

  1. type - rails dbconsole
  2. type - .tables (check to see if there was an error during the rake db:migrate that has the table name like -- create_table(:test) rake aborted!)
  3. If you see the table name after running the .tables in the console type - drop table TABLENAME;
  4. Then .quit to go back to the branch and run the rake db:migrate command again.
Omal Perera
  • 2,971
  • 3
  • 21
  • 26
-1

this was what i did:

rails db:environment:set RAILS_ENV=test

If you need to do it manually

rake db:schema:load RAILS_ENV=test

and then

bundle exec rake db:migrate

Thanks to Ahmed Ali....... your comment was helpful.

aUXcoder
  • 1,048
  • 1
  • 20
  • 32