Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): Safer migrations #14452

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions posthog/management/commands/test_migrations_are_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,42 @@ def run_and_check_migration(variable):
try:
results = re.findall(r"([a-z]+)\/migrations\/([a-zA-Z_0-9]+)\.py", variable)[0]
sql = call_command("sqlmigrate", results[0], results[1])
if (
re.findall(r"(?<!DROP) (NOT NULL|DEFAULT)", sql, re.M & re.I)
and "Create model" not in sql
and "-- not-null-ignore" not in sql
):
print(
f"\n\n\033[91mFound a non-null field or default added to an existing model. This will lock up the table while migrating. Please add 'null=True, blank=True' to the field"
)
sys.exit(1)

if "RENAME" in sql and "-- rename-ignore" not in sql:
print(
f"\n\n\033[91mFound a rename command. This will lock up the table while migrating. Please create a new column and provide alternative method for swapping columns"
)
sys.exit(1)

if "DROP COLUMN" in sql and "-- drop-column-ignore" not in sql:
print(
f"\n\n\033[91mFound a drop command. This could lead to unsafe states for the app. Please avoid dropping columns"
)
sys.exit(1)
operations = sql.split("\n")
for operation_sql in operations:
if (
re.findall(r"(?<!DROP) (NOT NULL|DEFAULT)", operation_sql, re.M & re.I)
and "CREATE TABLE" not in operation_sql
and "-- not-null-ignore" not in operation_sql
):
print(
f"\n\n\033[91mFound a non-null field or default added to an existing model. This will lock up the table while migrating. Please add 'null=True, blank=True' to the field.\nSource: `{operation_sql}`"
)
sys.exit(1)

if "RENAME" in operation_sql and "-- rename-ignore" not in operation_sql:
print(
f"\n\n\033[91mFound a rename command. This will lock up the table while migrating. Please create a new column and provide alternative method for swapping columns.\nSource: `{operation_sql}`"
)
sys.exit(1)

if "DROP COLUMN" in operation_sql and "-- drop-column-ignore" not in operation_sql:
print(
f"\n\n\033[91mFound a drop command. This could lead to unsafe states for the app. Please avoid dropping columns.\nSource: `{operation_sql}`"
)
sys.exit(1)

if "DROP TABLE" in operation_sql:
print(
f"\n\n\033[91mFound a DROP TABLE command. This could lead to unsafe states for the app. Please avoid dropping tables.\nSource: `{operation_sql}`"
)
sys.exit(1)

if "CONSTRAINT" in operation_sql and "CREATE TABLE" not in operation_sql:
print(
f"\n\n\033[91mFound a CONSTRAINT command. This locks tables which causes downtime. Please avoid adding constraints to existing tables.\nSource: `{operation_sql}`"
)
sys.exit(1)

except (IndexError, CommandError):
pass

Expand Down