Files
archie/internal/infrastructure/postgres/postgres.go
T
sam a69f9c81c2 feat(postgres): State Store schema and sqlc queries, with the claim path proven on PG 18
goose migration 0001 lands the twelve State Store tables on Postgres, and sqlc generates the task queries from it. Two behaviours that genuinely differ from SQLite are pinned by tests against a real PG 18: claiming takes the oldest queued row with FOR UPDATE SKIP LOCKED (without the clause a second claimer blocks, which the test shows by timing out), and every column behind a Go int is bigint -- SQLite's INTEGER is 64-bit, and the chat issue-number allocator seeds at 10^15, which does not fit an int4.

task db:generate writes the committed postgresdb package; task db:check regenerates it in the gate, as proto:check and ui:check do.

The remaining store methods (events, resources, identities, the status tables) still run on SQLite: porting them is the rest of archie-core-7r0x.2.
2026-09-24 01:18:19 +10:00

80 lines
2.2 KiB
Go

// Package postgres opens archie's PostgreSQL database and applies its schema
// migrations.
package postgres
import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/pressly/goose/v3/lock"
)
// minServerVersion is PostgreSQL 18 in server_version_num form.
const minServerVersion = 180000
//go:embed migrations/*.sql
var migrations embed.FS
// Migrations is the State Store schema, rooted at the migration files.
func Migrations() fs.FS {
sub, err := fs.Sub(migrations, "migrations")
if err != nil {
// Unreachable: the pattern above embeds the directory.
panic(err)
}
return sub
}
// ErrServerTooOld is returned for a server older than PostgreSQL 18.
var ErrServerTooOld = errors.New("postgres: server is older than PostgreSQL 18")
func checkServerVersion(num int) error {
if num < minServerVersion {
return fmt.Errorf("%w (server_version_num %d)", ErrServerTooOld, num)
}
return nil
}
// Open connects a pool to url and refuses a server older than PostgreSQL 18.
func Open(ctx context.Context, url string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, url)
if err != nil {
return nil, fmt.Errorf("postgres: open: %w", err)
}
var num int
if err := pool.QueryRow(ctx, "SELECT current_setting('server_version_num')::int").Scan(&num); err != nil {
pool.Close()
return nil, fmt.Errorf("postgres: read server version: %w", err)
}
if err := checkServerVersion(num); err != nil {
pool.Close()
return nil, err
}
return pool, nil
}
// Migrate applies the goose migrations in fsys. A session-level advisory lock
// makes processes that start together apply each migration once.
func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS) error {
locker, err := lock.NewPostgresSessionLocker()
if err != nil {
return fmt.Errorf("postgres: migration lock: %w", err)
}
db := stdlib.OpenDBFromPool(pool)
defer func() { _ = db.Close() }()
provider, err := goose.NewProvider(goose.DialectPostgres, db, fsys, goose.WithSessionLocker(locker))
if err != nil {
return fmt.Errorf("postgres: migrations: %w", err)
}
if _, err := provider.Up(ctx); err != nil {
return fmt.Errorf("postgres: migrate: %w", err)
}
return nil
}