Detecting Incomplete or Missing Migrations with Django and CircleCI

Tue 04 October 2016 by George Dorn

Update (2017-06-15): Django 1.10 added --check and deprecated --exit. This makes the logic far more easy to follow. Just run this instead:

python makemigration --dry-run --no-input --check

And the same command will work in CircleCI (or your favorite CI service.)

Original, deprecated:

It's pretty easy to put together a pull request for a change in a model and then forget to create a migration for it. If you are using a CI server, here's a one-liner (for django 1.8 and above) that returns an error if there would be any migrations created by makemigrations:

! python makemigrations --dry-run --no-input --exit

The NOT (!) is important. The --exit flag to makemigrations causes it to return an error code when there are no migrations to make, and we're trying to detect the opposite situation.

In a circle.yml file, you could do this:

        - "! (python makemigrations --dry-run --no-input --exit && echo 'missing migrations')"

Note that quotes are required as ! is the tag operator in YAML and needs to be escaped.

Now when your CircleCI-enhanced django project gets a pull request, it'll see if migrations are complete and automatically mark the PR as broken if any are missing.

See the django-admin makemigrations docs, particularly the --exit flag.