Wednesday, October 07, 2009

Django South Migrations

Install Django South
- Place the untared 'south' folder into your Django applicaion
- Modify the project's settings.py and add 'south' to the INSTALLED_APPS list
- For new Django projects:
---1) initialize any Django apps or models to monitor with South.
----a) See Initializing Applications below
---2) run ./manage.py syncdb
---3) run ./manage.py migrate

To view help on startmigration:  # South 7.x+ uses the schemamigration command instead
./manage.py startmigration
Usage: ./manage.py startmigration appname migrationname [--initial] [--auto]
[--model ModelName] [--add-field ModelName.field_name] [--freeze] [--stdout]

Initializing Applications - needed whenever you want to track more Django apps
# this creates a 'migrations' folder in your Django app directory
./manage startmigration app_name comment --initial  # updates for South 7.x+
./manage schemamigration app_name comment --initial # Init's all models in application
./manage startmigration app_name --initial # updates for South 7.x+
./manage schemamigration app_name --initial # comment is optional here

Adding South with no data and no existing models.py objects:
# 1) syncdb as normal
python manage.py syncdb
# 2) Start an application if needed
python manage.py startapp appname
# 3) create a migration file with a blueprint to create tables
python manage.py startmigration appname --initial 
 # updates for South 7.x+
python manage.py schemamigration appname --initial
# 4) Apply the blueprint and create the tables
python manage.py migrate appname
# 5) Repeat steps 3 and 4 for each additional app if needed

Adding south with no data and existing models.py objects:
# 1) syncdb as normal if needed
python manage.py syncdb
# 2) create a migration file with a blueprint to create tables
python manage.py startmigration appname --initial # updates for South 7.x+
python manage.py schemamigration appname --initial
# 3) Apply the blueprint but it doesn't actually create the tables
python manage.py migrate appname --fake
# 4) Repeat steps 2 and 3 for each additional app if needed

Applying changes after making changes to a model (repeat for each app):
# Creates a migration 'blueprint' file and guesses what should take place
python manage.py startmigration appname migrationname --auto # updates for South 7.x+
python manage.py schemamigration appname migrationname --auto
# OR create the blueprint file but it will be empty
python manage.py startmigration appname migrationname  # updates for South 7.x+
python manage.py schemamigration appname migrationname

# make any modifications needed
# migrate the tables in the app
python manage.py migrate appname
# OR migrate all tables in all apps
python manage.py migrate

Adding a new model to an app already managed with south:
./manage startmigration app_name comment --model m1 --model m2 # updates for South 7.x+
./manage schemamigration app_name comment --model m1 --model m2 # inits specific models

List available migrations
python manage.py migrate --list

http://south.aeracode.org/wiki/ConvertingAnApp

No comments: