Skip to content

Instantly share code, notes, and snippets.

@geoom
Last active December 19, 2015 10:49
Show Gist options
  • Save geoom/5943179 to your computer and use it in GitHub Desktop.
Save geoom/5943179 to your computer and use it in GitHub Desktop.
basic commands for using south (tested in ubuntu)
/* -----------------------------------------
Installation
----------------------------------------- */
pip install south // install south app
// added south to INSTALLED_APPS in settings
python manage.py syncdb // create the app's models that we don't need migrate (auth, contenttypes, session, admin, south, other instaled apps ...)
/* -----------------------------------------
Working with a new app
----------------------------------------- */
// create the app (startapp <app_name>) and fill the models.py file
python manage.py schemamigration <app_name> –initial // created the script to initial migration for specific app
python manage.py migrate app_name // to apply the changes to database
/* -----------------------------------------
Working with a previously created app (only for apps that we want that supports migrations)
----------------------------------------- */
python manage.py convert_to_south <app_name> // initial convertion
python manage.py migrate app_name –fake // to developers are using their database's own instance
/* -----------------------------------------
MIGRATE MODELS
----------------------------------------- */
python manage.py schemamigration app_name –auto // create the new migration
python manage.py migrate app_name // to apply the changes to database
/* ---------------------------------------------------------------------------
Set up the database right after clone a repository that works with south
------------------------------------------------------------------------------- */
python manage.py syncdb // load Third-party apps on database
python manage.py migrate --all //apply to the latest migration for all remaining apps
/* ---------------------------------------------------------------------------
Reset migration history for all project
------------------------------------------------------------------------------- */
rm -r appname/migrations/
./manage.py reset south
./manage.py convert_to_south appname
** reset south is not a good idea if you have any third party apps that uses South, for example django-cms
/* ---------------------------------------------------------------------------
Reset migration history for specific app
------------------------------------------------------------------------------- */
1. Delete migration history for my apps
sql> delete from south_migrationhistory where app_name = 'my_app';
2. Delete migrations for my apps
$ rm -rf my_app/migrations/
3. Create new initial migrations for my apps
$ ./manage.py schemamigration --initial my_app
4. Fake execute the initial migrations for my apps
This inserts the migrations into south_migrationhistory without touching actual tables:
$ ./manage.py migrate --fake my_app
Step 3 and 4 is actually just a longer variant of manage.py convert_to_south my_app, but I prefer that extra control, in such delicate situation as modifying the production database.
/* -----------------------------------------
References
----------------------------------------- */
http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/
http://agiliq.com/blog/2012/01/south/
http://south.readthedocs.org/en/latest/index.html
http://stackoverflow.com/a/15323642
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment