Just A Summary

Piers Cawley Practices Punditry

Rails tip: Dealing with out of sync migrations 4

Posted by Piers Cawley Wed, 07 May 2008 15:09:00 GMT

Sometimes, for one embarrassing reason or another (usually involving chaotic branch merges…) a database migration can get leapfrogged. When this happens, it’s tempting to renumber the leapfrogged migration, but that breaks any servers where the migration didn’t get renumbered. Here’s how I dealt with it recently:

class MaybeOldMigration < ActiveRecord::Migration
  def self.up
    unless old_migration_applied?
      OldMigration.up
    end
  end

  def old_migration_applied?
    # Checks that the schema looks as it should
    # if the old migration got applied
  end
end

Yeah, it’s a hack, but it’s a fairly robust hack.

Comments

Leave a response

  1. Avatar
    Chris Heald 4 months later:

    Rails 2.1 names migrations based on timestamp, rather than sequential numbers specifically to address this issue.

  2. Avatar
    Piers Cawley 4 months later:

    Doesn’t that have problems when branches get merged?

  3. Avatar
    Pat Maddox 4 months later:

    @piers Only if the migrations have dependencies between them. If one migration creates a users table and one migration creates a companies table, it doesn’t matter.

  4. Avatar
    Piers Cawley 4 months later:

    So, if we have a situation where there’s a master branch, which has migrations up to Tuesday, and on Wednesday a branch gets merged which introduces a migration that’s timestamped Monday, the Monday migration would still be applied correctly by a cap deploy?

    I don’t like renumbering (or re timestamping) migrations because that can cause confusion on developer boxes with attempts to reapply migrations and the like. Hence the hack.

Comments



Just A Summary