Skip to content

Instantly share code, notes, and snippets.

@smcoll
Created February 10, 2017 20:39
Show Gist options
  • Save smcoll/8a3579feff6ab2c024f67da5f6a56280 to your computer and use it in GitHub Desktop.
Save smcoll/8a3579feff6ab2c024f67da5f6a56280 to your computer and use it in GitHub Desktop.
Django test to detect uncreated/unmerged migration files
from StringIO import StringIO
from django.core.management import call_command
from django.test import TestCase, override_settings
class TestMigrationDiscrepancies(TestCase):
@override_settings(MIGRATION_MODULES={})
def test_for_missing_migrations(self):
output = StringIO()
try:
call_command('makemigrations', interactive=False, dry_run=True, exit_code=True, stdout=output)
except SystemExit as e:
# The exit code will be 1 when there are no missing migrations
assert unicode(e) == '1'
else:
self.fail("There are missing migrations:\n %s" % output.getvalue())
@smcoll
Copy link
Author

smcoll commented May 10, 2018

For Django 2.0:

@override_settings(MIGRATION_MODULES={})
def test_for_missing_migrations(self):
    """ Test for missing migrations; the exit code will be non-zero.
    """
    output = StringIO()
    try:
        call_command('makemigrations', dry_run=True, check=True, stdout=output)
    except SystemExit:
        self.fail("There are missing migrations:\n %s" % output.getvalue())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment