django database migrations with south
I have been using django for web development for almost a year now, and I just recently started using South to do database migrations. To be fair, most of the work that I have been doing with databases has centered around MongoDB and schema-less document stores instead of a traditional RDBMS. Since Django does not come with any database migration tools, my standard approach was to make sure that my models are completely thought out before running the manage.py syncdb command. The lack of a good database migration tool was one of the things that originally had turned me off to django.
Enter South. South lets you manage your database in a way very similar to how Ruby on Rails works.
Converting a project to a South-managed project is very easy:
Ensure that your database and models are completely synced up. (i.e. your models are not ahead of your database or vice-versa)
Install South by running
[sudo] pip install south
Add South to your
INSTALLED_APPSlist in the
settings.pyfor your django project.
./manage.py syncdbin your project root directory to add the South database tables to your database.
If you have an existing application that you would like to conver to a South-managed application, run the following command:
./manage.py convert_to_south YOUR_APP_NAMEIf not, go to the next step!
Now you are ready to go! You can change one of your models and then proceed to the next step.
Run the following command to get South to create an automatic migration for you:
./manage.py schemamigration YOUR_APP_NAME --auto
Now you can apply your newly created migration to your database:
./manage.py migrate YOUR_APP_NAME
Congratulations, you have performed your first database migration using South!
South lets you apply up to or back to any migration point by running a command like:
./manage.py migrate YOUR_APP_NAME 0001 (that command would take you back to your initial migration point. You can get a list of all your migrations and a description about each one by running
./manage.py migrate YOUR_APP_NAME --list. This lists all of the migrations you have available and denotes with a (*) which ones have been applied.
South is great for working in a team. All migrations are stored in YOUR_APP_NAME/migrations, so you can simply add these to your VCS and all of your team members will get all of your migrations. If there is a conflict in some of the migrations that you and a team member have been working on, South will detect it and let you merge the conflicts.
All in all, I am really loving South. It makes working with an RDBMS and Django much more pleasant!