From f3b9f6f688e1aebf3999f622facb8b484f486c3a Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:10:57 +0100 Subject: [PATCH] db: refuse a pending migration older than the newest applied one applyMigrations now fails closed, before any write, when a pending migration has a version below the highest version recorded in schema_migrations: migrations must be appended, never inserted. --- db/migrate.go | 15 ++++++++++++- db/migrate_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/db/migrate.go b/db/migrate.go index e6ed780..a86d1d1 100644 --- a/db/migrate.go +++ b/db/migrate.go @@ -98,7 +98,8 @@ func loadMigrations(fsys fs.FS) ([]migration, error) { // // It fails closed before applying anything when the database records a version // this binary does not ship, or an applied migration whose name or checksum -// differs from the file. Each pending migration then runs in its own +// differs from the file, or a pending migration is older than the newest +// applied one. Each pending migration then runs in its own // transaction together with its schema_migrations row; a failure rolls that one // back and stops, leaving earlier migrations applied. func applyMigrations(ctx context.Context, conn *sql.Conn, fsys fs.FS) error { @@ -152,6 +153,18 @@ func applyMigrations(ctx context.Context, conn *sql.Conn, fsys fs.FS) error { } } + // A pending version below the newest applied one would run out of order on + // this database: refuse it before writing anything. + if len(appliedVersions) > 0 { + maxVersion := appliedVersions[len(appliedVersions)-1] + for _, m := range migrations { + if _, done := applied[m.version]; !done && m.version < maxVersion { + return fmt.Errorf("migration %s (%04d) is older than the applied migration %s (%04d); migrations must be appended, never inserted", + m.name, m.version, applied[maxVersion].name, maxVersion) + } + } + } + for _, m := range migrations { if _, done := applied[m.version]; done { continue diff --git a/db/migrate_test.go b/db/migrate_test.go index 76b695a..0106c2e 100644 --- a/db/migrate_test.go +++ b/db/migrate_test.go @@ -358,3 +358,58 @@ SELECT 1/0; t.Error("migration_broken exists: the failed migration was not rolled back") } } + +// A database already at 0003 must refuse a binary that adds 0002 below it, +// before writing anything. +func TestMigrationsRefuseOutOfOrderVersion(t *testing.T) { + dsn := scratchDSN(t, "outoforder") + records, err := fs.ReadFile(migrationsFS(), llmRecordsMigration) + if err != nil { + t.Fatal(err) + } + d, err := openWithMigrations(dsn, fstest.MapFS{ + llmRecordsMigration: {Data: records}, + "0003_probe.sql": probeMigration, + }) + if err != nil { + t.Fatal(err) + } + before := readApplied(t, d) + d.Close() + if len(before) != 2 || before[0].version != 1 || before[1].version != 3 { + t.Fatalf("setup schema_migrations = %+v, want versions 1 and 3", before) + } + + d, err = openWithMigrations(dsn, withEmbedded(t, fstest.MapFS{"0003_probe.sql": probeMigration})) + if err == nil { + d.Close() + t.Fatal("Open succeeded with 0002 pending below applied 0003") + } + for _, w := range []string{llmUsageMigration, "(0002)", "0003_probe.sql", "(0003)", "never inserted"} { + if !strings.Contains(err.Error(), w) { + t.Errorf("error %q does not mention %q", err, w) + } + } + + raw, err := sql.Open("pgx", dsn) + if err != nil { + t.Fatal(err) + } + defer raw.Close() + after := readApplied(t, &DB{raw}) + if len(after) != len(before) { + t.Fatalf("schema_migrations changed: before %+v, after %+v", before, after) + } + for i := range before { + if after[i].version != before[i].version || after[i].checksum != before[i].checksum || !after[i].appliedAt.Equal(before[i].appliedAt) { + t.Errorf("schema_migrations row %d changed: before %+v, after %+v", i, before[i], after[i]) + } + } + var usage bool + if err := raw.QueryRow(`SELECT to_regclass('llm_usage') IS NOT NULL`).Scan(&usage); err != nil { + t.Fatal(err) + } + if usage { + t.Error("llm_usage exists: the refused 0002 migration ran") + } +}