217

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?

murgatroid99
  • 15,284
  • 8
  • 52
  • 88
Ruiwen
  • 2,285
  • 2
  • 16
  • 12

3 Answers3

334

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
  • 39,551
  • 8
  • 79
  • 83
  • 1
    Unfortunately, when you create your next migration, it doesn't skip the ones in between, so you just migrate through them again later. Seems like there could be a better way. – mlissner Jul 02 '11 at 19:51
  • 44
    @mlissner If you really want, after rolling back the database, go to the migrations folder of the given app (in the above example your_app/migrations) and delete the unwanted migration – Josh Russo Aug 16 '11 at 00:15
  • 1
    Exactly -- South never skips migrations; it expects that the files from 0001-nnnn represent a consistent set of migrations, for any value of nnnn. If that's not the case, then you need to re-order or delete the offending ones yourself. – Ian Clelland Aug 19 '11 at 15:13
218

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,871
  • 1
  • 20
  • 29
  • 6
    Someone ran migrate 0001 --fake, and this was the only way to run 0001 backwards. Thanks! – jmanning2k Jul 06 '12 at 22:12
  • 1
    Very important answer, I wondered why `migrate 0000` didn't work. About the fake migration, yes, you may need it, if you e.g. need to only undo the (probably wrong) initial migration, but the migration history thinks, that this migration never happened. – Tomasz Gandor Jan 16 '14 at 16:01
3

Add a migration name at the end of the parameters:

./manage.py migrate app-name 00xx-migration-name
Jerzyk
  • 3,496
  • 20
  • 40
  • 2
    It's OK, and I did that before, but it's a lot of typing / pasting. The bare "state" number - in this case `00xx` - is enough. When improving and testing a migration you can have both commands in the history: forward (no argument), backward with previous state number. – Tomasz Gandor Feb 01 '13 at 09:14