Backwards migration with Django South

Ruiwen picture Ruiwen · Apr 28, 2011 · Viewed 68.7k times · Source

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?

Answer

Ian Clelland picture Ian Clelland · Apr 28, 2011

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.