Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacqueswww/5ba0ac7e27f7b05ad859d0428e5c4177 to your computer and use it in GitHub Desktop.
Save jacqueswww/5ba0ac7e27f7b05ad859d0428e5c4177 to your computer and use it in GitHub Desktop.
Django management command to detect missing migration files.
import sys
from django.apps import apps
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connections
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.state import ProjectState
from django.db.utils import OperationalError
class Command(BaseCommand):
"""
Detect if any apps have missing migration files
(not necessaily applied though)
"""
help = "Detect if any apps have missing migration files"
def handle(self, *args, **kwargs):
changed = set()
self.stdout.write("Checking...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
sys.exit("Unable to check migrations: cannot connect to database\n")
autodetector = MigrationAutodetector(
executor.loader.project_state(),
ProjectState.from_apps(apps),
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
if changed:
sys.exit("Apps with model changes but no corresponding migration file: %(changed)s\n" % {
'changed': list(changed)
})
else:
sys.stdout.write("All migration files present\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment