{"modulePath":"github.com/LeanerCloud/cloud-commitments-platform","version":"v0.0.0-20261202160902-a99bcb5bfea3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package database\n\n## Types\n\n```go\ntype Config struct {\n\tHost              string\n\tLogLevel          string\n\tDatabase          string\n\tUser              string\n\tPassword          string //nolint:gosec // G101: field holds a user-supplied runtime password, not a hardcoded credential\n\tPasswordSecret    string\n\tSSLMode           string\n\tMigrationsPath    string\n\tMinConnections    int\n\tMaxConnLifetime   time.Duration\n\tMaxConnIdleTime   time.Duration\n\tHealthCheckPeriod time.Duration\n\tConnectTimeout    time.Duration\n\tMaxConnections    int\n\tPort              int\n\tAutoMigrate       bool\n}\n```\nConfig holds database configuration.\n\n```go\nfunc LoadFromEnv() (*Config, error)\n```\nLoadFromEnv loads database configuration from environment variables. A set but malformed numeric, boolean or duration variable is an error, like an out-of-range one: falling back to the default would let a typo such as DB\\_MAX\\_CONNECTIONS=\"100 \" run the pool at a quarter of its intended size with nothing in the logs (#120).\n\n```go\nfunc (c *Config) DSN(passwordOverride string) string\n```\nDSN generates a PostgreSQL connection string If passwordOverride is provided, it's used instead of config.Password.\n\n```go\nfunc (c *Config) RedactedDSN() string\n```\nRedactedDSN returns a DSN string with the password masked, safe for logging.\n\n```go\nfunc (c *Config) Validate() error\n```\nValidate checks if the configuration is valid.\n\n```go\ntype Connection struct {\n\t// contains filtered or unexported fields\n}\n```\nConnection wraps a PostgreSQL connection pool.\n\n```go\nfunc NewConnection(ctx context.Context, config *Config, secretResolver SecretResolver) (*Connection, error)\n```\nNewConnection creates a new database connection pool If secretResolver is provided and config.PasswordSecret is set, password will be retrieved from secret manager.\n\n```go\nfunc OpenFromEnv(ctx context.Context) (*Connection, error)\n```\nOpenFromEnv creates a database connection using environment-variable configuration.\n\nIt 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.\n\nThis 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.\n\n```go\nfunc (c *Connection) Acquire(ctx context.Context) (*pgxpool.Conn, error)\n```\nAcquire gets a connection from the pool.\n\n```go\nfunc (c *Connection) AdvisoryLockHeldFor(ctx context.Context, lockID int64) (time.Duration, bool, error)\n```\nAdvisoryLockHeldFor reports how long another session has held the session-level advisory lock lockID. found is false when no session holds it (for example it was released between a failed TryAdvisoryLock and this call) or when the holder's age is not visible to this role.\n\nThe age is exact for locks taken through TryAdvisoryLock (see advisoryLockHolderAgeSQL). It under-reports for a lock stranded on a connection that went back to the pool and keeps running other queries, since each query refreshes state\\_change; ReleaseAdvisoryLock closes such connections instead of pooling them (issue #105), so that case should not arise. Read-only and safe to call concurrently.\n\n```go\nfunc (c *Connection) Begin(ctx context.Context) (pgx.Tx, error)\n```\nBegin starts a new transaction.\n\n```go\nfunc (c *Connection) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)\n```\nBeginTx starts a new transaction with options.\n\n```go\nfunc (c *Connection) Close()\n```\nClose closes the connection pool.\n\n```go\nfunc (c *Connection) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)\n```\nExec executes a command.\n\n```go\nfunc (c *Connection) HealthCheck(ctx context.Context) error\n```\nHealthCheck verifies the database connection is healthy.\n\n```go\nfunc (c *Connection) Ping(ctx context.Context) error\n```\nPing checks the database connection.\n\n```go\nfunc (c *Connection) Pool() *pgxpool.Pool\n```\nPool returns the underlying connection pool.\n\n```go\nfunc (c *Connection) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)\n```\nQuery executes a query.\n\n```go\nfunc (c *Connection) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row\n```\nQueryRow executes a query that returns at most one row.\n\n```go\nfunc (c *Connection) ReleaseAdvisoryLock(_ context.Context, lockID int64)\n```\nReleaseAdvisoryLock releases a previously acquired advisory lock. It reuses the same pinned connection used by TryAdvisoryLock to ensure the unlock targets the correct PostgreSQL session. The context parameter is deliberately unused (see below); it stays in the signature so the method keeps satisfying the server.TaskLocker interface.\n\n```go\nfunc (c *Connection) Stats() *pgxpool.Stat\n```\nStats returns connection pool statistics.\n\n```go\nfunc (c *Connection) TryAdvisoryLock(ctx context.Context, lockID int64) (bool, error)\n```\nTryAdvisoryLock attempts to acquire a PostgreSQL session-level advisory lock. Returns true if the lock was acquired, false if another session holds it.\n\nSession-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.\n\n```go\ntype SecretResolver interface {\n\tGetSecret(ctx context.Context, secretID string) (string, error)\n\tClose() error\n}\n```\nSecretResolver interface for retrieving secrets from cloud providers.\n\n","path":"github.com/LeanerCloud/cloud-commitments-platform/internal/database","name":"database","synopsis":"","isRedistributable":true}
