Rails tip: Dealing with out of sync migrations 4
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
endYeah, it’s a hack, but it’s a fairly robust hack.

Rails 2.1 names migrations based on timestamp, rather than sequential numbers specifically to address this issue.
Doesn’t that have problems when branches get merged?
@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.
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.