[Django]-Backwards migration with Django South

335πŸ‘

βœ…

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.

πŸ‘€Ian Clelland

219πŸ‘

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

πŸ‘€Ctrl-C

3πŸ‘

Add a migration name at the end of the parameters:

./manage.py migrate app-name 00xx-migration-name
πŸ‘€Jerzyk

Leave a comment