From b6440584a066eba46cb341ff5263c3ac4a3340fa Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sat, 5 Dec 2026 05:52:24 +0000 Subject: [PATCH 1/4] Migrations: Squawk lints each new migration's SQL (lock_timeout, NOT VALID foreign keys), in the hook and the migrations job (sc-9611) --- .github/workflows/ci.yml | 5 +++++ .squawk.toml | 16 ++++++++++++++++ AGENTS.md | 4 ++++ lefthook.yml | 4 ++++ mise.toml | 1 + scripts/ci/check_suppressions.py | 1 + scripts/ci/squawk_migrations.sh | 33 ++++++++++++++++++++++++++++++++ 7 files changed, 64 insertions(+) create mode 100644 .squawk.toml create mode 100755 scripts/ci/squawk_migrations.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14a8a1fe..3921d2a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,6 +155,9 @@ jobs: # Migrations run before the rollout, so the release still serving must work with the new schema: a new migration # mustn't drop, rename or retype what main's code reads (4 Dec 2026: a dropped column failed requests for ~20 s). - run: python3 -m unittest scripts/ci/test_migration_safety.py + - uses: jdx/mise-action@924e7c586e33d7d4bae9c14e26c5d22e2beb6e78 # v5.3.0 + with: + install_args: github:sbdchd/squawk # Migrations run in file-name order and are recorded by name, so one already on main is never renamed or edited, # and a new one sorts after all of main's (two branches that each add one: the second to merge takes a later # timestamp). With no base it can reach (a new branch, a force-push), only the names are checked. @@ -167,6 +170,8 @@ jobs: else scripts/ci/check_migrations.sh "$BASE" python3 scripts/ci/migration_safety.py "$BASE" + # Postgres migration lint on the new ones' SQL (.squawk.toml): locks that block traffic, unsafe changes. + scripts/ci/squawk_migrations.sh "$BASE" fi # The CircleCI orb's scripts (clients/circleci-orb), rendered and linted: validating it with CircleCI needs a token. diff --git a/.squawk.toml b/.squawk.toml new file mode 100644 index 00000000..26cf97a8 --- /dev/null +++ b/.squawk.toml @@ -0,0 +1,16 @@ +# Squawk (scripts/ci/squawk_migrations.sh): Postgres migration lint on the SQL of each new migration in +# app/database/migrations, in CI's migrations job. Every rule applies except these, each with its reason. +pg_version = "17.0" +# laravel-rust runs each migration in a transaction. +assume_in_transaction = true +excluded_rules = [ + # CREATE INDEX CONCURRENTLY can't run in a transaction, and every migration runs in one; lock_timeout bounds the wait. + "require-concurrent-index-creation", + # scripts/ci/migration_safety.py owns dropped columns: it fails one main's code still reads and allows the rest. + "ban-drop-column", + # BIGSERIAL keys and integer counters are the schema's convention since the initial migration. + "prefer-identity", + "prefer-bigint-over-int", + # A data backfill may legitimately run long; lock_timeout (required) is what keeps a migration from blocking traffic. + "require-statement-timeout", +] diff --git a/AGENTS.md b/AGENTS.md index 301eda0d..b23b8868 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -119,6 +119,10 @@ design is in `docs/design.md`; read it before changing the architecture, and upd is on main. If another branch's migration merged first with a later timestamp, rename yours to a newer one before merging. A migration on main is never edited either (deployed databases won't run it again): fix it with a new one. scripts/ci/check_migrations.sh checks all of this in CI's `migrations` job. +- **New migrations are linted by Squawk** (`.squawk.toml`, `scripts/ci/squawk_migrations.sh`, in the hook and the + `migrations` job): start each with `SET LOCAL lock_timeout = '5s';`, and add a foreign key to an existing table + `NOT VALID`, then `VALIDATE` it in a later migration. Silence a finding only with `-- squawk-ignore ` above the + statement, listed with its reason in `scripts/ci/suppressions.txt`. - The server connects with sqlx's prepared-statement cache off (`config::without_statement_cache`): a migration that adds a column makes Postgres refuse an old pod's cached `SELECT table.*` ("cached plan must not change result type"). Keep it off. `tests/rollouts.rs` fails if it comes back on. (4 Dec 2026, #178's rollout.) diff --git a/lefthook.yml b/lefthook.yml index de4bea90..9fb2db33 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -31,6 +31,10 @@ pre-commit: run: scripts/ci/check_file_length.py {staged_files} suppressions: run: scripts/ci/check_suppressions.py + # Squawk on staged migrations' SQL (.squawk.toml). + squawk: + glob: "app/database/migrations/*.rs" + run: scripts/ci/squawk_migrations.sh --files {staged_files} # The workflows and actions: actionlint (with shellcheck on their scripts) and zizmor (.github/zizmor.yml). actionlint: glob: ".github/workflows/*.{yml,yaml}" diff --git a/mise.toml b/mise.toml index 9e0ea562..ef41ef14 100644 --- a/mise.toml +++ b/mise.toml @@ -13,3 +13,4 @@ hadolint = "2.15.1" typos = "1.53.0" oxlint = "1.96.0" lychee = "0.24.2" +"github:sbdchd/squawk" = "2.74.0" diff --git a/scripts/ci/check_suppressions.py b/scripts/ci/check_suppressions.py index 4aa8cc3a..2020d9e0 100755 --- a/scripts/ci/check_suppressions.py +++ b/scripts/ci/check_suppressions.py @@ -36,6 +36,7 @@ r"hadolint ignore=[\w,]+", r"--ignore DL\d{4}", r"nosemgrep", + r"squawk-ignore[ \w,-]*", r"(?:gitleaks|betterleaks):allow", ] # Case-insensitive: `# NOQA` silences as well as `# noqa`. diff --git a/scripts/ci/squawk_migrations.sh b/scripts/ci/squawk_migrations.sh new file mode 100755 index 00000000..e51c3155 --- /dev/null +++ b/scripts/ci/squawk_migrations.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# Squawk (.squawk.toml) on the SQL of each migration added since a base commit, or of the migrations named. The SQL +# lives in Rust raw strings (`r"..."`, `r#"..."#`), so each file's strings are copied into a .sql file first. Old +# migrations aren't checked: production ran them long ago. Silence one finding with `-- squawk-ignore ` above the +# statement, listed with its reason in scripts/ci/suppressions.txt. +# +# scripts/ci/squawk_migrations.sh +# scripts/ci/squawk_migrations.sh --files ... +set -euo pipefail +cd "$(git rev-parse --show-toplevel)" +if [ "${1-}" = --files ]; then + shift + files=("$@") +else + mapfile -t files < <(git diff --name-only --diff-filter=A "${1:?base commit}" -- 'app/database/migrations/*.rs') +fi +if [ ${#files[@]} -eq 0 ]; then + echo "Squawk: no new migrations." + exit 0 +fi +out=$(mktemp -d) +trap 'rm -rf "$out"' EXIT +python3 - "$out" "${files[@]}" <<'PY' +import os, re, sys +out = sys.argv[1] +for path in sys.argv[2:]: + source = open(path).read() + parts = [m.group(2) for m in re.finditer(r'\br(#*)"(.*?)"\1', source, re.S)] + sql = "\n".join(p if p.rstrip().endswith(";") else p.rstrip() + ";" for p in parts if p.strip()) + open(os.path.join(out, os.path.basename(path)[:-3] + ".sql"), "w").write(sql + "\n") +PY +squawk --config .squawk.toml "$out"/*.sql +echo "Squawk: ${#files[@]} new migrations clean." From 8bcae24487a5a8e979d4035bde39760a435d1ef2 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sat, 5 Dec 2026 05:56:31 +0000 Subject: [PATCH 2/4] Suppressions: squawk-ignore's pattern stops at the rule names; the Squawk script's usage comment listed --- scripts/ci/check_suppressions.py | 2 +- scripts/ci/suppressions.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/ci/check_suppressions.py b/scripts/ci/check_suppressions.py index 2020d9e0..ae8a8f68 100755 --- a/scripts/ci/check_suppressions.py +++ b/scripts/ci/check_suppressions.py @@ -36,7 +36,7 @@ r"hadolint ignore=[\w,]+", r"--ignore DL\d{4}", r"nosemgrep", - r"squawk-ignore[ \w,-]*", + r"squawk-ignore(?: [\w-]+(?:, ?[\w-]+)*)?", r"(?:gitleaks|betterleaks):allow", ] # Case-insensitive: `# NOQA` silences as well as `# noqa`. diff --git a/scripts/ci/suppressions.txt b/scripts/ci/suppressions.txt index a7fede3e..039f89cc 100644 --- a/scripts/ci/suppressions.txt +++ b/scripts/ci/suppressions.txt @@ -9,6 +9,7 @@ .github/workflows/release-gallery-python.yml zizmor: ignore[cache-poisoning] -- setup-node there sets no cache; zizmor flags the action itself .github/workflows/release.yml zizmor: ignore[dangerous-triggers] -- workflow_run is how the release waits for CI; every job checks for a successful CI run of a push to main .github/zizmor.yml zizmor: ignore[audit] -- not a suppression: the file's comment showing the syntax +scripts/ci/squawk_migrations.sh squawk-ignore -- not a suppression: the script's comment showing the syntax clients/github-action/action.yml zizmor: ignore[github-env] -- GITHUB_PATH gets a directory under RUNNER_TEMP named from the workflow author's own inputs # docker/renderer.Dockerfile's two hadolint warnings wait for the renderer's next rebuild (sc-9674): any edit to that file rebuilds the renderer image. From cdfc88fdf41d650bb5e4e777ce2627e9bde05a2f Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sat, 5 Dec 2026 06:05:05 +0000 Subject: [PATCH 3/4] Squawk: plain strings passed to tx.query are read too, a migration with no readable SQL fails, and require-concurrent-index-creation is back on (review) --- .squawk.toml | 6 +++--- scripts/ci/squawk_migrations.sh | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.squawk.toml b/.squawk.toml index 26cf97a8..1ebb5be4 100644 --- a/.squawk.toml +++ b/.squawk.toml @@ -1,11 +1,11 @@ # Squawk (scripts/ci/squawk_migrations.sh): Postgres migration lint on the SQL of each new migration in # app/database/migrations, in CI's migrations job. Every rule applies except these, each with its reason. pg_version = "17.0" -# laravel-rust runs each migration in a transaction. +# laravel-rust runs each migration in a transaction, so CREATE INDEX CONCURRENTLY isn't possible: an index on an existing +# table blocks its writes while it builds. On a small table, silence require-concurrent-index-creation with a listed +# `-- squawk-ignore` and the table's size as the reason. assume_in_transaction = true excluded_rules = [ - # CREATE INDEX CONCURRENTLY can't run in a transaction, and every migration runs in one; lock_timeout bounds the wait. - "require-concurrent-index-creation", # scripts/ci/migration_safety.py owns dropped columns: it fails one main's code still reads and allows the rest. "ban-drop-column", # BIGSERIAL keys and integer counters are the schema's convention since the initial migration. diff --git a/scripts/ci/squawk_migrations.sh b/scripts/ci/squawk_migrations.sh index e51c3155..68aef3ed 100755 --- a/scripts/ci/squawk_migrations.sh +++ b/scripts/ci/squawk_migrations.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # Squawk (.squawk.toml) on the SQL of each migration added since a base commit, or of the migrations named. The SQL -# lives in Rust raw strings (`r"..."`, `r#"..."#`), so each file's strings are copied into a .sql file first. Old +# lives in Rust strings (raw-string consts, or literals passed straight to tx.query), so each file's are copied into a +# .sql file first; a migration whose SQL can't be found that way (a format! string) fails. Old # migrations aren't checked: production ran them long ago. Silence one finding with `-- squawk-ignore ` above the # statement, listed with its reason in scripts/ci/suppressions.txt. # @@ -22,12 +23,29 @@ out=$(mktemp -d) trap 'rm -rf "$out"' EXIT python3 - "$out" "${files[@]}" <<'PY' import os, re, sys -out = sys.argv[1] + +# Rust string escapes in a plain "..." literal: a backslash-newline drops the newline and the next line's indent. +ESCAPES = {"n": "\n", "t": "\t", "r": "\r", "0": "\0", "\\": "\\", '"': '"', "'": "'"} + + +def plain(body): + body = re.sub(r"\\\n\s*", "", body) + return re.sub(r"\\(.)", lambda m: ESCAPES.get(m.group(1), m.group(0)), body) + + +out, failed = sys.argv[1], False for path in sys.argv[2:]: source = open(path).read() + # Raw strings anywhere (the SQL consts), and plain strings passed straight to .query(...). parts = [m.group(2) for m in re.finditer(r'\br(#*)"(.*?)"\1', source, re.S)] + parts += [plain(m.group(1)) for m in re.finditer(r'\.query\(\s*"((?:[^"\\]|\\.)*)"', source, re.S)] sql = "\n".join(p if p.rstrip().endswith(";") else p.rstrip() + ";" for p in parts if p.strip()) + if not sql.strip(): + print(f"{path}: no SQL found. Write it as a literal string, in a raw-string const or passed straight to " + "tx.query(...), so Squawk can read it (a format! string can't be checked).", file=sys.stderr) + failed = True open(os.path.join(out, os.path.basename(path)[:-3] + ".sql"), "w").write(sql + "\n") +sys.exit(1 if failed else 0) PY squawk --config .squawk.toml "$out"/*.sql echo "Squawk: ${#files[@]} new migrations clean." From d085e93c5f7acb846c99de576022aa9ee13840ee Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sat, 5 Dec 2026 06:10:18 +0000 Subject: [PATCH 4/4] Suppressions: list .squawk.toml's comment about squawk-ignore (it shows the syntax, it silences nothing) --- scripts/ci/suppressions.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/ci/suppressions.txt b/scripts/ci/suppressions.txt index 039f89cc..f29fed3f 100644 --- a/scripts/ci/suppressions.txt +++ b/scripts/ci/suppressions.txt @@ -10,6 +10,7 @@ .github/workflows/release.yml zizmor: ignore[dangerous-triggers] -- workflow_run is how the release waits for CI; every job checks for a successful CI run of a push to main .github/zizmor.yml zizmor: ignore[audit] -- not a suppression: the file's comment showing the syntax scripts/ci/squawk_migrations.sh squawk-ignore -- not a suppression: the script's comment showing the syntax +.squawk.toml squawk-ignore -- not a suppression: the comment telling a migration how to write one clients/github-action/action.yml zizmor: ignore[github-env] -- GITHUB_PATH gets a directory under RUNNER_TEMP named from the workflow author's own inputs # docker/renderer.Dockerfile's two hadolint warnings wait for the renderer's next rebuild (sc-9674): any edit to that file rebuilds the renderer image.