ci(migrations): guard against editing already-committed migrations #415
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/DEV-395-migration-immutability-guard"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Implements DEV-395. Prevents the migration-checksum break that blocked the v0.4.0 deploy on nc-01.
Problem
sqlx stores a SHA-384 checksum of each migration in
_sqlx_migrationson apply and re-verifies on every startup. Editing a migration already applied to any DB makes it refuse to boot:migration N was previously applied but has been modified. That is exactly what happened:023_seed_data.sqlwas edited (seed cleanup, commit398c0ba) after a build had applied the earlier version, so v0.4.0 could not start on prod.Guard
scripts/check-migration-immutability.nu(nushell, mirrors the existingcheck-migration-prefixes.nu):migrations/against the merge-base withorigin/main(git diff --diff-filter=MRD --name-only origin/main...HEAD -- migrations/).mainwas Modified, Renamed, or Deleted, listing the file(s) and telling the author to add a new migration instead.Wired into
.forgejo/workflows/check.ymlnext to the prefix check, withfetch-depth: 0on the checkout so the merge-base is available.Verified locally
023_seed_data.sql-> fail (exit 1), names the file.Notes
mainthat edit a migration are not caught byorigin/main...HEAD(merge-base equals HEAD post-push); main should stay PR-gated.check-migration-versions.shwith the same content-immutability check (it currently only enforces version-number uniqueness).sqlx records a SHA-384 checksum of each migration in `_sqlx_migrations` when it applies it and re-verifies on every startup, so editing a migration that has already been applied to any database makes that database refuse to boot ("migration N was previously applied but has been modified"). This is exactly how the v0.4.0 deploy broke nc-01: `023_seed_data.sql` was edited (a routine seed cleanup) after a build had already applied the earlier version. Add `scripts/check-migration-immutability.nu`: it diffs `migrations/` against the merge-base with `origin/main` (`origin/main...HEAD`, `--diff-filter=MRD`) and fails if any migration file already present on `main` was modified, renamed, or deleted. Adding a new migration is always allowed. It fails loud (exit 2) if the diff cannot run, so a broken base can never read as "nothing changed". Wired into `.forgejo/workflows/check.yml` next to the existing prefix-uniqueness check, with `fetch-depth: 0` on the checkout so the merge-base is available. Documents the immutability rule in CLAUDE.md. #DEV-395 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: nrupard <natrsmith11@gmail.com>