Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4migrationrails-migrations

Is there a way to resolve Rails deleted migrations?


I happened to have deleted migrations and I don't want to revert these removed migrations.

This is what rake db:migrate:status produces:

 Status   Migration ID    Migration Name
------------------------------------------------------

   up     0               ********** NO FILE *********
   up     20150209023430  Create users 
   up     20150320184628  ********** NO FILE **********
   up     20150322004817  Add roles to users
   up     20150403190042  ********** NO FILE **********

rake db:migrate and rake db:rollback commands won't work because of the missing files.

I have no intentions to lose my data, so I don't want to use rake db:drop or rake db:reset.

What can I do to be able to perform migrate and rollback and how to get rid of the missing files?


Solution

  • If you can find the migration's timestamp - the number part of the filename, then you can do:

    rake db:migrate:down VERSION=20100905201547
    

    You can find the timestamp using rake db:migrate:status

    Edit - when the migration file has been irrecoverably deleted

    You can get around this by creating an empty migration file, migrating, then rolling back twice. Then, make sure you delete the empty migration file before migrating again.

    $ bundle exec rails g migration fix_rollback_error
    $ bundle exec rails db:migrate
    $ bundle exec rails db:rollback STEP=2
    $ rm db/migrate/20200217040302_fix_rollback_error.rb
    $ bundle exec rails db:migrate