Sunday, November 4, 2007

Always Use Automatic Migrations

The first time I saw Err the Blog's auto_migrations plugin I knew I would never respect any Rails project which didn't use it.

Here's why: I just took over a legacy Rails app from a client who used the app to learn Rails. The migrations are absolutely full of "maybe I'll do this - no, wait, I'll do that." By definition, every Rails developer has to learn Rails at some point, which means that there are a lot of apps like this out there. Guess what happens when a new dev comes on board? They go absolutely freaking nuts trying to migrate your DB from its initial state to a working state, and most of the energy they spend doing that is wasted energy trying to figure out why they can migrate up only to version X and not any further than that.

Worse yet, if there's some bug in a migration, or some minor config detail is off, your migrations fail mid-migration - which means you can't even migrate down to 0 and back up again gracefully, because the migration's up and down methods only do their jobs in the event of no errors. When there's an error, if you've got an up method which creates two tables, one of them is created and the other is not, and whatever the number in schema_info is, it's wrong. So your migrations have a huge wow factor and microscopic usefulness, and you're back in the pre-Ant and pre-Rake days of manual builds and spending all day, or several days, just getting a database working.

What a ridiculous waste of time.

If you use auto_migrations, you have one file to maintain, and either it works or it doesn't. It's nice, it's simple, it's plain, and if for some reason you've got a working DB but no files to generate it, you can get the file to generate it just by running rake db:schema:dump. This is the type of architecture that brought me to Rails and made me a true believer. It's a real pleasure to use, and I'd say it's the most absolutely essential plugin there is. There's no excuse for not using it in any situation, ever.

Update: I really shouldn't speak so stridently - I know intelligent people can disagree. This even holds true on Rails core.