Backwards migration with Django South

DjangoMigrationDjango South

Django Problem Overview


Ok, so this seems like a really silly thing to ask, and I'm sure I'm missing something somewhere.

How do you perform a backwards migration using South on Django?

So I've tweaked my models, created a migration with schemamigration, run the migration with migrate, and now I realise that's not quite what I wanted and I want it back the way before.

Short of manually editing db tables and removing migration files, how should I go about rolling the migration back? I find references to backward migrations using South via Google, but have yet to find a solid code example for it.

Can anyone help?

Django Solutions


Solution 1 - Django

You need to figure out the number of the migration just before the one you want to roll back.

Your app should have a migrations directory, with files in it named like

0000_initial.py
0001_added_some_fields.py
0002_added_some_more_fields.py
0003_deleted_some_stuff.py

Normally, when you run ./manage.py migrate your_app, South runs all new migrations, in order. (It looks at the database tables to decide which ones are 'new').

However, you can also specify any migration by number, and South will migrate your database, either forward or backward, to take it to that point. So, with the example files above, if you have already migrated up to 0003, and you wanted to run 0003 in reverse (undoing it, effectively), you would run

./manage.py migrate your_app 0002

South would look at the database, realise that it has run 0003 already, and determine that it has to run the reverse migration for 0003 in order to get back to 0002.

Solution 2 - Django

Just in case someone (like me) wondered how to migrate back from initial (0001):

django-admin.py migrate some_app zero

output:

Running migrations for some_app:
 - Migrating backwards to zero state.
 < some_app:0001_initial

"zero" is a special state before any migration.

Reference: http://south.aeracode.org/docs/commands.html

Solution 3 - Django

Add a migration name at the end of the parameters:

./manage.py migrate app-name 00xx-migration-name

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRuiwenView Question on Stackoverflow
Solution 1 - DjangoIan ClellandView Answer on Stackoverflow
Solution 2 - DjangoCtrl-CView Answer on Stackoverflow
Solution 3 - DjangoJerzykView Answer on Stackoverflow