db

package
v0.35.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package db provides PostgreSQL connection management with retry, keep-alive, and schema migration utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(databaseURL string, opts ...ConnectOption) (*sqlx.DB, error)

Connect opens a PostgreSQL connection using context.Background(). Use ConnectContext to control startup cancellation.

func ConnectContext

func ConnectContext(ctx context.Context, databaseURL string, opts ...ConnectOption) (*sqlx.DB, error)

ConnectContext opens a PostgreSQL connection with retry and exponential backoff + jitter. On success it configures pool parameters and validates connectivity via PingContext.

func Migrate

func Migrate(db *sqlx.DB, cfg MigrateConfig) error

Migrate runs all up migrations from the embedded filesystem. ErrNoChange is silently ignored.

func MigrateDown

func MigrateDown(db *sqlx.DB, cfg MigrateConfig) error

MigrateDown rolls back all migrations.

func MigrateForce

func MigrateForce(db *sqlx.DB, cfg MigrateConfig, version int) error

MigrateForce sets the migration version without running any migrations. Use this to fix a dirty migration state.

func MigrateSteps

func MigrateSteps(db *sqlx.DB, cfg MigrateConfig, n int) error

MigrateSteps runs n migration steps. Positive n migrates up, negative migrates down.

func MigrateVersion

func MigrateVersion(db *sqlx.DB, cfg MigrateConfig) (version uint, dirty bool, err error)

MigrateVersion returns the current migration version and dirty flag.

func StartKeepAlive

func StartKeepAlive(ctx context.Context, db *sqlx.DB, interval time.Duration, _ int)

StartKeepAlive launches an optional keep-alive loop. For backward compatibility, poolSize is ignored; only one goroutine is started.

func StartKeepAliveWithConfig

func StartKeepAliveWithConfig(ctx context.Context, db *sqlx.DB, cfg KeepAliveConfig)

StartKeepAliveWithConfig launches one goroutine that periodically runs PingContext with a timeout. This is intended for explicit opt-in usage.

Types

type ConnectConfig

type ConnectConfig struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
	MaxRetries      int
	AttemptTimeout  time.Duration
	PgBouncerSafe   bool
	Logger          *slog.Logger
	OnRetry         OnRetryFunc
}

ConnectConfig holds connection pool and retry parameters.

type ConnectOption

type ConnectOption func(*ConnectConfig)

ConnectOption configures a ConnectConfig.

func WithAttemptTimeout

func WithAttemptTimeout(d time.Duration) ConnectOption

WithAttemptTimeout sets the timeout for each connectivity check attempt.

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) ConnectOption

WithConnMaxIdleTime sets the maximum idle time for a connection.

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) ConnectOption

WithConnMaxLifetime sets the maximum lifetime of a connection.

func WithLogger

func WithLogger(l *slog.Logger) ConnectOption

WithLogger sets the logger used to log retry attempts. A nil logger disables retry logging.

func WithMaxIdleConns

func WithMaxIdleConns(n int) ConnectOption

WithMaxIdleConns sets the maximum number of idle connections.

func WithMaxOpenConns

func WithMaxOpenConns(n int) ConnectOption

WithMaxOpenConns sets the maximum number of open connections.

func WithMaxRetries

func WithMaxRetries(n int) ConnectOption

WithMaxRetries sets the number of connection retry attempts.

func WithOnRetry

func WithOnRetry(fn OnRetryFunc) ConnectOption

WithOnRetry sets a callback invoked before each retry sleep.

func WithPgBouncerSafe

func WithPgBouncerSafe(enabled bool) ConnectOption

WithPgBouncerSafe forces pgx simple protocol mode, which is compatible with PgBouncer transaction pooling.

type KeepAliveConfig

type KeepAliveConfig struct {
	Interval time.Duration
	Timeout  time.Duration
}

KeepAliveConfig configures optional background connectivity checks.

type MigrateConfig

type MigrateConfig struct {
	FS        embed.FS
	Directory string
	Driver    string
}

MigrateConfig configures the migration runner.

type OnRetryFunc

type OnRetryFunc func(attempt int, err error, backoff time.Duration)

OnRetryFunc is called before each retry sleep with the attempt number, the error from the failed ping, and the backoff duration.

Directories

Path Synopsis
Package sqlite provides SQLite connection management and schema migration utilities for hamr-scaffolded projects.
Package sqlite provides SQLite connection management and schema migration utilities for hamr-scaffolded projects.

Jump to

Keyboard shortcuts

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