Rolling back a failed Rails migration Rolling back a failed Rails migration ruby-on-rails ruby-on-rails

Rolling back a failed Rails migration


Unfortunately, you must manually clean up failed migrations for MySQL. MySQL does not support transactional database definition changes.

Rails 2.2 includes transactional migrations for PostgreSQL. Rails 2.3 includes transactional migrations for SQLite.

This doesn't really help you for your problem right now, but if you have a choice of database on future projects, I recommend using one with support for transactional DDL because it makes migrations much more pleasant.

Update - this is still true in 2017, on Rails 4.2.7 and MySQL 5.7, reported by Alejandro Babio in another answer here.


To go to a specified version just use:

rake db:migrate VERSION=(the version you want to go to)

But if a migration fails part way, you'll have to clean it up first. One way would be:

  • edit the down method of the migration to just undo the part of the up that worked
  • migrate back to the prior state (where you started)
  • fix the migration (including undoing your changes to the down)
  • try again


OK, folks, here's how you actually do it. I don't know what the above answers are talking about.

  1. Figure out which part of the up migration worked. Comment those out.
  2. Also comment out/remove the part of the migration that broke.
  3. Run the migration again. Now it will complete the non-broken parts of the migration, skipping the parts that have already been done.
  4. Uncomment the bits of the migration you commented out in step 1.

You can migrate down and back up again if you want to verify that you've got it right now.