Skip to content

Instantly share code, notes, and snippets.

@SebCorbin
Created August 11, 2023 13:51
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 SebCorbin/bfebe4391b662d5aa5d08353ce80c36c to your computer and use it in GitHub Desktop.
Save SebCorbin/bfebe4391b662d5aa5d08353ce80c36c to your computer and use it in GitHub Desktop.
Git post-checkout hook to detect applied missing migrations in Django

To install this hook

echo "#\!/bin/bash\npython manage.py check_applied_missing_migrations\n" > .git/hooks/post-checkout
chmod + .git/hooks/post-checkout
from django.core.management import BaseCommand
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
class Command(BaseCommand):
def handle(self, *args, **options):
# Work out which apps have migrations and which do not
executor = MigrationExecutor(connection)
applied_missing_migrations = set(executor.loader.applied_migrations) - set(
executor.loader.disk_migrations
)
if applied_missing_migrations:
self.stderr.write(
"Warning: you have applied migrations that no longer exist:"
)
for app_label, migration_name in applied_missing_migrations:
self.stderr.write(
f" - {app_label}: {migration_name}", style_func=self.style.WARNING
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment