mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 04:38:02 +00:00
database/sqlmigrate/pgmigrate: add Schema field for qualified _migrations table
Add Schema string field to Migrator. When set, Applied() constructs a
schema-qualified table name via pgx.Identifier.Sanitize() rather than
the bare "_migrations". New() signature is unchanged.
Usage:
runner := pgmigrate.New(conn)
runner.Schema = "authz"
This commit is contained in:
parent
17bbc881a9
commit
65432d7c29
@ -2,12 +2,10 @@
|
|||||||
//
|
//
|
||||||
// # Multi-tenant schemas
|
// # Multi-tenant schemas
|
||||||
//
|
//
|
||||||
// For schema-based multi-tenancy, set search_path on the connection
|
// Pass a Schema to target a specific PostgreSQL schema:
|
||||||
// before creating the migrator:
|
|
||||||
//
|
//
|
||||||
// conn, _ := pgx.Connect(ctx, pgURL)
|
|
||||||
// _, _ = conn.Exec(ctx, fmt.Sprintf("SET search_path TO %s", pgx.Identifier{schema}.Sanitize()))
|
|
||||||
// runner := pgmigrate.New(conn)
|
// runner := pgmigrate.New(conn)
|
||||||
|
// runner.Schema = "authz"
|
||||||
//
|
//
|
||||||
// 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;
|
||||||
@ -28,6 +26,7 @@ import (
|
|||||||
// Migrator implements sqlmigrate.Migrator using a single pgx.Conn.
|
// Migrator implements sqlmigrate.Migrator using a single pgx.Conn.
|
||||||
type Migrator struct {
|
type Migrator struct {
|
||||||
Conn *pgx.Conn
|
Conn *pgx.Conn
|
||||||
|
Schema string // optional; qualifies the _migrations table (e.g. "authz")
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a Migrator from the given connection.
|
// New creates a Migrator from the given connection.
|
||||||
@ -35,6 +34,15 @@ func New(conn *pgx.Conn) *Migrator {
|
|||||||
return &Migrator{Conn: conn}
|
return &Migrator{Conn: conn}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// migrationsTable returns the (optionally schema-qualified) _migrations table
|
||||||
|
// name, safe for direct interpolation into a query string.
|
||||||
|
func (r *Migrator) migrationsTable() string {
|
||||||
|
if r.Schema == "" {
|
||||||
|
return "_migrations"
|
||||||
|
}
|
||||||
|
return pgx.Identifier{r.Schema, "_migrations"}.Sanitize()
|
||||||
|
}
|
||||||
|
|
||||||
// verify interface compliance at compile time
|
// verify interface compliance at compile time
|
||||||
var _ sqlmigrate.Migrator = (*Migrator)(nil)
|
var _ sqlmigrate.Migrator = (*Migrator)(nil)
|
||||||
|
|
||||||
@ -73,7 +81,7 @@ func (r *Migrator) execInTx(ctx context.Context, sql string) error {
|
|||||||
// error may surface at rows.Err() rather than at Query(). Both sites
|
// error may surface at rows.Err() rather than at Query(). Both sites
|
||||||
// must check for it.
|
// must check for it.
|
||||||
func (r *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
|
func (r *Migrator) Applied(ctx context.Context) ([]sqlmigrate.Migration, error) {
|
||||||
rows, err := r.Conn.Query(ctx, "SELECT id, name FROM _migrations ORDER BY name")
|
rows, err := r.Conn.Query(ctx, "SELECT id, name FROM "+r.migrationsTable()+" ORDER BY name")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if isUndefinedTable(err) {
|
if isUndefinedTable(err) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user