Phase 9, part 3: schema migrations for both databases

Migrations from schema 30 on are one list (internal/db/migrations.go),
written in SQLite's SQL and translated for Postgres (db.PostgresSQL)
unless one gives its own. On Postgres the whole migration is one
transaction under an advisory lock, and each step is recorded in
schema_migrations. TestSchemasMatch compares the two schemas (tables,
columns, types, NOT NULL, nocase, indexes), with and without a new
migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 07:17:56 -07:00
co-authored by Claude Opus 5.5
parent 1b2947de7c
commit d827d507c1
9 changed files with 508 additions and 32 deletions
+45 -9
View File
@@ -45,10 +45,18 @@ func OpenPostgres(url string) (*DB, error) {
return d, nil
}
// migratePostgres creates the schema in an empty database, and checks an
// existing one isn't newer than this version knows. One copy of the app
// at a time does it (an advisory lock), so several can start together.
// AppVersion is the app's version, recorded with each Postgres migration.
var AppVersion = "dev"
// migratePostgres creates the schema in an empty database, at schema 29
// (postgres.sql), then applies the migrations after it, all in one
// transaction: a failure leaves the database as it was. One copy of the
// app at a time does it (an advisory lock), so several can start
// together. Each step is recorded in schema_migrations.
func (d *DB) migratePostgres() error {
if err := checkMigrations(); err != nil {
return err
}
tx, err := d.SQL.Begin()
if err != nil {
return err
@@ -57,10 +65,23 @@ func (d *DB) migratePostgres() error {
if _, err := tx.Exec(`SELECT pg_advisory_xact_lock(4708421)`); err != nil { // any fixed number: "the schema"
return err
}
if _, err := tx.Exec(`CREATE TABLE IF NOT EXISTS schema_migrations (
version integer PRIMARY KEY,
note text NOT NULL,
app text NOT NULL,
applied_at timestamptz NOT NULL DEFAULT now()
)`); err != nil {
return err
}
record := func(version int, note string) error {
_, err := tx.Exec(`INSERT INTO schema_migrations (version, note, app) VALUES ($1, $2, $3)`, version, note, AppVersion)
return err
}
var exists bool
if err := tx.QueryRow(`SELECT to_regclass('schema_version') IS NOT NULL`).Scan(&exists); err != nil {
return err
}
version := pgBaseline
if !exists {
if _, err := tx.Exec(pgSchema); err != nil {
return fmt.Errorf("creating the schema: %w", err)
@@ -71,17 +92,32 @@ func (d *DB) migratePostgres() error {
if _, err := tx.Exec(`INSERT INTO schema_version (version) VALUES ($1)`, pgBaseline); err != nil {
return err
}
return tx.Commit()
}
var version int
if err := tx.QueryRow(`SELECT version FROM schema_version`).Scan(&version); err != nil {
if err := record(pgBaseline, "created: schema 29 as a whole (postgres.sql)"); err != nil {
return err
}
} else if err := tx.QueryRow(`SELECT version FROM schema_version`).Scan(&version); err != nil {
return err
}
if version > Schema() {
return fmt.Errorf("the database is from a newer Supermarket (schema %d, this version knows %d); update Supermarket", version, Schema())
}
if version < Schema() {
return fmt.Errorf("the database is at schema %d and this version needs %d, but Postgres has no migrations past %d yet", version, Schema(), pgBaseline)
if version < pgBaseline {
return fmt.Errorf("the database says schema %d, older than any Postgres database Supermarket made", version)
}
if exists && version < Schema() {
d.MigratedFrom = version
}
for i := version; i < Schema(); i++ {
m, q := postgresStep(i)
if _, err := tx.Exec(q); err != nil {
return fmt.Errorf("migration %d (%s): %w", i+1, m.Note, err)
}
if _, err := tx.Exec(`UPDATE schema_version SET version = $1`, i+1); err != nil {
return err
}
if err := record(i+1, m.Note); err != nil {
return err
}
}
return tx.Commit()
}