Skip to content

Commit

Permalink
finally
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbiggs committed Feb 22, 2024
1 parent c8ddcd5 commit 47f2b80
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions .github/scripts/check_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,30 @@
import sys
import subprocess

def list_files_on_branch():
def get_diff():
# Run git diff command to list files on the specified branch
subprocess.run(['git', 'fetch', 'origin'])
output = subprocess.check_output(['git', 'diff', '--name-only', 'origin/develop']).decode().split('\n')
output = [f"./{file_path}" for file_path in subprocess.check_output(['git', 'diff', '--name-only', 'origin/develop']).decode().splitlines()]
if not output:
output = subprocess.check_output(['git', 'diff', '--name-only', 'origin/main']).decode().split('\n')
output = [f"./{file_path}" for file_path in subprocess.check_output(['git', 'diff', '--name-only', 'origin/main']).decode().splitlines()]
return output



def get_migration_files():
migration_files = []

for folder in os.walk('./etna'):
if folder[0].endswith('migrations'):
for file in folder[2]:
migration_files.append(os.path.join(folder[0], file))
return set(migration_files)

def get_main_branch_migration_files():
result = subprocess.run(['git', 'checkout', 'main'], stdout=subprocess.PIPE)
main_branch_files = get_migration_files()
subprocess.run(['git', 'checkout', '-'], stdout=subprocess.PIPE) # switch back to the original branch
return main_branch_files

def check_migration_file(filepath):
with open(filepath) as f:
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: {filepath} contains a migration that may cause data loss. Please review the migration.')
print(f'Warning: {file} contains a migration that may cause data loss. Please review the migration.')
sys.exit(1)

def main():
current_branch_files = list_files_on_branch()
print("Files on current branch:")
print(current_branch_files)
file_diff = get_diff()
print(file_diff)

# migration_files = get_migration_files()
# main_branch_files = get_main_branch_migration_files()
# new_files = migration_files
# for filepath in new_files:
# check_migration_file(filepath)
for file in file_diff:
if "/migrations/" in file and file.endswith(".py"):
check_migration_file(file)

if __name__ == '__main__':
main()

0 comments on commit 47f2b80

Please sign in to comment.