Adopt golib/pg for migrations and pool construction #125
Reference in New Issue
Block a user
Delete Branch "benvin/adopt-golib-pg"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Why
The Postgres schema was a ~180-line DDL blob inlined in
internal/database/postgres.goand re-executed on every start. It could only grow — each change appended anotherALTER TABLE ... ADD COLUMN IF NOT EXISTS— and nothing recorded what had ever been applied.golib/pgalready owns that mechanic for the estate (arrproxy#10), so artifactapi keeps the SQL and hands over the machinery.How
git.unkin.net/unkin/golibv0.1.0.migrations/0001_init.sql, embedded by the newmigrationspackage, and build the pool withpg.NewMigrated(LockName: "artifactapi-migrations"). The runner takes a cluster-wide advisory lock the old blob never took, so replicas starting together queue instead of racing each other through the DDL.schema_migrations, so its first start on this build re-runs0001. Every statement isIF NOT EXISTS-guarded, so that run changes nothing and only lands the tracking row.TestMigratingAnAlreadyPopulatedSchemaIsANoOpdrops the row from a migrated database and asserts exactly that;TestMigrationsAreIdempotentkeeps future migrations additive and idempotent.config.DatabaseDSNstays the DSN source rather thanpg.DSNFromEnv: the variable names match, but golib deliberately has no default user or database name, and artifactapi documents and shipsDBUSER/DBNAMEdefaults ofartifacts.migrations/, and the derived advisory key is pinned so a rename cannot silently let two builds migrate at once.GOPRIVATE=git.unkin.netfor the first cross-repo Go dependency — exported by theMakefile, set in theDockerfileand both woodpecker Go steps, documented in the README.database.New's signature and every caller are untouched.Transitive bumps come with golib's
go.mod(testcontainers-go 0.42 → 0.44, otel, x/crypto, x/net);go build,go vet,golangci-lint,go test -race ./pkg/... ./internal/...andpre-commit run --all-filesare clean.