Skip to content

Commit fc3f296

Browse files
committed
CI: a new migration mustn't drop, rename or retype what the release still serving reads, or add a required column with no default; AGENTS.md says why (the 4 Dec first-build 500)
1 parent 5add582 commit fc3f296

6 files changed

Lines changed: 190 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ jobs:
120120
with:
121121
fetch-depth: 0
122122
- run: scripts/ci/cli_version.sh
123+
# Migrations run before the rollout, so the release still serving must work with the new schema: a new migration
124+
# mustn't drop, rename or retype what main's code reads (4 Dec 2026: a dropped column failed requests for ~20 s).
125+
- run: python3 -m unittest scripts/ci/test_migration_safety.py
126+
- env:
127+
BASE: ${{ github.event.pull_request.base.sha || github.event.before }}
128+
run: |
129+
if [ -z "$BASE" ] || [ "$BASE" = 0000000000000000000000000000000000000000 ]; then
130+
echo "No earlier commit to compare with."
131+
else
132+
python3 scripts/ci/migration_safety.py "$BASE"
133+
fi
123134
124135
# The CircleCI orb's scripts (clients/circleci-orb), rendered and linted: validating it with CircleCI needs a token.
125136
orb:

‎AGENTS.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ design is in `docs/design.md`; read it before changing the architecture, and upd
6363
- A test never skips quietly. When a service it needs (the blob store, Postgres, a forge stub) is missing, it fails
6464
with a message naming the service: a test that returns early passes in CI. (3 Dec 2026: the server tests that need
6565
blobs returned early in CI's `test` job, which runs no S3, and passed without checking anything.)
66+
- Migrations run before the rollout, so for a few seconds the previous release serves against the new schema. A
67+
migration must work with the code before it: add the new column and copy the data in one change, stop reading the
68+
old one in the same change, and drop or rename it in a later change. A required column gets a default. CI's
69+
`cli-version` job runs scripts/ci/migration_safety.py, which fails a new migration that drops, renames or retypes
70+
anything main's code still names. This is deploy safety, not backwards compatibility: the old column still goes, one
71+
release later. (4 Dec 2026, a dropped organizations column failed every request for ~20 s, a new user's first build
72+
among them.)
6673
- Every background process (workers, renderers, scheduled jobs) ships with a health check or heartbeat that checks
6774
the work is done, not just that the process is up, in the same change.
6875
- Production reads its secrets from 1Password through the cluster's ExternalSecrets (talira-ai/infra,
7.65 KB
Binary file not shown.
4.08 KB
Binary file not shown.

‎scripts/ci/migration_safety.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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]))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Tests for migration_safety.py: python3 -m unittest scripts/ci/test_migration_safety.py"""
2+
3+
import os
4+
import sys
5+
import unittest
6+
7+
sys.path.insert(0, os.path.dirname(__file__))
8+
from migration_safety import allowed, removed_names # noqa: E402
9+
10+
11+
def names(sql):
12+
return [(kind, name) for kind, name, _ in removed_names(sql)]
13+
14+
15+
class RemovedNames(unittest.TestCase):
16+
def test_the_4_dec_migration_drops_the_column_the_old_release_read(self):
17+
sql = """
18+
ALTER TABLE organizations ADD COLUMN setup_shown BOOLEAN;
19+
UPDATE organizations SET setup_shown = false WHERE setup_dismissed_at IS NOT NULL;
20+
ALTER TABLE organizations DROP COLUMN setup_dismissed_at;
21+
"""
22+
self.assertEqual(names(sql), [("column", "setup_dismissed_at")])
23+
24+
def test_drops_renames_and_retypes(self):
25+
sql = """
26+
ALTER TABLE builds DROP COLUMN IF EXISTS a, DROP COLUMN "b";
27+
ALTER TABLE builds RENAME COLUMN c TO d;
28+
ALTER TABLE builds RENAME e TO f;
29+
ALTER TABLE builds ALTER COLUMN g TYPE BIGINT, ALTER h SET DATA TYPE TEXT;
30+
ALTER TABLE old_things RENAME TO new_things;
31+
DROP TABLE IF EXISTS t1, t2 CASCADE;
32+
"""
33+
self.assertEqual(
34+
names(sql),
35+
[("column", "a"), ("column", "b"), ("column", "c"), ("column", "e"), ("column", "g"), ("column", "h"),
36+
("table", "old_things"), ("table", "t1"), ("table", "t2")],
37+
)
38+
39+
def test_additions_constraints_defaults_and_comments_are_safe(self):
40+
sql = """
41+
-- ALTER TABLE x DROP COLUMN y;
42+
/* DROP TABLE z; */
43+
ALTER TABLE builds ADD COLUMN base_branch TEXT;
44+
ALTER TABLE builds DROP CONSTRAINT builds_check, ALTER COLUMN x DROP DEFAULT, ALTER COLUMN y DROP NOT NULL;
45+
ALTER TABLE builds RENAME CONSTRAINT a TO b;
46+
CREATE INDEX i ON builds (x);
47+
DROP INDEX IF EXISTS i;
48+
"""
49+
self.assertEqual(names(sql), [])
50+
51+
def test_a_required_column_without_a_default_breaks_the_old_inserts(self):
52+
sql = """
53+
ALTER TABLE builds ADD COLUMN a TEXT NOT NULL, ADD COLUMN b INT NOT NULL DEFAULT 0, ADD COLUMN c TEXT;
54+
ALTER TABLE organizations ADD COLUMN t DOUBLE PRECISION NOT NULL DEFAULT 0 CHECK (t >= 0 AND t <= 1);
55+
"""
56+
self.assertEqual(names(sql), [("table (a required column with no default added to it)", "builds")])
57+
58+
def test_a_contract_line_lets_a_name_through(self):
59+
self.assertEqual(allowed("-- contract: state: the old code's `state` is builds.state, not this table's\n"), {"state"})
60+
61+
62+
if __name__ == "__main__":
63+
unittest.main()

0 commit comments

Comments
 (0)