Files
archie/internal/platform/pg/pg.go
T

235 lines
8.0 KiB
Go

// Package pg opens Archie's Postgres connections and applies its one ordered
// migration series.
package pg
import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
"log/slog"
"slices"
"strconv"
"strings"
"github.com/jackc/pgx/v5"
// Registers the "pgx" database/sql driver that sqlx and goose use.
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"
)
//go:embed migrations
var migrationFiles embed.FS
// Open connects with the runtime URL and checks the connection.
func Open(ctx context.Context, url string) (*sqlx.DB, error) {
db, err := sqlx.Open("pgx", url)
if err != nil {
return nil, fmt.Errorf("open database: %w", err)
}
if err := db.PingContext(ctx); err != nil {
_ = db.Close()
return nil, fmt.Errorf("reach database: %w", err)
}
return db, nil
}
// migrateLockName names the advisory lock that serialises migration and
// privilege grants across instances.
const migrateLockName = "migrate"
// Migrate applies every pending migration as the owner, then grants the
// runtime role DML on each module schema. Both steps run under one session
// advisory lock, so two instances starting together take turns: the second
// finds the migrations applied and the grants already in place. Granting on the
// same object from two sessions fails with "tuple concurrently updated", so the
// grants need the lock as much as the migrations do. It returns an error on any
// failure, and the caller retries.
func Migrate(ctx context.Context, log *slog.Logger, ownerURL, runtimeURL string, schemas []string) error {
db, err := Open(ctx, ownerURL)
if err != nil {
return fmt.Errorf("owner connection: %w", err)
}
defer func() { _ = db.Close() }()
release, err := lockMigrate(ctx, db)
if err != nil {
return err
}
defer release()
files, err := fs.Sub(migrationFiles, "migrations")
if err != nil {
return fmt.Errorf("migration files: %w", err)
}
provider, err := goose.NewProvider(goose.DialectPostgres, db.DB, files)
if err != nil {
return fmt.Errorf("migration provider: %w", err)
}
results, err := provider.Up(ctx)
if err != nil {
return fmt.Errorf("apply migrations: %w", err)
}
for _, r := range results {
log.InfoContext(ctx, "migration applied", "migration", r.Source.Version, "file", r.Source.Path, "took", r.Duration)
}
return grantRuntime(ctx, db, runtimeURL, schemas)
}
// lockMigrate waits for the migration advisory lock and holds it on a dedicated
// connection until release is called. Goose's own session locker covers only
// the migrations, which leaves the grants unprotected, so the lock is taken here
// around both.
func lockMigrate(ctx context.Context, db *sqlx.DB) (release func(), err error) {
conn, err := db.Conn(ctx)
if err != nil {
return nil, fmt.Errorf("lock migrations: %w", err)
}
if _, err := conn.ExecContext(ctx, "SELECT pg_advisory_lock(hashtextextended($1, 0))", migrateLockKey()); err != nil {
_ = conn.Close()
return nil, fmt.Errorf("lock migrations: %w", err)
}
return func() {
// Closing the connection would release the lock too, but the pool may
// keep it open.
_, _ = conn.ExecContext(context.WithoutCancel(ctx),
"SELECT pg_advisory_unlock(hashtextextended($1, 0))", migrateLockKey())
_ = conn.Close()
}, nil
}
func migrateLockKey() string { return "archie." + migrateLockName + ".owner" }
// schemaPrivileges returns the runtime DML privileges for schema. The audit
// schema is append-only so the runtime role may never UPDATE or DELETE rows.
func schemaPrivileges(schema string) string {
if schema == "audit" {
return "SELECT, INSERT"
}
return "SELECT, INSERT, UPDATE, DELETE"
}
// grantRuntime gives the runtime role USAGE and DML on each schema, never DDL.
func grantRuntime(ctx context.Context, db *sqlx.DB, runtimeURL string, schemas []string) error {
cfg, err := pgx.ParseConfig(runtimeURL)
if err != nil {
return fmt.Errorf("parse runtime URL: %w", err)
}
role := pgx.Identifier{cfg.User}.Sanitize()
for _, schema := range schemas {
name := pgx.Identifier{schema}.Sanitize()
privs := schemaPrivileges(schema)
for _, stmt := range []string{
"GRANT USAGE ON SCHEMA " + name + " TO " + role,
"GRANT " + privs + " ON ALL TABLES IN SCHEMA " + name + " TO " + role,
// A table keyed by a serial draws from a sequence, which is its
// own grant: a module whose insert draws on one would fail DML it
// is otherwise allowed.
"GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA " + name + " TO " + role,
} {
if _, err := db.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("grant runtime role on %s: %w", schema, err)
}
}
}
if _, err := db.ExecContext(ctx, "GRANT SELECT ON TABLE public.goose_db_version TO "+role); err != nil {
return fmt.Errorf("grant runtime role on goose_db_version: %w", err)
}
return nil
}
// VerifySchema checks that every embedded migration has been applied to db.
// Serving processes without DDL credentials call this at boot and refuse to
// serve if migrations are pending.
func VerifySchema(ctx context.Context, db *sqlx.DB) error {
embedded, err := embeddedMigrationVersions()
if err != nil {
return err
}
var applied []int64
if err := db.SelectContext(ctx, &applied, "SELECT version_id FROM public.goose_db_version WHERE is_applied"); err != nil {
return fmt.Errorf("read schema version (has 'archie migrate' run?): %w", err)
}
missing := missingMigrations(embedded, applied)
if len(missing) > 0 {
return fmt.Errorf("database schema is behind by %d migration(s) (next missing: %d); run 'archie migrate'", len(missing), missing[0])
}
return nil
}
func embeddedMigrationVersions() ([]int64, error) {
entries, err := fs.ReadDir(migrationFiles, "migrations")
if err != nil {
return nil, fmt.Errorf("read embedded migrations: %w", err)
}
var versions []int64
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
continue
}
prefix, _, ok := strings.Cut(entry.Name(), "_")
if !ok {
return nil, fmt.Errorf("migration %q has no version prefix", entry.Name())
}
v, err := strconv.ParseInt(prefix, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse migration %q version: %w", entry.Name(), err)
}
versions = append(versions, v)
}
slices.Sort(versions)
return versions, nil
}
func missingMigrations(embedded, applied []int64) []int64 {
seen := make(map[int64]struct{}, len(applied))
for _, v := range applied {
seen[v] = struct{}{}
}
var missing []int64
for _, v := range embedded {
if _, ok := seen[v]; !ok {
missing = append(missing, v)
}
}
return missing
}
// ServeLockKey is the advisory-lock key the serving processes hold in its shared
// form and a restore takes exclusively: several processes may serve at once, and
// none may serve while a restore runs. The other archie binaries lock the same
// key through internal/infrastructure/postgres, so there is one name for it.
func ServeLockKey(name string) string { return "archie." + name + ".owner" }
// ServeLockName is the one name every serving process claims and every restore
// takes exclusively. A serving process and a restore that named different locks
// would exclude nothing.
const ServeLockName = "serve"
// ClaimServe holds the serve lock shared for as long as the process serves. It
// fails when a restore holds the lock exclusively, so a process never starts on a
// database another process is restoring.
func ClaimServe(ctx context.Context, db *sqlx.DB, name string) (release func(), err error) {
conn, err := db.Conn(ctx)
if err != nil {
return nil, fmt.Errorf("claim the serve lock: %w", err)
}
var taken bool
if err := conn.QueryRowContext(ctx,
"SELECT pg_try_advisory_lock_shared(hashtextextended($1, 0))", ServeLockKey(name)).Scan(&taken); err != nil {
_ = conn.Close()
return nil, fmt.Errorf("claim the serve lock: %w", err)
}
if !taken {
_ = conn.Close()
return nil, errors.New("a restore holds this database")
}
return func() {
_, _ = conn.ExecContext(context.WithoutCancel(ctx),
"SELECT pg_advisory_unlock_shared(hashtextextended($1, 0))", ServeLockKey(name))
_ = conn.Close()
}, nil
}