Skip to content

Versioned Postgres schema migrations with Goose (as Agent Substrate does) #691

Open

Description

@rakyll

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 a DROP COLUMN in 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 locks primary-key rewrite asks operators to stop every replica before upgrading (the comment in schema.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.sql is today's schema without the IF NOT EXISTS guards 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)), then provider.Up(ctx). This replaces createSchema.
    • The locker is lock.NewPostgresSessionLocker, keyed by hashtextextended('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 migrateToLatest does, and report partial failures through goose.PartialError.
  • Refuse an unknown schema. Before Goose creates its ledger, check for AX tables without a schema_migrations table, as Substrate's rejectUnversionedSubstrateSchema does. AX has pre-release installs, so instead of refusing outright we can adopt them once: if the tables exist without a ledger, verify they match 000001_initial.sql and record version 1. Otherwise refuse with a clear error.
  • A verify script, like Substrate's hack/verify/postgresql-migrations.sh, run by make verify. It checks:
    • files are named NNNNNN_name.sql and numbered without gaps
    • each file has -- +goose Up and no -- +goose Down
    • no -- +goose no transaction, no IF NOT EXISTS, no ENVSUB, and no BEGIN/COMMIT, so Goose controls the transaction
    • against the merge base, merged migrations are never changed or deleted
  • 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.

Activity

  1. added this to the v1.0.0 milestone on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions