Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I ever want to revert a migration?

In Rails, migrations have a down method by default for reverting a migration. In what scenario would I ever want to revert a migration, though?

Some thoughts:

Whether in development or production, I always have a snapshot of my database to go back to, before I even run the migrations. Especially for migrations which perform data conversion, I find in most cases that reverting a snapshot is even faster than reverting a migration. (So I would never do it in a rush!)

If a migration were to fail, it would either:

  • fail with an exception on a non-transactional database, and thus leave the database broken, or
  • fail with an exception and roll back the transaction, and thus there would be no need to revert otherwise.

If the changes made are in production (or late in development), and later turn out to be a mistake, I would fix my mistake in a new migration. I would not revert the old one. In development, I'd simply delete the migration.

I also find that the down method introduces extra code in which I repeat myself, and thus may introduce new bugs. This is against the DRY principle.

So I'm curious about the pros, because I can't think of any.

like image 917
Stéphan Kochen Avatar asked Feb 21 '10 15:02

Stéphan Kochen


People also ask

How do I revert migration?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements. where n is number of migrations to rollback, counting from latest migration.

Which command is true to rollback migration in rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

How do I rollback migration code first in Entity Framework?

Rollback Migration Suppose you want to roll back the database schema to any of the previous states, then you can execute the update-database command with the –TargetMigration parameter to the point which you want to roll back to.


1 Answers

In development, it is easy and fast to incrementally "improve" migrations by using the down method automatically. Eg

  1. Create a migration and migrate to it
  2. Realize you need to make a change
  3. Migrate to the ver prior to your new migration by using db:migrate with a version
  4. Improve/fix your migration
  5. Rerun the migration task

Your method of taking snapshots works fine. But rails includes the same effect auto-magically using the "down" migration techniques. Works with all db's, tastes great

Added:

For production, I agree that a down migration shouldn't be needed. But sometimes mistakes happen and you need to roll back. The down migration path gives you a first, and quick opportunity to fix things in an emergency situation during an upgrade that goes wrong.

-- it is much faster to try a down migration in an emergency than to restore the db using a checkpoint.

like image 93
Larry K Avatar answered Oct 18 '22 20:10

Larry K