views:

75

answers:

4

As all databases should be, the source for ours is versioned using source control. The database is upgraded using a series of SQL scripts generated by Red Gate's comparison tool, which is essentially the same as an 'up' migration in the numerous database migration frameworks that seem to have sprung up recently.

But what's the point in the 'down' migrations in these frameworks? Often the code for the 'up' migration is extremely complex (typically complex data migration as features evolve) and I struggle to see the purpose of having to write it all in reverse for the 'down' one. It's certainly something I've never felt the need for. Am I missing something here...?

A: 
  1. Rollbacks. You push everything into production and it blows up - down migrations are a good safety net for rolling back.
  2. Or you're developing with multiple code branches - you can go back and forth between versions to your heart's content.
mopoke
As for #1, we found that backup before upgrade is usually a better practice.
Joshua
But with (1) surely you've tested it well, done numerous deployment tests, and tested backup/restore of your database; surely restoring the last-known-good database before deployment is better than running code, after all if up failed then why should down go any better? Also with (2) why not just start from the previous release baseline and only apply the scripts relevant to your branch? That's what we do and it works fine...
Greg Beech
Of course you should always backup your DB before making any changes in production. Rolling back using migrations could be faster than performing a full DB restore (while still preserving data which has altered between your backup and the restore point).Some systems use migrations extensively and it's part of the framework (e.g. rails) and so that's just how you do development. Of course there are other ways to achieve it. But if you use migrations for the ups, why not make use of them for the downs too? Keep the same system, make use of the tools which do a job well, etc...
mopoke
A: 

Customer: "We don't like the new version and want to go back to the old version."

Joshua
+2  A: 

It seems that the pertinent question here is:

  • Why would a scripted rollback ever be preferable to a full database restore from a backup taken immediately before the upgrade?

I can think of several reasons:

  1. The database is very large - say a few hundred GB - and your company cannot afford the downtime and/or administrative overhead that would be involved in a full restore.

  2. A bug was introduced that was not discovered until a week or two into production. If you've never experienced this before, you're lucky. Once you've got a week's worth of transactions in the new database, you can forget about just restoring from backup.

  3. The bug was not discovered until months into the release. In other words, you don't even have the backup anymore, and you're officially in damage control/disaster recovery mode. I've never experienced this, but I've heard stories. It's a scary thought - how do you undo all the damage that was done? In this case your downgrade might not be perfect, but it might still be better than the alternative.

  4. By contrast, perhaps the database changes were trivial - adding a few rows here, a few triggers there. In this case, a scripted rollback is going to take much less time than a restore. It's possible that some things that took hours to upgrade - such as creation of new indexes or addition of new columns - may only take seconds to downgrade (drop).

  5. You're deploying to customer sites. Some of them may not have backups at all (yeah, it's pathetic, but there's nothing you can do about it). If one of them needs a rollback, this is your only option.

There may be other reasons to have downgrade scripts - this is just off the top of my head.

Aaronaught
A: 

If you upgrade, and subsequently data is added to your database that you want to preserve, a rollback script (as long as it is designed as such) should achieve this, whereas if you simply restore a backup you'll lose it.

But you could get round the above by restoring a backup and using SQL Data Compare to copy the additional data across.

David Atkinson