database

package
v0.0.0-...-eeb6c7b Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: OSL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host              string
	LogLevel          string
	Database          string
	User              string
	Password          string //nolint:gosec // G101: field holds a user-supplied runtime password, not a hardcoded credential
	PasswordSecret    string
	SSLMode           string
	MigrationsPath    string
	MinConnections    int
	MaxConnLifetime   time.Duration
	MaxConnIdleTime   time.Duration
	HealthCheckPeriod time.Duration
	ConnectTimeout    time.Duration
	MaxConnections    int
	Port              int
	AutoMigrate       bool
}

Config holds database configuration.

func LoadFromEnv

func LoadFromEnv() (*Config, error)

LoadFromEnv loads database configuration from environment variables.

func (*Config) DSN

func (c *Config) DSN(passwordOverride string) string

DSN generates a PostgreSQL connection string If passwordOverride is provided, it's used instead of config.Password.

func (*Config) RedactedDSN

func (c *Config) RedactedDSN() string

RedactedDSN returns a DSN string with the password masked, safe for logging.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid.

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection wraps a PostgreSQL connection pool.

func NewConnection

func NewConnection(ctx context.Context, config *Config, secretResolver SecretResolver) (*Connection, error)

NewConnection creates a new database connection pool If secretResolver is provided and config.PasswordSecret is set, password will be retrieved from secret manager.

func OpenFromEnv

func OpenFromEnv(ctx context.Context) (*Connection, error)

OpenFromEnv creates a database connection using environment-variable configuration.

It loads the DB config from the environment, optionally builds a secrets.Resolver when DB_PASSWORD_SECRET is set (resolving the password synchronously before returning), then returns an open connection pool. The resolver is closed as soon as the password has been resolved; callers do not need to manage it.

This is the canonical bootstrap helper for cmd/* entrypoints that only need a DB connection and have no other use for the secrets resolver. If you also need the resolver for a different purpose (e.g. cmd/rekey loads a key via credentials.LoadKey before opening the DB), wire the resolver yourself and call NewConnection directly.

func (*Connection) Acquire

func (c *Connection) Acquire(ctx context.Context) (*pgxpool.Conn, error)

Acquire gets a connection from the pool.

func (*Connection) Begin

func (c *Connection) Begin(ctx context.Context) (pgx.Tx, error)

Begin starts a new transaction.

func (*Connection) BeginTx

func (c *Connection) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

BeginTx starts a new transaction with options.

func (*Connection) Close

func (c *Connection) Close()

Close closes the connection pool.

func (*Connection) Exec

func (c *Connection) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

Exec executes a command.

func (*Connection) HealthCheck

func (c *Connection) HealthCheck(ctx context.Context) error

HealthCheck verifies the database connection is healthy.

func (*Connection) Ping

func (c *Connection) Ping(ctx context.Context) error

Ping checks the database connection.

func (*Connection) Pool

func (c *Connection) Pool() *pgxpool.Pool

Pool returns the underlying connection pool.

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query executes a query.

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow executes a query that returns at most one row.

func (*Connection) ReleaseAdvisoryLock

func (c *Connection) ReleaseAdvisoryLock(ctx context.Context, lockID int64)

ReleaseAdvisoryLock releases a previously acquired advisory lock. It reuses the same pinned connection used by TryAdvisoryLock to ensure the unlock targets the correct PostgreSQL session.

func (*Connection) Stats

func (c *Connection) Stats() *pgxpool.Stat

Stats returns connection pool statistics.

func (*Connection) TryAdvisoryLock

func (c *Connection) TryAdvisoryLock(ctx context.Context, lockID int64) (bool, error)

TryAdvisoryLock attempts to acquire a PostgreSQL session-level advisory lock. Returns true if the lock was acquired, false if another session holds it.

Session-level advisory locks in PostgreSQL are tied to the backend session, not the transaction. With a connection pool, lock and unlock must use the same underlying connection. This method pins a connection for the duration of the lock and stores it internally; callers MUST call ReleaseAdvisoryLock to release both the lock and the pinned connection.

type SecretResolver

type SecretResolver interface {
	GetSecret(ctx context.Context, secretID string) (string, error)
	Close() error
}

SecretResolver interface for retrieving secrets from cloud providers.

Directories

Path Synopsis
postgres

Jump to

Keyboard shortcuts

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