db:migrate error on test after Rails 4.2 update

657 views
Skip to first unread message

Sean Kelley

unread,
Jan 29, 2015, 11:51:35 AM1/29/15
to rubyonra...@googlegroups.com
I updated to rails 4.2 from 4.1 recently.  Not sure if that fact is related to my problem or not.  I have not run any tests since before update.  Now attempting to run a controller test give me the error:
'migrations are pending' with recommendation to run: bin/rake db:migrate RAILS_ENV=test

when I run migrate on test I get error:
PGDuplicateTable relation 'users' already exists

If I run migration on production I do not get error.  If I run the following:
bundle exec rake db:migrate:reset RAILS_ENV=test
I can then run the test once, but trying to re-run gives me the migration pending error again.  

Incidentally, my first project migration in migrations directory creates users table:
class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t| 
       ...

This is my first rails app so I am wondering what is causing this and how to resolve.  Any ideas?

background: I am developing on windows 7 with rspec testing and Postgres database if any of that matters.

Antônio Augusto Sousa Britto

unread,
Jan 30, 2015, 6:42:19 AM1/30/15
to rubyonra...@googlegroups.com
try this: bundle exec rake db:drop db:create db:migrate db:test:prepare

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c1324137-ecb6-4a92-802f-833ac0d32528%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sean Kelley

unread,
Jan 30, 2015, 9:17:07 AM1/30/15
to rubyonra...@googlegroups.com
Thanks. Your command line affects development db also which is not ideal.  I looked up db:test:prepare and it appears to be deprecated in 4.2
http://stackoverflow.com/questions/15169894/what-does-rake-dbtestprepare-actually-do

I decided to try it anyway with RAILS_ENV=test but I get same error of duplicate table 'users' so I tried without the test env set and I get db connection is closed message for test. 

Colin Law

unread,
Jan 30, 2015, 9:41:26 AM1/30/15
to rubyonra...@googlegroups.com
On 30 January 2015 at 14:17, Sean Kelley <kelle...@gmail.com> wrote:
>
> Thanks. Your command line affects development db also which is not ideal. I looked up db:test:prepare and it appears to be deprecated in 4.2
> http://stackoverflow.com/questions/15169894/what-does-rake-dbtestprepare-actually-do
>
> I decided to try it anyway with RAILS_ENV=test but I get same error of duplicate table 'users' so I tried without the test env set and I get db connection is closed message for test.

I presume your development db is up to date (so running migrate on
that does nothing).
What happens if you recreate the test db and then run migrate on it?
Does the migrate run successfully?

Colin

Colin Law

unread,
Jan 30, 2015, 9:44:28 AM1/30/15
to rubyonra...@googlegroups.com
On 30 January 2015 at 14:40, Colin Law <cla...@gmail.com> wrote:
> On 30 January 2015 at 14:17, Sean Kelley <kelle...@gmail.com> wrote:
>>
>> Thanks. Your command line affects development db also which is not ideal. I looked up db:test:prepare and it appears to be deprecated in 4.2
>> http://stackoverflow.com/questions/15169894/what-does-rake-dbtestprepare-actually-do

I think if you read that right through including /all/ the comments
you will find it has been un-deprecated again.

Colin

Sean Kelley

unread,
Jan 30, 2015, 1:03:13 PM1/30/15
to rubyonra...@googlegroups.com
If I run:
bundle exec rake db:drop db:create db:migrate
I get error that db:migrate needs to be run with RAILS_ENV=test
if I 
bundle exec rake db:drop db:create db:migrate RAILS_ENV=test
I get error regarding duplicate table again

If I delete db from postgres management screen then run
bundle exec rake db:drop db:create db:migrate RAILS_ENV=test
I can again run the test once, but the second time I get the error that migrations are pending and to run db:migrate RAILS_ENV=test

I wonder why the act of running a simple test trigges this migration message

Sean Kelley

unread,
Feb 2, 2015, 9:20:31 AM2/2/15
to rubyonra...@googlegroups.com
I put this in my test.rb file and it seems to have fixed the problem:

# from: https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
config.active_record.maintain_test_schema = false

I also commented out any pending migration checks in my rails_helper file
Reply all
Reply to author
Forward
0 new messages