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..1ebb5be4 --- /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, 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 = [ + # 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..ae8a8f68 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-]+(?:, ?[\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..68aef3ed --- /dev/null +++ b/scripts/ci/squawk_migrations.sh @@ -0,0 +1,51 @@ +#!/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 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. +# +# 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 + +# 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." diff --git a/scripts/ci/suppressions.txt b/scripts/ci/suppressions.txt index a7fede3e..f29fed3f 100644 --- a/scripts/ci/suppressions.txt +++ b/scripts/ci/suppressions.txt @@ -9,6 +9,8 @@ .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 +.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.