mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
feat(pgmigrate): add PostgreSQL backend for sqlmigrate
This commit is contained in:
parent
a3ecf5ac81
commit
b5d9c7fab7
16
database/sqlmigrate/pgmigrate/go.mod
Normal file
16
database/sqlmigrate/pgmigrate/go.mod
Normal file
@ -0,0 +1,16 @@
|
||||
module github.com/therootcompany/golib/database/sqlmigrate/pgmigrate
|
||||
|
||||
go 1.26.1
|
||||
|
||||
require (
|
||||
github.com/jackc/pgx/v5 v5.9.1
|
||||
github.com/therootcompany/golib/database/sqlmigrate v1.0.0
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
28
database/sqlmigrate/pgmigrate/go.sum
Normal file
28
database/sqlmigrate/pgmigrate/go.sum
Normal file
@ -0,0 +1,28 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc=
|
||||
github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/therootcompany/golib/database/sqlmigrate v1.0.0 h1:vehFGUBdv/su0jDzSNxYN7N7i1KE3l2QJDf4x9LXhwQ=
|
||||
github.com/therootcompany/golib/database/sqlmigrate v1.0.0/go.mod h1:7PQUjwT78Hx+SftcIKI2PH4zSFlrSO0V9h618PJqC38=
|
||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
100
database/sqlmigrate/pgmigrate/pgmigrate.go
Normal file
100
database/sqlmigrate/pgmigrate/pgmigrate.go
Normal file
@ -0,0 +1,100 @@
|
||||
// Package pgmigrate implements sqlmigrate.Migrator for PostgreSQL via pgx/v5.
|
||||
//
|
||||
// # Multi-tenant schemas
|
||||
//
|
||||
// For schema-based multi-tenancy, set search_path on the pool's connection
|
||||
// config so all migrations target the correct schema:
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
// Each schema gets its own _migrations table, so tenants are migrated
|
||||
// independently. The sql-migrate CLI supports this via TENANT_SCHEMA;
|
||||
// see the CLI help for details.
|
||||
package pgmigrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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.
|
||||
type Migrator struct {
|
||||
Pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// New creates a Migrator from the given pool.
|
||||
func New(pool *pgxpool.Pool) *Migrator {
|
||||
return &Migrator{Pool: pool}
|
||||
}
|
||||
|
||||
// verify interface compliance at compile time
|
||||
var _ sqlmigrate.Migrator = (*Migrator)(nil)
|
||||
|
||||
// ExecUp runs the up migration SQL inside a PostgreSQL transaction.
|
||||
func (r *Migrator) ExecUp(ctx context.Context, m sqlmigrate.Migration) error {
|
||||
return r.execInTx(ctx, m.Up)
|
||||
}
|
||||
|
||||
// ExecDown runs the down migration SQL inside a PostgreSQL transaction.
|
||||
func (r *Migrator) ExecDown(ctx context.Context, m sqlmigrate.Migration) error {
|
||||
return r.execInTx(ctx, m.Down)
|
||||
}
|
||||
|
||||
func (r *Migrator) execInTx(ctx context.Context, sql string) error {
|
||||
tx, err := r.Pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: begin: %w", sqlmigrate.ErrExecFailed, err)
|
||||
}
|
||||
defer tx.Rollback(ctx) //nolint:errcheck
|
||||
|
||||
if _, err := tx.Exec(ctx, sql); err != nil {
|
||||
return fmt.Errorf("%w: exec: %w", sqlmigrate.ErrExecFailed, err)
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("%w: commit: %w", sqlmigrate.ErrExecFailed, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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.AppliedMigration, error) {
|
||||
rows, err := r.Pool.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
|
||||
}
|
||||
return nil, fmt.Errorf("%w: %w", sqlmigrate.ErrQueryApplied, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var applied []sqlmigrate.AppliedMigration
|
||||
for rows.Next() {
|
||||
var a sqlmigrate.AppliedMigration
|
||||
if err := rows.Scan(&a.ID, &a.Name); err != nil {
|
||||
return nil, fmt.Errorf("%w: scanning row: %w", sqlmigrate.ErrQueryApplied, err)
|
||||
}
|
||||
applied = append(applied, a)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("%w: reading rows: %w", sqlmigrate.ErrQueryApplied, err)
|
||||
}
|
||||
|
||||
return applied, nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user