echo "#\!/bin/bash\npython manage.py check_applied_missing_migrations\n" > .git/hooks/post-checkout
chmod + .git/hooks/post-checkout
Created
August 11, 2023 13:51
-
-
Save SebCorbin/bfebe4391b662d5aa5d08353ce80c36c to your computer and use it in GitHub Desktop.
Git post-checkout hook to detect applied missing migrations in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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