|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fail when a new migration drops, renames or retypes a column or table that the release still serving reads. |
| 3 | +
|
| 4 | +ArgoCD runs migrations before the rollout, so for some seconds the previous release's pods serve against the new |
| 5 | +schema. On 4 Dec 2026 a migration dropped organizations.setup_dismissed_at while those pods still selected it: every |
| 6 | +request that loaded an organization answered 500 for about 20 s, a new user's first build among them. So a migration |
| 7 | +must work with the code before it (expand, then contract): add the new column and copy the data in one release, stop |
| 8 | +reading the old one, and drop it in a later release, once the code on main no longer names it. |
| 9 | +
|
| 10 | +Usage: migration_safety.py <base-ref> (the commit the release serving was built from: a pull request's base, or the |
| 11 | +previous main). Checks the migrations added since <base-ref> under app/database/migrations against app/src at |
| 12 | +<base-ref>. A name the old code uses for something else can be let through with a line in the migration: |
| 13 | +`-- contract: <name>: <why the release serving doesn't read it>`. |
| 14 | +""" |
| 15 | + |
| 16 | +import re |
| 17 | +import subprocess |
| 18 | +import sys |
| 19 | + |
| 20 | +MIGRATIONS = "app/database/migrations/" |
| 21 | +CODE = "app/src" |
| 22 | + |
| 23 | +IDENT = r'"?([A-Za-z_][A-Za-z0-9_]*)"?' |
| 24 | + |
| 25 | + |
| 26 | +def strip_comments(sql): |
| 27 | + sql = re.sub(r"/\*.*?\*/", " ", sql, flags=re.S) |
| 28 | + return re.sub(r"--[^\n]*", " ", sql) |
| 29 | + |
| 30 | + |
| 31 | +def removed_names(source): |
| 32 | + """(kind, name, statement) for each column or table the migration drops, renames or retypes.""" |
| 33 | + out = [] |
| 34 | + for statement in strip_comments(source).split(";"): |
| 35 | + s = " ".join(statement.split()) |
| 36 | + table = re.match(r"(?i)ALTER TABLE (?:IF EXISTS )?(?:ONLY )?" + IDENT, s) |
| 37 | + if table: |
| 38 | + for m in re.finditer(r"(?i)\bDROP (?:COLUMN )?(?:IF EXISTS )?" + IDENT, s): |
| 39 | + if m.group(1).upper() in ("CONSTRAINT", "DEFAULT", "NOT", "INDEX"): |
| 40 | + continue |
| 41 | + out.append(("column", m.group(1), s)) |
| 42 | + for m in re.finditer(r"(?i)\bRENAME (?:COLUMN )?" + IDENT + r" TO\b", s): |
| 43 | + if m.group(1).upper() in ("CONSTRAINT", "TO"): |
| 44 | + continue |
| 45 | + out.append(("column", m.group(1), s)) |
| 46 | + for m in re.finditer(r"(?i)\bALTER (?:COLUMN )?" + IDENT + r" (?:SET DATA )?TYPE\b", s): |
| 47 | + out.append(("column", m.group(1), s)) |
| 48 | + if re.search(r"(?i)\bRENAME TO\b", s): |
| 49 | + out.append(("table", table.group(1), s)) |
| 50 | + # A required column with no default: the old release's inserts into the table leave it out and fail. |
| 51 | + for m in re.finditer(r"(?i)\bADD (?:COLUMN )?(?:IF NOT EXISTS )?" + IDENT + r"([^,]*)", s): |
| 52 | + spec = m.group(2).upper() |
| 53 | + if re.search(r"\bNOT NULL\b", spec) and not re.search(r"\bDEFAULT\b|\bGENERATED\b", spec): |
| 54 | + out.append(("table (a required column with no default added to it)", table.group(1), s)) |
| 55 | + drop = re.match(r"(?i)DROP TABLE (?:IF EXISTS )?(.+?)(?: CASCADE| RESTRICT)?$", s) |
| 56 | + if drop: |
| 57 | + for name in drop.group(1).split(","): |
| 58 | + m = re.fullmatch(IDENT, name.strip()) |
| 59 | + if m: |
| 60 | + out.append(("table", m.group(1), s)) |
| 61 | + return out |
| 62 | + |
| 63 | + |
| 64 | +def allowed(source): |
| 65 | + return {m.group(1) for m in re.finditer(r"--\s*contract:\s*" + IDENT, source)} |
| 66 | + |
| 67 | + |
| 68 | +def run(*args): |
| 69 | + return subprocess.run(args, check=True, capture_output=True, text=True).stdout |
| 70 | + |
| 71 | + |
| 72 | +def still_read(base, name): |
| 73 | + """Whether app/src at `base` names `name` as a whole word.""" |
| 74 | + found = subprocess.run(["git", "grep", "-q", "-w", "-e", name, base, "--", CODE], capture_output=True) |
| 75 | + return found.returncode == 0 |
| 76 | + |
| 77 | + |
| 78 | +def main(base): |
| 79 | + added = [ |
| 80 | + line.split("\t", 1)[1] |
| 81 | + for line in run("git", "diff", "--name-status", "--diff-filter=A", f"{base}...HEAD", "--", MIGRATIONS).splitlines() |
| 82 | + if line.endswith(".rs") |
| 83 | + ] |
| 84 | + problems = [] |
| 85 | + for path in added: |
| 86 | + source = open(path).read() |
| 87 | + ok = allowed(source) |
| 88 | + for kind, name, statement in removed_names(source): |
| 89 | + if name in ok or not still_read(base, name): |
| 90 | + continue |
| 91 | + problems.append(f"{path}: drops or changes {kind} `{name}`, which {CODE} still reads at {base[:12]}:\n {statement}") |
| 92 | + if problems: |
| 93 | + print("\n".join(problems), file=sys.stderr) |
| 94 | + print( |
| 95 | + "\nMigrations run before the rollout, so the release serving must still work afterwards. Add the new\n" |
| 96 | + "column and copy the data now, stop reading the old one in this change, and drop it in a later change\n" |
| 97 | + "(once main's code doesn't name it). If the old code uses the name for something else, add\n" |
| 98 | + "`-- contract: <name>: <why>` to the migration.", |
| 99 | + file=sys.stderr, |
| 100 | + ) |
| 101 | + return 1 |
| 102 | + print(f"{len(added)} new migration(s); none drops or changes anything the release serving reads.") |
| 103 | + return 0 |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + if len(sys.argv) != 2: |
| 108 | + sys.exit(__doc__) |
| 109 | + sys.exit(main(sys.argv[1])) |
0 commit comments