Test your reverse migrations
So here we are, merrily generating models, controllers and more. We’re writing code, writing tests and even running the tests to make sure they pass. All is good, right?
Wrong.
How many of you test your reverse migrations? Do they even work? I was bitten by this recently. The column names in a migration were spelled incorrectly so I thought, no problem, I’ll migrate down, fix the script and migrate up again.
Not so fast! The down migration was dropping columns that hadn’t been added yet — an error caused by modifying the migration before reversing it. Another error was caused by a misspelled table name, it wasn’t pluralized.
Since I’m working with mysql, ddl isn’t wrapped in a transaction. My development database is a snapshot of the customer’s data so dropping the whole database to recover from the failed migration isn’t an option either.
My advice: after creating a migration, migrate up, then immediately migrate back down. Make sure it works before moving on. The time you save might just be your own.
Filed under: Rails

Leave a Reply