Checking for missing migrations in Django
Back in the day, Django didn’t have a built-in way to change model schemas. You either had to figure out and apply the changes yourself or use a third party tool like South.
After a successful Kickstarter campaign, Andrew Godwin, Django core member and the author of South, added a native migration tool to Django. It solved problems such as conflicting merge names and the ability to squash migrations once there are too many of them in an app.
One thing it also changed is introduce what I have dubbed ‘cosmetic migrations’: these are migrations that make no changes to the database schema, but only add internal Django changes, such as changing a field’s
choices or the
ordering of a model. I’m sure the change is for good reason, but it annoys me to no end, because the lack of schema impact means I’m unlikely to notice that I haven’t made them until at some point I do make a schema-altering change, and that migration is then flooded with an untold number of cosmetic changes. This is a problem because commits should be atomic.
Django will occasionally notify you that ‘[y]our models have changes that are not yet reflected in a migration’, but I found that I would only see those when it was too late.
Thankfully, it is possible to make these checks yourself, although I have never seen it advertised anywhere. Executing this command will give your what you need:
$ python manage.py makemigrations --check
This command seems wholly counter-intuitive to me, but it does what I want: Exit with a code 1 if there are unreflected changes.
You can plug the above in to your Continuous Integration system or possibly a pre-commit hook. If you do so, I recommend that you also make it
--dry-run, like so:
$ python manage.py makemigrations --check --dry-run
This will ensure that no migrations are actually created, which just seems the saner option if you want to check.