ChatGPT解决这个技术问题 Extra ChatGPT

Backwards migration with Django South

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?

good question!!

I
Ian Clelland

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.


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 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
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.
C
Ctrl-C

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


Someone ran migrate 0001 --fake, and this was the only way to run 0001 backwards. Thanks!
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.
J
Jerzyk

Add a migration name at the end of the parameters:

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

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.