Rake db: rollback returning incorrect migration

I am having a problem returning the last migration.

Ever since I set the "letrate" score to evaluate, any one rake db:rollback

returns exactly this letrate gem migration, NOT the last migration as expected.

I doubt it has anything to do with the stone itself.

Any ideas how to fix this so I can enjoy a very convenient rollback?

Same as problem:

rake db:migrate:redo

      

Result:

==  CreateRates: reverting ====================================================
-- drop_table(:rates)
   -> 0.0224s
==  CreateRates: reverted (0.0225s) ===========================================

==  CreateRates: migrating ====================================================
-- create_table(:rates)
NOTICE:  CREATE TABLE will create implicit sequence "rates_id_seq" for serial column "rates.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rates_pkey" for table "rates"
   -> 0.1787s
-- add_index(:rates, :rater_id)
   -> 0.0032s
-- add_index(:rates, [:rateable_id, :rateable_type])
   -> 0.0024s
==  CreateRates: migrated (0.1850s) ===========================================

      

rake db: migrate: status

...
   up     20121205224038  Rename user address column
   up     20121206125016587  ********** NO FILE **********
   up     20121206125016605  ********** NO FILE **********
   up     20121210152550  Create reservations
   up     20121210180233  Create transactions
   up     20121210215840  ********** NO FILE **********
   up     20121218144200  Create videos
   up     20121218144800  Add video to videos
   up     20130108225007  Devise invitable add to users
   up     20130130202046  Acts as taggable on migration
   up     20130205154206  Create commissions
   up     20130207133520  Add user id to event transition

      

and files

-rw-r--r--@  1 joel  staff   137 Dec  7 16:40 20121205224038_rename_user_address_column.rb
-rw-r--r--@  1 joel  staff   443 Dec  7 16:40 20121206125016587_create_rating_caches.rb
-rw-r--r--@  1 joel  staff   432 Dec  7 16:40 20121206125016605_create_rates.rb
-rw-r--r--@  1 joel  staff   429 Dec 10 23:30 20121210152550_create_reservations.rb
-rw-r--r--@  1 joel  staff   414 Dec 10 19:03 20121210180233_create_transactions.rb
-rw-r--r--@  1 joel  staff   237 Dec 18 15:44 20121218144200_create_videos.rb
-rw-r--r--@  1 joel  staff   172 Dec 18 16:18 20121218144800_add_video_to_videos.rb
-rw-r--r--@  1 joel  staff   758 Jan  8 23:50 20130108225007_devise_invitable_add_to_users.rb
-rw-r--r--   1 joel  admin   775 Jan 30 21:20 20130130202046_acts_as_taggable_on_migration.rb
-rw-r--r--@  1 joel  admin   422 Feb  5 17:05 20130205154206_create_commissions.rb
-rw-r--r--@  1 joel  admin   266 Feb  7 15:20 20130207133520_add_user_id_to_event_transition.rb

      

+3


source to share


1 answer


Ok, the problem is the version number of your porting port. Rails simply orders timestamps in the migration files to see which one has been applied recently. With 3 more digits in the timestamp, 20121206125016605_create_rates.rb and 20121206125016587_create_rating_caches will always be detected as recent migrations.

Try to fix it and clear the migration status. First rollback of problematic migrations:

rake db:rollback STEP=2

      

Yours rake db:migrate:status

should now look like this:

up     20121205224038  Rename user address column
up     20121210152550  Create reservations
up     20121210180233  Create transactions
up     20121210215840  ********** NO FILE **********
up     20121218144200  Create videos
up     20121218144800  Add video to videos
up     20130108225007  Devise invitable add to users
up     20130130202046  Acts as taggable on migration
up     20130205154206  Create commissions
up     20130207133520  Add user id to event transition

      

Now correct their version number (assuming your migrations are in the db/migrate

default folder )

mv db/migrate/20121206125016605_create_rates.rb db/migrate/20121206125017_create_rates.rb
mv db/migrate/20121206125016587_create_rating_caches.rb db/migrate/20121206125016_create_rating_caches.rb

      

Yours db:migrate:status

should now look like this:

up     20121205224038  Rename user address column
down   20121206125016  Create rating caches
down   20121206125016  Create rates  
up     20121210152550  Create reservations
up     20121210180233  Create transactions
up     20121210215840  ********** NO FILE **********
up     20121218144200  Create videos
up     20121218144800  Add video to videos
up     20130108225007  Devise invitable add to users
up     20130130202046  Acts as taggable on migration
up     20130205154206  Create commissions
up     20130207133520  Add user id to event transition

      

Now the "timeline" is fixed, but we still need to reapply these migrations. rake db:migrate

won't work because the last migration is now 20130207133520_add_user_id_to_event_transition.rb and it is already being applied, so the rake thinks it is relevant ... So we need to fool the rake: edit every migration file that appears after 20121206125017_create_rates.rb in the migration status output by commenting everything inside the method down

. If only a method exists change

, comment it out and create empty up-down methods. Thus, all these methods will look something like this:

def down
  # This is the old code
  # which I will uncomment later...
end

      

You also need to create an empty migration because there is a strange migration that doesn't have an associated file ( ********** NO FILE **********

). Therefore, create a file called db / migrate / 20121210215840_ghost_migration.rb with the following content:



class GhostMigration < ActiveRecord::Migration
  def up
  end

  def down
  end
end

      

We are now ready to simulate the ride all the way back. So that

rake db:rollback STEP=9

      

The migration status output should now be

up     20121205224038  Rename user address column
down   20121206125016  Create rating caches
down   20121206125016  Create ratings
down   20121210152550  Create reservations
down   20121210180233  Create transactions
down   20121210215840  Ghost migration
down   20121218144200  Create videos
down   20121218144800  Add video to videos
down   20130108225007  Devise invitable add to users
down   20130130202046  Acts as taggable on migration
down   20130205154206  Create commissions
down   20130207133520  Add user id to event transition

      

You can now change the files back to their original state by uncommenting what you commented earlier and deleting the Ghost migration file. You should actually comment out the "up" methods the same way we did using the down methods, delete the Ghost migration file and migrate everything

rake db:migrate

      

Finally, uncomment whatever you commented in the files and everything should be smooth after that (hopefully).

Regarding why this happened in the first place, I think it actually has something to do with the gem itself, which in my opinion shouldn't generate these migrations with invalid (or at least non-standard) version numbers. It looks like the stone generates two migrations in one second, so maybe the author added these 3 extra digits to prevent the version number from colliding. I think it would be better to do everything in one migration.

Hope this helps you solve your problem!

UPDATE

Maybe I did too much. If you don't mind that you discard all your migrations and then migrate them again, you don't need to comment on anything in any file (the "Ghost Migration" trick will still be needed). I just felt it would be safer.

+5


source







All Articles