Help us shape the future of Go! Take the 2026 Go Developer Survey

database

package
v1.17.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Options(
	fx.Provide(NewPgxConnPool),
	fx.Provide(NewGormDB),
	fx.Provide(
		fx.Annotate(
			NewPgxConnPool,
			fx.ParamTags(`name:"river"`),
			fx.ResultTags(`name:"river"`),
		),
	),
)

Functions

func CalcBatchSize added in v1.13.3

func CalcBatchSize(db *gorm.DB, model any) (int, error)

CalcBatchSize returns a safe batch size for CreateInBatches based on the model's column count, with ~10% headroom for extra params (e.g. ON CONFLICT clauses in upserts).

func GetMigrationVersionFromDB added in v1.13.0

func GetMigrationVersionFromDB(db shared.DB) (uint, bool, error)

GetMigrationVersionFromDB returns the current migration version using an existing GORM database instance

func IsDuplicateKeyError

func IsDuplicateKeyError(err error) bool

func NewGormDB

func NewGormDB(existingPool *pgxpool.Pool) *gorm.DB

NewGormDB creates a GORM instance using an existing *pgxpool.Pool

func NewPgxConnPool

func NewPgxConnPool(cfg PoolConfig) *pgxpool.Pool

func RunMigrations

func RunMigrations(db shared.DB) error

RunMigrations runs all pending migrations inside a single transaction, so a failure anywhere in the batch rolls back the whole thing instead of leaving partial state.

func RunMigrationsFromSource added in v1.12.2

func RunMigrationsFromSource(db shared.DB, src migsource.Driver) error

RunMigrationsFromSource is RunMigrations with an injectable migration source, so tests can exercise it against crafted migrations instead of the real embedded ones.

func WrapConnError added in v1.17.0

func WrapConnError(cfg PoolConfig, err error) error

WrapConnError adds the connection details (never the password) to a connection/query error, and for common Postgres auth/catalog failures gives a specific hint about what's misconfigured. Postgres reports a missing role the same way as a wrong password (SQLSTATE 28P01) to avoid leaking valid usernames, so those two cases can't be told apart here.

Types

type PoolConfig

type PoolConfig struct {
	User     string
	Password string
	Host     string
	Port     string
	DBName   string
	SSLMode  string `validate:"oneof=disable require"` // "disable" or "require"

	MaxOpenConns    int32
	MinConns        int32
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

PoolConfig holds database connection pool configuration This is used by both GORM and pgx pools to ensure consistent connection management

func GetPoolConfigFromEnv

func GetPoolConfigFromEnv() PoolConfig

GetPoolConfigFromEnv reads pool configuration from environment variables Falls back to sensible defaults if not specified

Environment variables: - DB_MAX_OPEN_CONNS: Maximum number of open connections (default: 25) - DB_MAX_IDLE_CONNS: Maximum number of idle connections (default: 5) - DB_CONN_MAX_LIFETIME: Maximum connection lifetime, e.g. "5m" (default: 5 minutes) - DB_CONN_MAX_IDLE_TIME: Maximum idle time before closing, e.g. "1m" (default: 1 minute)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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