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

postgres

package
v0.0.0-...-abb7a11 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package postgres provides the shared PostgreSQL database infrastructure.

Index

Constants

View Source
const MigrationSource = "migrations"

MigrationSource is the filesystem containing ordered *.up.sql and *.down.sql files.

Variables

This section is empty.

Functions

func LoadMigrationFS

func LoadMigrationFS(path string) (fs.FS, error)

LoadMigrationFS loads migrations from a filesystem directory for CLI/tests.

func OperationCreationUnitOfWork

func OperationCreationUnitOfWork(ctx context.Context, db *gorm.DB, fn func(*gorm.DB, *sql.Tx) error) error

OperationCreationUnitOfWork executes fn with a GORM transaction. Raw SQL that must be atomic with GORM writes must use tx.Statement.ConnPool as a *sql.Tx.

func RunMigrations

func RunMigrations(ctx context.Context, db *sql.DB, migrationFS fs.FS) error

RunMigrations runs all pending PostgreSQL migrations. ErrNoChange is success; dirty state and all other failures are returned as migration_failed.

func RunMigrationsDown

func RunMigrationsDown(ctx context.Context, db *sql.DB, migrationFS fs.FS, steps int) error

RunMigrationsDown rolls back the requested number of schema versions. It is intended for explicit operator rollback, never normal service startup.

func ValidateDSN

func ValidateDSN(dsn string) error

ValidateDSN validates a PostgreSQL URL without returning credentials.

func WithConnection

func WithConnection(ctx context.Context, db *sql.DB, fn func(*sql.Conn) error) error

WithConnection acquires one connection from the shared pool for session scoped PostgreSQL operations such as advisory locks.

Types

type AdvisoryLock

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

AdvisoryLock holds a PostgreSQL session-level advisory lock. The lock is released automatically when Unlock is called or when the underlying connection returns to the pool.

func AcquireAdvisoryLock

func AcquireAdvisoryLock(ctx context.Context, db *sql.DB, key int64) (*AdvisoryLock, error)

AcquireAdvisoryLock acquires a connection from the shared db pool and obtains a session-level advisory lock on the same connection. Call Unlock to release the lock and return the connection.

func TryAcquireAdvisoryLock

func TryAcquireAdvisoryLock(ctx context.Context, db *sql.DB, key int64) (*AdvisoryLock, bool, error)

TryAcquireAdvisoryLock attempts to acquire a session-level advisory lock without blocking. Returns the lock and true on success, or nil and false if the lock is already held by another session.

func (*AdvisoryLock) Key

func (l *AdvisoryLock) Key() int64

Key returns the advisory lock key.

func (*AdvisoryLock) Unlock

func (l *AdvisoryLock) Unlock(ctx context.Context) error

Unlock releases the advisory lock and returns the connection to the pool. It is safe to call multiple times; subsequent calls are no-ops.

The caller owns the context: a session-level advisory lock must be released even when the request that acquired it has already timed out, so a request-scoped caller should pass context.WithoutCancel(ctx) rather than the cancellable context itself.

type Database

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

Database owns one physical database/sql pool and a GORM wrapper over it.

func Open

Open validates configuration, opens the shared pgx stdlib pool, waits for the server to answer, and wraps the same pool with GORM. It never logs or returns the DSN.

The wait is bounded and only retries transport-level failures (TASK-351): a pod that starts before kube-router has admitted its address to the postgres NetworkPolicy gets ECONNREFUSED for a few hundred milliseconds, and a single ping would turn that window into exit 1 plus a CrashLoopBackOff restart. A server that answers with an error is reported without retrying.

func (*Database) Close

func (d *Database) Close() error

Close is idempotent.

func (*Database) GORM

func (d *Database) GORM() *gorm.DB

GORM returns the GORM wrapper over SQLDB.

func (*Database) SQLDB

func (d *Database) SQLDB() *sql.DB

SQLDB returns the shared database/sql pool used by GORM and raw SQL.

Jump to

Keyboard shortcuts

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