Skip to content

Commit

Permalink
Added check_migrations.py to check new migration files
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbiggs committed Feb 22, 2024
1 parent 741a91e commit 1d06db6
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/scripts/check_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import subprocess
import sys


def get_diff():
subprocess.run(
["git", "fetch", "origin"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
output = [
f"./{file_path}"
for file_path in subprocess.check_output(
["git", "diff", "--name-only", "origin/develop"]
)
.decode()
.splitlines()
]
if not output:
output = [
f"./{file_path}"
for file_path in subprocess.check_output(
["git", "diff", "--name-only", "origin/main"]
)
.decode()
.splitlines()
]
return output


def check_migration_file(file):
with open(file) as f:
contents = f.read()

keywords = ["DeleteModel", "AlterField"]
if any(keyword in contents for keyword in keywords):
print(f"Warning: {file} contains a migration that may cause data loss.")
return True


def main():
file_diff = get_diff()

migration_alert = False
for file in file_diff:
if "/migrations/" in file and file.endswith(".py"):
migration_alert = check_migration_file(file)
if migration_alert:
print("Please review the migrations before pushing, to ensure no loss of data.")
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit 1d06db6

Please sign in to comment.