Django Migrations Doesn't Play Nice with Third-Party Stuffs
I ran into something annoying while working on my Tweetpile project the other day and it just happened to me today on Atlas. Sometimes, removing code can cause explosions with migrations -- even when they've already been run.
Example:
- You've created a new class called
MyClass
. - It subclasses
models.Model
It makes use of a handy mixin you wrote called
MyMixin
:class MyClass(MyMixin, models.Model): # stuff here
You create a migration for it, run it, commit your code and congratulate yourself on code well done.
- Months later you come back and realise that the use of
MyMixin
was a terrible mistake, so you remove it. - Now migrations don't work anymore.
Here's what happened:
Creating a migration that's dependent on non-Django-core stuff to assemble the model (think mixins that add fields, or the use of custom fields etc.) means that migrations
has to import those modules to run. This is a problem because every time you run manage.py migrate
it loads all migration files into memory, and if those files are importing now-non-existent modules, everything breaks.
Solution:
It's an ugly one, but so far it's the only option I can figure: manually collapsing the migration stack. Basically you make sure you've run all of the migrations to date, then delete the offending classes, delete all of the migration files, and recreate a new empty migration:
$ cd /project/root/
$ ./manage.py migrate
$ rm -rf myapp/migrations/*
$ touch myapp/migrations/__init__.py
[ modify your code to remove the offending fields/mixins ]
$ ./manage makemigrations myapp
Now run this in your database:
DELETE FROM django_migrations WHERE app = 'myapp' AND name <> '0001_initial';
UPDATE django_migrations SET applied = NOW() where app = 'myapp';
The new single migration created won't be importing the removed classes, so everything will be ok, and you have the added benefit of not having so many migrations to import. Note however that this may cause problems with migrations from other apps that may have been created dependent on your now-deleted migrations, so this may start you down a rabbit-hole if you're unlucky.
I hope this helps someone in the future should this sort of thing present itself again.
Comments
Post a Comment of Your Own
Preview