Wednesday, December 22, 2010

What to do when code breaks your migrations (Ruby/Git edition)

Sometimes, unless you're working for a retarded startup, you'll go away for a week or so and come back to find a massive change merged in. It's likely that you have a long list of pending database migrations.

It's also likely that the second migration got broken by a code commit that removed something it needs to work, so you can't run it. You need to figure out a quick way to roll back the code to a revision that works, run the migration, then revert back to the head commit. There's a few ways that git will help you here.

1. (easiest) Use git bisect. Find a commit that lets you get past the migration and run it (usually the one that contains the migration works...) then cancel the bisect. Done, total time 2 mins.
2. (still easy) Use git reset to reset your repo to the state it was at the failing commit. After the migration runs pull again and you're dandy.
3. If you don't have good version control you're a bit screwed. Anonymize a prod database dump and reload your dev database. Or find exactly what code breaks you migrations and temporarily monkeypatch it to get past the problem. Doing this multiple times sucks so after doing this once refuse to work without decent version control. If your boss won't budge then quit and go work for someone that's not a moron.

No comments:

Post a Comment