Django and South

Recently I have been experimenting with some Heroku apps and up to now it hasn’t mattered whether or not the data gets destroyed each time I push a new release. However, now that some of these have some traction I need to be able to ensure that the data is preserved and/or extended each time. In Ruby-on-Rails this was simple as there was in-built support for database migrations. For some reason, Django doesn’t have these built in. Luckily there is South.

Using it is fairly straightforward, install it:

# easy_install south

Then, against your application:

1 # python <app>/manage.py syncdb
2 # python <app>/manage.py convert_to_south <app>
3 # python <app>/manage.py schemamigration <app> --auto
4 # python <app>/manage.py migrate <app>

Now, add south to your requirements.txt file:

South>=0.7.6

Finally, just add all of the south migrations to your git repository and you’re ready to go.

Once this has been done, you can push up to heroku and then:

# heroku run python <app>/manage.py syncdb --migrate

Although the first time I did this I needed to drop all of my database tables otherwise the schema migration didn’t work due to pre-existing tables and/or indexes.

Now that your app is South-aware, you can simply make changes to your models.py anytime you need to. To update the migrations you only need to:

# python <app>/manage.py schemamigration <app> --auto

Then when you push to heroku just remember to re-run the sync db command and you should be golden.


Tags: django, south, migrations, heroku
blog comments powered by Disqus