Auto Migrations

I have a love/hate relationship with Rails’ “Migrations” system. While it’s fantastically useful, and absolutely necessary if you’re ever planning on changing your schema after going live, I find it incredibly fiddly and annoying creating and modifying all those little files. They’re all numbered, you basically have to make a new file (and add to subversion) every single time you make a DB change – which is all the fucking time.

So I never end up using it. I *should* but I don’t. Isn’t that always the way. What I always end up doing is just making the changes directly in the DB, then exporting the DB schema to schema.rb, copying it into a big first migration, using that, and always having this plan to switch to migrations sometime after I’ve finished active development – I just can’t stand it getting in the way all the time before then.

So imagine my delight at Err The Blog’s latest plugin – “Auto Migrations”. Here’s the deal – you maintain one big schema file, which you already have in schema.rb, and whenever you change it you run the plugin – it looks at the DB, sees if anything needs to be added, removed, or indexed – and does it. One file. No more little migrations files. But you retain the ability to do live, in-place updates.

Fucking Brilliant. This is exactly what I wanted, and I didn’t even realise it. Take a look here. Tested and working for me. This kind of shit is exactly why I love Rails.

3 Responses to “Auto Migrations”

  1. Wincent Colaiuta Says:

    I use them but I too don’t like the way the number of migration files builds up over time. So, given that I haven’t actually deployed the app yet I periodically merge them all into one file. By “merge” I mean copy and paste, nothing fancy. Once I actually deploy I’ll use migrations como Dios manda.

  2. Sho Says:

    Well, why don’t you try out this plugin? It strikes me as a very elegant way to do things!

    Rails migrations are a wonderful idea but the implementation is a joke. For all that rhetoric about DRY, migrations are perhaps the single most repetitive and messy chore in the development process!

    I personally couldn’t care less about “versioning” my database, especially when data is lost going backwards through time. All I need, and my sole use for migrations, is to be able to maintain a local, and hopefully nice-looking, schema file, make any changes in that one place, and then to be able to deploy those changes automatically, both locally and remotely.

    This plug-in fits the bill perfectly. Highly recommended!

    And yes, I did go through the source (only 100 lines or so) .. it’s very nicely written. I would recommend doing a backup before running first time, of course, just in case .. : )

  3. Wincent Colaiuta Says:

    Different strokes for different folks I guess. I personally don’t like installing myriad plug-ins for every little thing. Too much bloat.

    What you describe as a “repetitive and messy chore” is for me an integral part of the iterative development process. For me the whole point of what we do is building something incrementally. It enables you to do TDD or BDD or something resembling it and have a working web application at every step of the process. The maintenance work of consolidating migrations into a single “setup” one is tiny compared to the rest of the development task.

Leave a Reply