fix(pgmigrate): take *pgx.Conn instead of *pgxpool.Pool

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.
This commit is contained in:
AJ ONeal 2026-04-09 10:46:38 -06:00
parent dec11dd6d6
commit d5d1df060f
2 changed files with 13 additions and 21 deletions

View File

@ -10,7 +10,5 @@ require (
require ( require (
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // 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 golang.org/x/text v0.29.0 // indirect
) )

View File

@ -2,18 +2,12 @@
// //
// # Multi-tenant schemas // # Multi-tenant schemas
// //
// For schema-based multi-tenancy, set search_path on the pool's connection // For schema-based multi-tenancy, set search_path on the connection
// config so all migrations target the correct schema: // before creating the migrator:
// //
// import "github.com/jackc/pgx/v5" // conn, _ := pgx.Connect(ctx, pgURL)
// // _, _ = conn.Exec(ctx, fmt.Sprintf("SET search_path TO %s", pgx.Identifier{schema}.Sanitize()))
// config, _ := pgxpool.ParseConfig(pgURL) // runner := pgmigrate.New(conn)
// 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)
// //
// Each schema gets its own _migrations table, so tenants are migrated // Each schema gets its own _migrations table, so tenants are migrated
// independently. The sql-migrate CLI supports this via TENANT_SCHEMA; // independently. The sql-migrate CLI supports this via TENANT_SCHEMA;
@ -25,20 +19,20 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/therootcompany/golib/database/sqlmigrate" "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 { type Migrator struct {
Pool *pgxpool.Pool Conn *pgx.Conn
} }
// New creates a Migrator from the given pool. // New creates a Migrator from the given connection.
func New(pool *pgxpool.Pool) *Migrator { func New(conn *pgx.Conn) *Migrator {
return &Migrator{Pool: pool} return &Migrator{Conn: conn}
} }
// verify interface compliance at compile time // 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 { 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 { if err != nil {
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err) 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. // Applied returns all applied migrations from the _migrations table.
// Returns an empty slice if the table does not exist (PG error 42P01). // Returns an empty slice if the table does not exist (PG error 42P01).
func (r *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) { 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 err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "42P01" { if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == "42P01" {
return nil, nil return nil, nil