From d5d1df060fcf65d4af17b51d3124ccc8fba173dc Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 9 Apr 2026 10:46:38 -0600 Subject: [PATCH] fix(pgmigrate): take *pgx.Conn instead of *pgxpool.Pool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrations run sequentially on a single connection — a pool adds unnecessary complexity and forces callers to create one. This also drops the puddle/v2 and x/sync transitive dependencies. --- database/sqlmigrate/pgmigrate/go.mod | 2 -- database/sqlmigrate/pgmigrate/pgmigrate.go | 32 +++++++++------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/database/sqlmigrate/pgmigrate/go.mod b/database/sqlmigrate/pgmigrate/go.mod index e3c3344..4c18367 100644 --- a/database/sqlmigrate/pgmigrate/go.mod +++ b/database/sqlmigrate/pgmigrate/go.mod @@ -10,7 +10,5 @@ require ( require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect - github.com/jackc/puddle/v2 v2.2.2 // indirect - golang.org/x/sync v0.17.0 // indirect golang.org/x/text v0.29.0 // indirect ) diff --git a/database/sqlmigrate/pgmigrate/pgmigrate.go b/database/sqlmigrate/pgmigrate/pgmigrate.go index 6a81f7b..4996a0d 100644 --- a/database/sqlmigrate/pgmigrate/pgmigrate.go +++ b/database/sqlmigrate/pgmigrate/pgmigrate.go @@ -2,18 +2,12 @@ // // # Multi-tenant schemas // -// For schema-based multi-tenancy, set search_path on the pool's connection -// config so all migrations target the correct schema: +// For schema-based multi-tenancy, set search_path on the connection +// before creating the migrator: // -// import "github.com/jackc/pgx/v5" -// -// config, _ := pgxpool.ParseConfig(pgURL) -// config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { -// _, err := conn.Exec(ctx, fmt.Sprintf("SET search_path TO %s", pgx.Identifier{schema}.Sanitize())) -// return err -// } -// pool, _ := pgxpool.NewWithConfig(ctx, config) -// runner := pgmigrate.New(pool) +// conn, _ := pgx.Connect(ctx, pgURL) +// _, _ = conn.Exec(ctx, fmt.Sprintf("SET search_path TO %s", pgx.Identifier{schema}.Sanitize())) +// runner := pgmigrate.New(conn) // // Each schema gets its own _migrations table, so tenants are migrated // independently. The sql-migrate CLI supports this via TENANT_SCHEMA; @@ -25,20 +19,20 @@ import ( "errors" "fmt" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" - "github.com/jackc/pgx/v5/pgxpool" "github.com/therootcompany/golib/database/sqlmigrate" ) -// Migrator implements sqlmigrate.Migrator using a pgxpool.Pool. +// Migrator implements sqlmigrate.Migrator using a single pgx.Conn. type Migrator struct { - Pool *pgxpool.Pool + Conn *pgx.Conn } -// New creates a Migrator from the given pool. -func New(pool *pgxpool.Pool) *Migrator { - return &Migrator{Pool: pool} +// New creates a Migrator from the given connection. +func New(conn *pgx.Conn) *Migrator { + return &Migrator{Conn: conn} } // verify interface compliance at compile time @@ -55,7 +49,7 @@ func (r *Migrator) ExecDown(ctx context.Context, m sqlmigrate.Migration, sql str } func (r *Migrator) execInTx(ctx context.Context, sql string) error { - tx, err := r.Pool.Begin(ctx) + tx, err := r.Conn.Begin(ctx) if err != nil { return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err) } @@ -75,7 +69,7 @@ func (r *Migrator) execInTx(ctx context.Context, sql string) error { // Applied returns all applied migrations from the _migrations table. // Returns an empty slice if the table does not exist (PG error 42P01). func (r *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) { - rows, err := r.Pool.Query(ctx, "SELECT id, name FROM _migrations ORDER BY name") + rows, err := r.Conn.Query(ctx, "SELECT id, name FROM _migrations ORDER BY name") if err != nil { if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "42P01" { return nil, nil