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
+23 -17
View File
@@ -1,4 +1,5 @@
// Package db opens the app's SQLite database and keeps its schema current.
// Package db opens the app's database, SQLite (the local edition) or
// Postgres (server and multi-tenant), and keeps its schema current.
package db
import (
@@ -10,8 +11,9 @@ import (
_ "modernc.org/sqlite" // registers the "sqlite" driver (pure Go, no cgo)
)
// migrations are applied in order, once each. Never edit or reorder one that
// has shipped: add a new one instead.
// migrations are SQLite's schemas 1 to 29, applied in order, once each.
// They're closed: a change to the schema now goes in both (migrations.go),
// for SQLite and Postgres alike.
var migrations = []string{
// 1: settings, inventory, the content pipeline's state, and the Hermes bridge
`
@@ -685,15 +687,13 @@ CREATE TABLE list_members (
`,
}
// BeforeMigrate, if set, runs before an existing database is migrated to
// a newer schema (from and to are the schema versions): the app backs it
// up. A new, empty database isn't backed up.
// BeforeMigrate, if set, runs before an existing SQLite database is
// migrated to a newer schema (from and to are the schema versions): the
// app backs it up. A new, empty database isn't backed up. (Postgres is
// backed up by whoever runs it; its migrations are recorded in
// schema_migrations.)
var BeforeMigrate func(d *sql.DB, from, to int) error
// Schema is the schema version this build migrates databases to.
func Schema() int { return len(migrations) }
// Open opens (creating if needed) the SQLite database at path and
// migrates it. In tests run with SUPERMARKET_TEST_POSTGRES set, it opens a
// new Postgres schema instead (see TestPostgres).
@@ -728,10 +728,10 @@ func OpenSQLite(path string) (*DB, error) {
sqldb.Close()
return nil, err
}
if version > 0 && version < len(migrations) {
if err := BeforeMigrate(sqldb, version, len(migrations)); err != nil {
if version > 0 && version < Schema() {
if err := BeforeMigrate(sqldb, version, Schema()); err != nil {
sqldb.Close()
return nil, fmt.Errorf("backing up before migrating to schema %d: %w", len(migrations), err)
return nil, fmt.Errorf("backing up before migrating to schema %d: %w", Schema(), err)
}
}
}
@@ -743,19 +743,25 @@ func OpenSQLite(path string) (*DB, error) {
}
func (d *DB) migrate() error {
if err := checkMigrations(); err != nil {
return err
}
var version int
if err := d.QueryRow(`PRAGMA user_version`).Scan(&version); err != nil {
return err
}
if version > len(migrations) {
return fmt.Errorf("the database is from a newer Supermarket (schema %d, this version knows %d); update Supermarket", version, len(migrations))
if version > Schema() {
return fmt.Errorf("the database is from a newer Supermarket (schema %d, this version knows %d); update Supermarket", version, Schema())
}
for i := version; i < len(migrations); i++ {
if version > 0 && version < Schema() {
d.MigratedFrom = version
}
for i := version; i < Schema(); i++ {
tx, err := d.SQL.Begin()
if err != nil {
return err
}
if _, err := tx.Exec(migrations[i]); err != nil {
if _, err := tx.Exec(sqliteStep(i)); err != nil {
tx.Rollback()
return fmt.Errorf("migration %d: %w", i+1, err)
}