Doesn't this same problem exist always - whether you are going down or
up, or store your migrations in files or the database? Some would
consider it an antipattern to put any models or app classes in your
Some would be wrong if they considered that an anti pattern ;). The
vast bulk of migrations in my projects are model-dependent for things
like cleaning up a data problem in production, initialising counter
fields based on non-sql-calculatable fields (e.g. API calls)
My normal practice when writing migrations which depend on model code
is to put a migration specific model clase in the migration file
itself, scoped inside the migration. The model mimics a subset of the
one in app/models including only necessary attribute declarations and
If you store the migrations in your version control system you can
recover the state of the migrations at any point in time. For a
particularly tricky down migration you can always step back revisiion
by revision. The database would have no such history and could never
provide the same level of fidelity or reliability that your SCM can.
Absolutely true. And the technique I described decouples the running
of migrations from the current state of the models, which facilitates
recovering the state of the migrations without requiring incremental
steps of get a revision, run migrations, get the next revision run
migrations... if that makes sense.
I'm still not clear on the original poster's root problem and
solution, but I don't think the possibility of outdated non-schema
migration code is a good reason to reject the idea.
The specific case mentioned was rolling back when the checkout is at
an older version and doesn't have the down migration. The solution
there is to rollback from the original revision.
I have to say that I never understood the proposed solution, frankly
it scared the bejesus out of me, but maybe that's just me.
If there's another case I'd be happy to re-evaluate but the power of
migrations is in their simplicity they're not some darcs-ish patch
algebra for DDL, they're little scripts that you run in order in which
you can do whatever you want.