Documentation
¶
Index ¶
- Variables
- func CalcBatchSize(db *gorm.DB, model any) (int, error)
- func GetMigrationVersionFromDB(db shared.DB) (uint, bool, error)
- func IsDuplicateKeyError(err error) bool
- func NewGormDB(existingPool *pgxpool.Pool) *gorm.DB
- func NewPgxConnPool(cfg PoolConfig) *pgxpool.Pool
- func RunMigrations(db shared.DB) error
- func RunMigrationsFromSource(db shared.DB, src migsource.Driver) error
- func WrapConnError(cfg PoolConfig, err error) error
- type PoolConfig
Constants ¶
This section is empty.
Variables ¶
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
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
GetMigrationVersionFromDB returns the current migration version using an existing GORM database instance
func IsDuplicateKeyError ¶
func NewPgxConnPool ¶
func NewPgxConnPool(cfg PoolConfig) *pgxpool.Pool
func RunMigrations ¶
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
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)