How to make a migration irreversible [1.7]

131 views
Skip to first unread message

Bibhas

unread,
Dec 21, 2014, 7:32:29 AM12/21/14
to django...@googlegroups.com
Hi,

I need to make a migration irreversible. And strangely I don't see any way to do it in the migration doc. I see that the Operation class has a property called `reversible`, but don't see any way to set it to False.

I had a legacy model that was generated with inspectdb, hence it had managed=False. But now when I Need to make changes to it I removed the managed flag and the migration is trying to delete the table and create it again. I removed the droptable operation and faked the migration to avoid the error. But if I revert the migration, that table is going to be dropped anyway, right? Because it's the revert operation of CreateTable. How do I stop the backward migration for this particular migration? There is nothing much important before it, so it's not a trouble if it's irreversible.

Am I missing something really obvious?

Thanks,
Bibhas

Markus Holtermann

unread,
Dec 21, 2014, 8:53:50 AM12/21/14
to django...@googlegroups.com
Hey Bibhas,

thank you for the report. I can verify that behavior and would classify it as a release blocker since it leads to potential data loss. Can you open a ticket on https://code.djangoproject.com/ please.

In general a migration is a collection of operations. The migration will only be unapplied if all operations are reversible. There is no direct way to prevent a migration (file) to be unapplied. I'm not sure if it's worth to add a property / attribute to the migration class to centralize that check. You could open a ticket as well.

As a workaround you can add "migrations.RunPython(lambda apps, schema_editor: None)" as first operation in the operations list for that migration. It is a noop in forwards migrations and not reversible. But in adding a RunPython operation you will lose the ability to squash migrations.

/Markus

Bibhas

unread,
Dec 21, 2014, 10:15:38 AM12/21/14
to django...@googlegroups.com
Hey Markus,

Thanks for the suggestion. Added that snippet for now.

I was kind of surprised when I could not find a way to handle the backward migrations. I created a ticket - https://code.djangoproject.com/ticket/24037

For now I don't mind the migration being irreversible. But the perfect solution would be a way to write my own backward migration so that I can tell it to not drop my database and not make the whole migration chain broken.

Thanks,
Bibhas
Reply all
Reply to author
Forward
0 new messages