postgres

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, dbURL string) (*pgxpool.Pool, error)

Connect returns a pgxpool.Pool for the given database URL. If dbURL starts with "postgres:tc:", it spins up a Testcontainer automatically. The testcontainer process lifetime is managed by the Docker daemon; callers should invoke pool.Close() when done with the connection.

func OpenPool

func OpenPool(ctx context.Context, dbcfg DBConfig, migrator *Migrator) (*pgxpool.Pool, error)

OpenPool returns the process-wide singleton pgxpool connection. The caller supplies the DBConfig (typically from AppConfig.DBConfig). The pool is created on the first call and reused on subsequent calls. A SIGTERM/SIGINT handler is registered to gracefully close the pool on shutdown.

Types

type DBConfig

type DBConfig struct {
	Host                string `env:"HOST"                  envDefault:"localhost"`
	Port                int    `env:"PORT"                  envDefault:"5432"`
	User                string `env:"USER"`
	Password            string `env:"PASSWORD"`
	Name                string `env:"NAME"`
	CloudSQLInstance    string `env:"CLOUD_SQL_INSTANCE"`
	DatabaseURLTemplate string `env:"URL_TEMPLATE" envDefault:"postgres:tc://[username]:[password]@[host]:[port]/[database_name]"`
}

DBConfig holds the database connection parameters. Environment variables are read with the "DB_" prefix (e.g. DB_HOST, DB_PORT).

func (DBConfig) LogValue

func (d DBConfig) LogValue() slog.Value

LogValue implements slog.LogValuer, redacting the password.

func (DBConfig) ResolveURL

func (d DBConfig) ResolveURL() string

ResolveURL expands the DatabaseURLTemplate placeholders using the struct's own credential fields.

type Migrator added in v0.0.2

type Migrator struct {

	// IsBaseline is invoked by runMigrations to determine whether the current
	// run should only record a baseline (i.e. mark existing migrations as
	// already applied without executing their SQL). A nil func disables
	// baseline handling entirely.
	IsBaseline func(ctx context.Context, pool *pgxpool.Pool) bool
	// contains filtered or unexported fields
}

Migrator bundles the migration file source with a caller-supplied baseline predicate. It is passed to OpenPool (and internally to runMigrations) so that the baseline decision is made by the caller rather than hard-coded.

func NewMigrator added in v0.0.2

func NewMigrator(migrationFiles fs.FS, isBaseline func(context.Context, *pgxpool.Pool) bool) *Migrator

NewMigrator creates a Migrator with the supplied migration file source and baseline predicate. Either argument may be nil; a nil IsBaseline disables baseline detection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL