52đ
I just started using South, and Iâm 100% sold on it. Itâs also one of the few thatâs still under very active development.
South should be able to properly handle the issues youâve describe above. For each change to the db, it creates a file that has 2 methods âfowardâ and âbackwardsâ. Hereâs a sample automatically generated migration:
# > manage.py schemamigration issuetracker added-status-field --auto
# 0004_added-status-field.py
class Migration:
def forwards(self, orm):
# Adding field 'Issue.status'
db.add_column('issuetracker_issue', 'status', orm['issuetracker.issue:status'])
def backwards(self, orm):
# Deleting field 'Issue.status'
db.delete_column('issuetracker_issue', 'status')
A couple of the nice things about itâŠ.
-
South lets you rollback to a specific migration # if youâd like
-
If your production site is on migration 0002 and your SVN commit is on 0004, South will do 0003 then 0004 to bring the production db up to speed.
-
If youâve gone ahead and made the changes yourself, you can tell South to run a âfakeâ migration. Normally a migration system would throw a hissy fit, but this makes it really easy to have flexible control over your db.
manage.py migrate [appname] --fake
-
If you need to have something custom happen, like say copying the data in a column to another column, because the migration files are just python files itâs easily to modify the forward/backwards functions.
-
Migrating to South after having already deployed an application was rather easy. The latest version 0.6 includes a command for it actually.
manage.py convert_ to _south [appname]
-
And of course, how could I forget, my favorite feature is the automatic generation of migration files
manage.py schemamigration [appname] [description] --auto
Gotchas
I figured I should add in some tips for mistakes I made when getting started with South. Not everything is 100% intuitive.
-
After youâve run the convert_to_south command on your development database, donât forget to run
migrate --fake
on your production database, otherwise South will think itâs out of date. -
If youâre creating a new app, you use the
--initial
flag -
Stop using manage.py syncdb. Really.
-
Editing a model is a 3 step process â
1.) save the model changes
2.) run
schemamigration --auto
3.) run
migrate
to actually commit the changes to the database
Edit â To clarify the comments below, South was officially voted by the core contributors to not be included with Version 1.2. This was in part because the author of South had requested that it not yet be included. Even still, there is a lot of community support for South, and some reusable app makers are beginning to include South migrations with their app.
Edit #2 â I made some updates to reflect the new manage.py command structure from the current trunk version of South. âstartmigrationâ has been split to âschemamigrationâ and âdatamigrationâ depending on what youâre doing.