Repository navigation
Open
Milestone
Description
AX has no schema management. internal/store/postgres/schema.go runs all of schema.sql on every ax-server start, under an advisory lock, and relies on CREATE ... IF NOT EXISTS, ADD COLUMN IF NOT EXISTS, and a hand-written DO $$ block that rewrites the locks primary key. schema.go already carries TODO: Add versioned migrations before the schema first changes after release.
That only works while every change is additive and idempotent:
- No removals. all: push tasks to runners and remove the runner pull protocol #688 deletes eight runner columns from
tasks(actor_uid,runnable,active_run_id,run_seq,runner_token_hash,runner_token_confirmed,claim_nonce,last_acked_runner_seq). Existing databases keep them forever, because aDROP COLUMNin an always-run script would run on every start, and older replicas still reading the column would break. - No record of what ran. Nothing records which schema a database has, so we can't tell an up-to-date database from one that a newer ax-server partly upgraded, or refuse to start an older binary against a newer schema.
- Breaking changes need manual steps. The
locksprimary-key rewrite asks operators to stop every replica before upgrading (the comment inschema.go). Nothing enforces or records that. - Data changes have no place. Backfills and data rewrites need to run exactly once, not on every start.
Proposal: Goose, as Agent Substrate does it
Agent Substrate already solves this with Goose (github.com/pressly/goose/v3, v3.27.3 in its go.mod) in cmd/ateapi/internal/store/atepg/schema.go. AX already depends on Substrate at v1.0.0 and also uses pgx, so we can copy its approach almost as-is:
- Embedded, numbered SQL files.
internal/store/postgres/migrations/NNNNNN_name.sql, embedded with//go:embed migrations/*.sql.000001_initial.sqlis today's schema without theIF NOT EXISTSguards and without the pre-release upgrade blocks. - Applied at startup with the Goose provider.
goose.NewProvider(goose.DialectPostgres, stdlib.OpenDBFromPool(pool), migrations, goose.WithTableName("schema_migrations"), goose.WithSessionLocker(locker)), thenprovider.Up(ctx). This replacescreateSchema.- The locker is
lock.NewPostgresSessionLocker, keyed byhashtextextended('ax:postgres:migrations:' || current_schema(), 0), so several replicas starting at once apply each migration once and AX never contends with Substrate's lock in a shared database.
- Logging. Log the starting, current, and latest versions, the number of migrations applied, and the duration, as Substrate's
migrateToLatestdoes, and report partial failures throughgoose.PartialError. - Refuse an unknown schema. Before Goose creates its ledger, check for AX tables without a
schema_migrationstable, as Substrate'srejectUnversionedSubstrateSchemadoes. AX has pre-release installs, so instead of refusing outright we can adopt them once: if the tables exist without a ledger, verify they match000001_initial.sqland record version 1. Otherwise refuse with a clear error. - A verify script, like Substrate's
hack/verify/postgresql-migrations.sh, run bymake verify. It checks:- files are named
NNNNNN_name.sqland numbered without gaps - each file has
-- +goose Upand no-- +goose Down - no
-- +goose no transaction, noIF NOT EXISTS, noENVSUB, and noBEGIN/COMMIT, so Goose controls the transaction - against the merge base, merged migrations are never changed or deleted
- files are named
- Forward-only. As in Substrate, there are no down migrations; a mistake is fixed by a new migration. Removing a column takes two releases: stop reading it, then drop it in a later migration once no running replica uses it.
- Tests. Apply all migrations to an empty database in the Postgres tier (
AX_TEST_POSTGRES_URL), and check that a database at version N upgrades to latest.
Metadata
Metadata
Assignees
Labels
No labels
Activity