Custom Search

Thursday, July 4, 2013

How to use django south

Install south
==========

http://south.readthedocs.org/en/latest/installation.html#using-easy-install

1)
Install south
---------------
#easy_install South

2)
Setting
----------
a) Add "south" to "settings.INSTALLED_APPS" and comment out all other apps
b) Then run "python manage.py syncdb"
c) Then uncomment all apps in the "settings.INSTALLED_APPS"
d) Then run "python manage.py runserver 8009" for testing

3)
Checking
----------
* Run "python manage.py --help" and you can see following commands provided by the "south" app.
[south]
    convert_to_south
    datamigration
    graphmigrations
    migrate
    migrationcheck
    schemamigration
    startmigration
    syncdb
    test
    testserver

4)
Note
---------------
http://south.readthedocs.org/en/latest/migrationstructure.html
When South loads migrations, it loads all the python files inside your app’s migrations/ directory in ASCII sort order (e.g. 1 is before 10 is before 2), and expects to find a class called Migration inside each one, with at least a forwards() and backwards() method.
When South wants to apply a migration, it simply calls the forwards() method, and similarly when it wants to roll back a migration it calls backwards()

Schema Migrations
===============

http://south.readthedocs.org/en/latest/tutorial/part1.html

1)
First step: Create a migrations directory inside our app.
---------------
* Run the command
#python manage.py schemamigration myapp --initial

*This command will create a migrations directory for us, and made a new migration inside it.

2)
How to apply/run our new migration script created by the command "python manage.py schemamigration myapp --initial"
How to create the tables
-----------------------------------
* Run the commmand
#python manage.py migrate myapp

* This command will create new tables for our models in the app "myapp"
* So we create a table witout using the command "python manage.py syncdb" <====

3)
How to update the tables
-------------------------
a) First change the moel definition (Eg: add a new column)
b) Create a new migration script using the option "--auto"
#python manage.py schemamigration myapp ----auto
c) Apply/Run the migration script created by the previous command
#python manage.py migrate myapp

4)
How to check the applied and not applied migration scripts
------------------------------------------------------------
#python manage.py migrate --list
* The output has an asterisk (*) next to a migration name if it has been applied, and an empty space ( ) if not

Data migrations
===============

* http://south.readthedocs.org/en/latest/tutorial/part3.html
* Data migrations are used to change the data stored in your database to match a new schema, or feature.

1)
Create an empty data migration script file
------------------------------------------
# python manage.py datamigration myapp my_script_file

2)
Open the data migration script file
-----------------------------------
#vim my_script_file.py
* In that file we can see models definitions, the forwards() and backwards() functions.
* Override the method "forwards()"

3)
Apply/Run the migration script
-------------------------------
#python manage.py migrate myapp

No comments:

Post a Comment