The Spaghetti Refactory Established 2015

Don't delete your migration files!

This is probably Rails 101 but this is something that I just had the extreme displeasure to experience, so I felt the need to comment. 

Exercise EXTREME caution when deleting a migration that has been deployed. 

This can seriously mess up your database.

I recently took over a project where the previous developer had two differently-named migration files trying to add the same column to the same table in the database, and it was messing things up. However, the first (incorrectly-named) file had already been migrated, leaving DuplicateColumn errors when trying to migrate the database when it got to the next migration. Since the first file was the one that was incorrectly named, I deleted that one. Unfortunately, that one had already been deployed to staging, so when I tried to migrate on the staging server, it still gave me a DuplicateColumn error because it saw the duplicate migration as a new migration that needed to run, and the first column was still in the schema.

Long story short (is that still possible?), I had to re-create the incorrectly-named migration file and remove the second migration file that was doing the same thing instead. Then, since I'm a teensy bit OCD and I don't like have migration files with incorrect names, I wrote a new migration to remove the column, migrated to update the schema, then deleted both migration files and created one last one that would add that needed column back into the database schema.

Needless to say, it was a huge hassle, and the moral of the story is: Be Very Careful Deleting Deployed Migrations.

Tags