Documentation
¶
Overview ¶
Package pgdb opens a PostgreSQL connection pool and helps with transactions.
It stays deliberately thin: queries are written by generators (sqlc for static ones, jet for dynamic ones) and only the parts identical in every project live here.
Index ¶
- Constants
- func InTx(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error) error
- func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS, log *slog.Logger) ([]string, error)
- func MigrateSchema(ctx context.Context, db Beginner, owner string, steps []SchemaStep) error
- func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)
- type Beginner
- type Config
- type SchemaStep
Constants ¶
const SchemaVersionsTable = "platform_schema_versions"
SchemaVersionsTable records the schema version of every owner that MigrateSchema ran for.
Variables ¶
This section is empty.
Functions ¶
func InTx ¶
InTx runs fn inside a transaction: it rolls back on error or panic and commits otherwise.
func Migrate ¶ added in v0.2.0
func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS, log *slog.Logger) ([]string, error)
Migrate applies the pending SQL migrations from fsys and returns the files it applied.
Migrations are goose files ("-- +goose Up"). A session advisory lock is held while they run, so several instances starting at once apply each migration exactly once. Files other than *.sql are ignored: the embedded directory also holds its Go file.
func MigrateSchema ¶ added in v0.8.0
MigrateSchema brings the tables a platform module owns to its last step.
Project tables live in goose migrations (Migrate); a module's own tables are versioned here, per owner, so a release can add a column and every database gets it. Steps run in one transaction under an advisory lock, so several instances starting at once apply each step exactly once. A database already at a newer version than the steps — a rollback to an older release — is left as it is: schema changes must stay readable by the release before them. Statements must be transactional: no CONCURRENTLY.
Types ¶
type Beginner ¶ added in v0.8.0
Beginner starts a transaction: *pgxpool.Pool, *pgx.Conn and pgx.Tx all satisfy it.
type Config ¶
type Config struct {
URL string
MaxConns int32
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
ConnectTimeout time.Duration
}
Config holds pool settings. Empty fields fall back to defaults.
type SchemaStep ¶ added in v0.8.0
SchemaStep is one version of the tables a platform module owns.