Files

163 lines
5.1 KiB
Go

// AGENT: golang-migrate now runs the embedded SQL files instead of the previous
// hand-written runner. The legacy table upgrade below keeps existing lab 2
// volumes usable, so switching engines does not require deleting data.
package database
import (
"context"
"embed"
"errors"
"fmt"
"time"
"github.com/golang-migrate/migrate/v4"
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
const (
// The old in-house runner used this advisory lock. We keep the same key for
// the one-time table upgrade so an upgrade cannot race a still-running old
// instance that is applying the baseline migration.
legacyMigrationLockID int64 = 728_2026
migrationsTable = "schema_migrations"
)
// Migrate applies the embedded golang-migrate migrations. golang-migrate takes
// its own PostgreSQL advisory lock, records the current version and dirty flag
// in schema_migrations, and wraps each migration file in one transaction.
func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error {
if err := upgradeLegacyMigrationsTable(ctx, pool); err != nil {
return err
}
source, err := iofs.New(migrationFiles, "migrations")
if err != nil {
return fmt.Errorf("load embedded migrations: %w", err)
}
connConfig, err := pgx.ParseConfig(databaseURL)
if err != nil {
_ = source.Close()
return fmt.Errorf("parse DATABASE_URL for migrations: %w", err)
}
// golang-migrate acquires its lock with context.Background(). A PostgreSQL
// lock timeout keeps startup bounded if another instance never releases it.
if connConfig.RuntimeParams == nil {
connConfig.RuntimeParams = make(map[string]string)
}
connConfig.RuntimeParams["lock_timeout"] = "30000"
connConfig.ConnectTimeout = 3 * time.Second
db := stdlib.OpenDB(*connConfig)
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
driver, err := pgxmigrate.WithInstance(db, &pgxmigrate.Config{
MigrationsTable: migrationsTable,
StatementTimeout: 30 * time.Second,
})
if err != nil {
_ = source.Close()
_ = db.Close()
return fmt.Errorf("create migration driver: %w", err)
}
migrator, err := migrate.NewWithInstance("iofs", source, "pgx5", driver)
if err != nil {
_ = source.Close()
_ = driver.Close()
return fmt.Errorf("create migrator: %w", err)
}
defer func() {
// Close closes both the embedded source and the database connection.
_, _ = migrator.Close()
}()
if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
return fmt.Errorf("apply migrations: %w", err)
}
return nil
}
// upgradeLegacyMigrationsTable replaces the table written by the previous
// in-house runner (version text, applied_at) with the golang-migrate schema
// (version bigint, dirty boolean). The schema itself is left untouched and the
// highest applied version is carried over, so a preserved volume does not need
// to be recreated. The migration DDL is idempotent, but preserving the version
// keeps the migration history truthful.
//
// AGENT: This is a one-time compatibility bridge, not a permanent second
// migration format. It runs under the old advisory lock before golang-migrate
// creates its own lock and metadata.
func upgradeLegacyMigrationsTable(ctx context.Context, pool *pgxpool.Pool) error {
conn, err := pool.Acquire(ctx)
if err != nil {
return fmt.Errorf("acquire migration connection: %w", err)
}
defer conn.Release()
if _, err := conn.Exec(ctx, "SELECT pg_advisory_lock($1)", legacyMigrationLockID); err != nil {
return fmt.Errorf("acquire migration lock: %w", err)
}
defer func() {
unlockCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", legacyMigrationLockID)
}()
var legacy bool
if err := conn.QueryRow(ctx, `
SELECT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = current_schema()
AND table_name = $1
AND column_name = 'applied_at'
)`, migrationsTable).Scan(&legacy); err != nil {
return fmt.Errorf("inspect %s: %w", migrationsTable, err)
}
if !legacy {
return nil
}
tx, err := conn.Begin(ctx)
if err != nil {
return fmt.Errorf("begin migration table upgrade: %w", err)
}
defer func() { _ = tx.Rollback(context.Background()) }()
const upgrade = `
DROP TABLE IF EXISTS schema_migrations_migrate_tmp;
CREATE TABLE schema_migrations_migrate_tmp (
version bigint NOT NULL PRIMARY KEY,
dirty boolean NOT NULL
);
INSERT INTO schema_migrations_migrate_tmp (version, dirty)
SELECT MAX(substring(version FROM '^[0-9]+')::bigint), false
FROM schema_migrations
WHERE version ~ '^[0-9]+'
HAVING COUNT(*) > 0;
DROP TABLE schema_migrations;
ALTER TABLE schema_migrations_migrate_tmp RENAME TO schema_migrations;`
if _, err := tx.Exec(ctx, upgrade); err != nil {
return fmt.Errorf("upgrade legacy %s: %w", migrationsTable, err)
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("commit migration table upgrade: %w", err)
}
return nil
}