(Note: this procedure will work pretty well for simple additions of properties on models, and the like. More complex changes will require more thought.)
Let's assume you have a branch off of master
that has been creating lots of migrations. It forked when master
was at 0017_migration_on_master.py. Now, on the branch, let's call it data-crazy
, we're up to 0028_migration_on_data_crazy.py. Sadly, someone had to jump onto master
and make a schema change to the database. This bumped master
up to 0018_migration_on_master.py. Now, we need to merge those changes into data-crazy
in order to eventually be able to merge back into master
once we're stable and our feature is complete.
- Validate the new base of your rebase.
$ cd migrations
$ ls -1 | sort | sed -e 's/\([0-9]*\).*/\1/' | uniq -d
- Diff the first conflicting migration made in
master
against its predecessor (which should be in both branches). Take that diff and remove the top hunk(s), keeping on the static parts at the bottom. If you don't know what I mean, ask me in person. Then patch that newly edited diff over each of the later existing migrations indata-crazy
. Be sure to usediff -c
in order forpatch
to be able to find the appropriate context within which to apply the new state.
# Create the diff to propagate through the rebased migrations
$ diff -c 0017_migration_on_master.py 0018_migration_on_master.py > 0018.diff
# IMPORTANT: Remove all of the diff hunks that relate to the initial add/remove operations in your migration
# Keep only the lines that correspond to the schema snapshot part of the migration file.
$ vi 0018.diff
# Clean things up a bit
$ rm *.orig *.rej *.pyc
$ patch 0018_migration_on_data_crazy.py 0018.diff
$ patch 0019* 0018.diff
$ patch 0020* 0018.diff
# etc...
- Rename all of the new files to bump their version numbers correctly (TODO: find a tool that can bump file numbers and keep the leading zeros)
$ mv 0028_migration_on_data_crazy.py 0029_migration_on_data_crazy.py
$ mv 0027_migration_on_data_crazy.py 0028_migration_on_data_crazy.py
$ mv 0026_migration_on_data_crazy.py 0027_migration_on_data_crazy.py
#etc...
Migrating your data on your dev machine is left as an exercise for the reader, but if you can pgdump from your production server, and then cat that into your dbshell, you should be well on your way...