Manage migrations in Django

March 26, 2011 | By Regaz | Filed in: Uncategorized.

If you want to manage your migrations in Django in a similar way than
is done on Rails you will need to install and use the South framework for Django.

I will resume here the easiest way to installing it :)

$ easy_install Sout

Once this ha been correctly accomplished you will follow to setup you application
for correctly using South.

I suppose you already are in a Django project and you have make a new application
than I will suppose memo-ridiculously to call southpark :)

$ python manage.py startapp southpark

Now, for permit use of South inside to your application you need to insert both
‘southpark’ and ‘South’ in
your settings.py for example like that:

INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
‘southpark’,
‘south’
)

After that you will provide to make the South migration history table for the first time by executing a syncdb:

$ python manage.py syncdb

than will give you a response from terminal something like that:

Syncing...
Creating table south_migrationhistory
Synced:
...
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)

Next we need to create an initial migration for South by:

$ python manage.py schemamigration YOURPROJECT.southpark --initial

You will get a terminal’s reply than will resemble something like below:

+ Added model southpark.Booking
+ Added unique constraint for ['booking_date', 'booking_number'] on southpark.Bo
oking
+ Added model southpark.Address
Created 0001_initial.py. You can now apply this migration with: ./manage.py migr
ate YOURPROJECT.southpark

Do Not Forget to Take a Look…

Bloogger Blogger, Google Blogger, Google Google

Feel Better on Debian Linux

Now we need to give control to South on our application by executing:

$ python manage.py migrate

Than will show a terminal response resembling to this beneath:

project.customer 0001 --fake
- Soft matched migration 0001 to 0001_initial.
Running migrations for southpark:
- Migrating forwards to 0001_initial.
(faked)

All the steps above where necessary for to setup South for the first time on your application and after every time you will need to make a new migration a database’s change you will provide to:
1) prepare your migration:

$ python manage.py schemamigration project.customer --auto

2) execute this one:

$ python manage.py migrate project.customer

All my best…
Push hard & Be strong! :)
Davide


Tags: , , , ,