diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 35c3f1e4..b3912a45 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -141,15 +141,31 @@ jobs: persist-credentials: false fetch-depth: 0 - run: scripts/ci/cli_version.sh + + # The migrations a change adds, against its base (a required check: main's ruleset makes a pull request up to date + # with main first, so two branches' migrations are always checked against each other). + migrations: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + persist-credentials: false + fetch-depth: 0 # 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 + # 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. - env: BASE: ${{ github.event.pull_request.base.sha || github.event.before }} run: | - if [ -z "$BASE" ] || [ "$BASE" = 0000000000000000000000000000000000000000 ]; then - echo "No earlier commit to compare with." + if [ -z "$BASE" ] || [ "$BASE" = 0000000000000000000000000000000000000000 ] || ! git cat-file -e "$BASE^{commit}" 2>/dev/null; then + echo "::warning::No earlier commit to compare the migrations with (${BASE:-none}): only their names are checked." + scripts/ci/check_migrations.sh "" else + scripts/ci/check_migrations.sh "$BASE" python3 scripts/ci/migration_safety.py "$BASE" fi diff --git a/AGENTS.md b/AGENTS.md index be1727ba..5f794b30 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -96,10 +96,15 @@ design is in `docs/design.md`; read it before changing the architecture, and upd - Migrations run before the rollout, so for a few seconds the previous release serves against the new schema. A migration must work with the code before it: add the new column and copy the data in one change, stop reading the old one in the same change, and drop or rename it in a later change. A required column gets a default. CI's - `cli-version` job runs scripts/ci/migration_safety.py, which fails a new migration that drops, renames or retypes + `migrations` job runs scripts/ci/migration_safety.py, which fails a new migration that drops, renames or retypes anything main's code still names. This is deploy safety, not backwards compatibility: the old column still goes, one release later. (4 Dec 2026, a dropped organizations column failed every request for ~20 s, a new user's first build among them.) +- Migrations run in file-name order and the database records each by name. Name a new one + `YYYY_MM_DD_HHMMSS_snake_case.rs` with a timestamp after every migration on main, and never rename or delete one that + 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. - 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/scripts/ci/check_migrations.sh b/scripts/ci/check_migrations.sh new file mode 100755 index 00000000..b059f61b --- /dev/null +++ b/scripts/ci/check_migrations.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# app/'s migrations, checked before they reach a deployed database (ported from fleetlayer's scripts/check-migrations, +# sc-9611). Migrations run in name order and a database records each one by name, so: +# - each is named YYYY_MM_DD_HHMMSS_snake_case.rs; +# - against a base commit (CI gives the push's or the pull request's base; default origin/main): every migration the +# base has is still there under the same name and unchanged (deployed databases have applied it, and never run it +# again: a fix is a new migration), and every new one sorts after +# all of them. Two sessions that each add a migration on their own branch: whoever rebases second renames theirs to +# a later timestamp, or it would run before one production already applied. +# +# scripts/ci/check_migrations.sh [] +set -euo pipefail +# Byte order, as the migrations macro sorts them (a locale's collation can order `_ab` and `_a_c` the other way). +export LC_ALL=C +cd "$(git rev-parse --show-toplevel)" +dir=app/database/migrations +base=${1-origin/main} +pattern='^[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{6}_[a-z0-9_]+\.rs$' + +now=$(find "$dir" -maxdepth 1 -type f -name '*.rs' -printf '%f\n' | LC_ALL=C sort) +bad=$(grep -vE "$pattern" <<<"$now" || true) +if [ -n "$bad" ]; then + echo "check_migrations: not named YYYY_MM_DD_HHMMSS_snake_case.rs: $bad" >&2 + exit 1 +fi + +if [ -z "$base" ] || ! git cat-file -e "$base^{commit}" 2>/dev/null; then + echo "check_migrations: $(wc -l <<<"$now") migrations, all named right (no base to compare with)." + exit 0 +fi +before=$(git ls-tree --name-only "$base" "$dir/" | xargs -rn1 basename | grep -E "$pattern" | LC_ALL=C sort || true) +while IFS= read -r f; do + [ -n "$f" ] || continue + if ! grep -qxF "$f" <<<"$now"; then + echo "check_migrations: $f is on $base already, so deployed databases may have applied it: never rename or remove it" >&2 + exit 1 + fi +done <<<"$before" +changed=$(git diff --name-only --diff-filter=M "$base" -- "$dir/" | xargs -rn1 basename) +if [ -n "$changed" ]; then + echo "check_migrations: changed since $base, which deployed databases already ran and won't run again: $changed. Put the change in a new migration." >&2 + exit 1 +fi +last=$(tail -n1 <<<"$before") +new=0 +while IFS= read -r f; do + grep -qxF "$f" <<<"$before" && continue + new=$((new + 1)) + if [ -n "$last" ] && [[ ! "$f" > "$last" ]]; then + echo "check_migrations: $f is new, but $base already has $last, which sorts after it: give it a later timestamp" >&2 + exit 1 + fi +done <<<"$now" +echo "check_migrations: $(wc -l <<<"$now") migrations, named right; $new new since $base, all after its last ($last)."