Rails tip: Dealing with out of sync migrations

Posted by Piers Cawley on May 5, 2008

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.