Skip to content

Instantly share code, notes, and snippets.

@SamStudio8
Created January 21, 2022 12:14
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 SamStudio8/d38d70c9d181860a2ec0726e517f4fe8 to your computer and use it in GitHub Desktop.
Save SamStudio8/d38d70c9d181860a2ec0726e517f4fe8 to your computer and use it in GitHub Desktop.
Django unchecked migration pre-commit
#!/usr/bin/env python3
import sys
import subprocess
p = subprocess.Popen("git ls-files --others --exclude-standard", shell=True, stdout=subprocess.PIPE)
out, err = p.communicate()
migrations = []
for unchecked_rp in out.decode('UTF-8').split('\n'):
if "migrations" in unchecked_rp:
# Look for a Migration class just to be sure?
with open(unchecked_rp) as maybe_migration_fh:
for line in maybe_migration_fh:
if "class Migration" in line:
migrations.append(unchecked_rp)
break
if len(migrations) > 0:
sys.stderr.write("Unchecked migrations detected: \n")
for migration_name in migrations:
sys.stderr.write(f" * {migration_name}\n")
sys.stderr.write("Commit all these files to permit a commit.\n")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment