Home

Rollback A Deleted Rails Database Migration

You got yourself into that pickle where you created a migration, migrated, deleted the migration, then tried to rollback. Here's how you get around it!

I've run into this pickle a few times. Here's how the story goes:

  • Create a migration,
  • Migrate the database.
  • Delete the migration file.
  • Rollback the database.

Rolling back the database doesn't work at that point. That's because Rails can't find the file of its latest migration to know which one to jump to.

I even tried rolling back multiple steps, but no luck.

Already Committed To Git

If you have committed the file to your git history, you're in luck. Stash your current code, rollback, then pop your stash.

$ git add .
$ git stash
$ bundle exec rake db:rollback
$ git stash pop

You should be good to go at this point.

New File

If the migration file you had deleted was a new file, then you have to be a little trickier.

I've gotten around this by creating an empty migration file, migrating, then rolling back twice.

$ bundle exec rails g migration fix_rollback_error
$ bundle exec rake db:migrate
$ bundle exec rake db:rollback STEP=2

Then, make sure you delete the empty migration file, and be on your way!

Let's Connect

Keep Reading

How To Use Paper Trail As An Activity Feed

That whole killing two birds with one stone approach might just work in using PaperTrail as an activity log, at least in simpler projects.

Jun 15, 2015

Convert PDF to Image with Dragonfly and Rails

Converting a PDF to an image using Rails and Dragonfly is actually quite simple. Check it out.

Jan 06, 2015

How to Transition from CarrierWave to Dragonfly

It can be a process to move away from CarrierWave once you're already using it. Here's a step-by-step process to make it easy to transition to Dragonfly.

Jan 02, 2015