-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.go
More file actions
92 lines (77 loc) · 2.86 KB
/
migrate.go
File metadata and controls
92 lines (77 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package pgoutbox
import (
"context"
"embed"
"fmt"
"io/fs"
"regexp"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
)
//go:embed sqlc/migrations/*.sql
var embedMigrations embed.FS
// schemaNameRE matches a safe Postgres identifier: leading letter or underscore,
// followed by letters, digits, or underscores, up to the 63-byte identifier
// limit. The schema name is interpolated into SQL (CREATE SCHEMA, the goose
// migrations table name, and the search_path startup parameter), so we reject
// anything outside this set to avoid identifier injection.
var schemaNameRE = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]{0,62}$`)
func validateSchemaName(schema string) error {
if !schemaNameRE.MatchString(schema) {
return fmt.Errorf("invalid schema name %q: must match %s", schema, schemaNameRE)
}
return nil
}
// Migrate runs the embedded pgoutbox migrations against the given pool.
// It is the explicit alternative to NewOutbox's auto-migration: callers
// that want to control when DDL runs (separate startup phase, release
// pipeline, etc.) should construct the outbox with WithAutoMigrate(false)
// and invoke Migrate themselves.
//
// Only WithSchema is consulted from opts; other options are accepted for
// API symmetry but ignored.
func Migrate(ctx context.Context, pool *pgxpool.Pool, opts ...OutboxOpt) error {
o := defaultOpts()
for _, f := range opts {
f(o)
}
return runMigrations(ctx, pool, o.schema)
}
func runMigrations(ctx context.Context, pool *pgxpool.Pool, schema string) error {
if err := validateSchemaName(schema); err != nil {
return err
}
quotedSchema := pgx.Identifier{schema}.Sanitize()
if _, err := pool.Exec(ctx, "CREATE SCHEMA IF NOT EXISTS "+quotedSchema); err != nil {
return fmt.Errorf("could not create schema %q: %w", schema, err)
}
// Pin search_path on every conn so the unqualified CREATE TABLE in the
// migration body lands in the configured schema. Safe to use the raw
// value here because validateSchemaName has already restricted it to
// identifier-safe characters.
connConfig := pool.Config().Copy().ConnConfig
connConfig.RuntimeParams["search_path"] = schema
db := stdlib.OpenDB(*connConfig)
defer db.Close()
fsys, err := fs.Sub(embedMigrations, "sqlc/migrations")
if err != nil {
return fmt.Errorf("could not create migration sub-filesystem: %w", err)
}
// Instance-based provider — avoids goose's package-level globals so that
// concurrent migrations against different schemas don't race.
provider, err := goose.NewProvider(
goose.DialectPostgres,
db,
fsys,
goose.WithTableName(pgx.Identifier{schema, "migrations"}.Sanitize()),
)
if err != nil {
return fmt.Errorf("could not create migration provider: %w", err)
}
if _, err := provider.Up(ctx); err != nil {
return fmt.Errorf("could not apply migrations: %w", err)
}
return nil
}