storage

package
v0.30.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package storage owns the Postgres connection pool and runs migrations.

In the new arch the catalog "domain repos" that used to live here moved out: each `app/X.Store` constructs its own sqlc-backed queries via `gen.New(pool)` against the pool surfaced by Storage.Pool(). Storage's remaining job is composition-root plumbing — open, ping, hand out the pool, close.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForceVersion added in v0.10.0

func ForceVersion(dsn string, version uint) error

ForceVersion records version as the schema's current one and clears the dirty flag, running no migration. A migration that failed half-way leaves the schema dirty and every later migrate refuses it; the operator repairs the schema by hand, then records where it now stands.

func InTx added in v0.10.1

func InTx(ctx context.Context, pool *pgxpool.Pool, fn func(ctx context.Context, tx DB) error) error

InTx runs fn in one repeatable-read transaction and commits when fn returns nil; an error or a panic rolls back every write fn made. Repeatable read keeps each row fn reads as it was when fn started, and fails a write to a row another transaction changed since rather than overwrite it.

func IsConflict added in v0.10.1

func IsConflict(err error) bool

IsConflict reports whether err is Postgres refusing a transaction because a concurrent one changed what it read (serialization failure) or locked what it needed (deadlock). Nothing was written; the same request may be retried.

func IsForeignKeyViolation added in v0.20.0

func IsForeignKeyViolation(err error, constraint string) bool

IsForeignKeyViolation reports whether err is Postgres refusing a write that would break the named foreign key.

func IsUniqueViolation added in v0.20.0

func IsUniqueViolation(err error, constraint string) bool

IsUniqueViolation reports whether err is Postgres refusing a write that would break the named unique constraint or index.

func MigrateTo added in v0.10.0

func MigrateTo(dsn string, version uint) error

MigrateTo moves the schema to an exact version, running the down migrations when it is below the current one. Exported for the rollback path: an old binary cannot read rows a newer schema wrote, so downgrading the image means downgrading the schema first.

func MigrateUp added in v0.10.6

func MigrateUp(dsn string) error

MigrateUp runs the pending up-migrations, as boot does, for a deployment that applies them in a step of its own before the pods roll.

func MigrationDSN added in v0.10.8

func MigrationDSN(dsn string, d time.Duration) (string, error)

MigrationDSN is dsn with Postgres' lock_timeout set to d, for the connections migrations run on: a migration queued behind a long transaction would otherwise hold every later query on its table behind it. A dsn that sets lock_timeout itself keeps it, and 0 leaves dsn unchanged.

func TryAdvisoryLock added in v0.11.6

func TryAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, fn func(context.Context) error) (bool, error)

TryAdvisoryLock runs fn only when the transaction-scoped advisory lock named by key is free, and reports whether it ran. Background work that one pod at a time should do uses it: the others skip the round instead of queueing behind the holder.

func WithAdvisoryLock added in v0.10.0

func WithAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, fn func(context.Context) error) error

WithAdvisoryLock runs fn while holding the transaction-scoped Postgres advisory lock named by key, so concurrent pods serialize on it. The lock is released when the transaction ends, including on a panic or a lost connection — nothing can leave it held.

Types

type DB added in v0.10.1

type DB interface {
	gen.DBTX
	Begin(ctx context.Context) (pgx.Tx, error)
}

DB is what a store runs its statements on: the pool, or a transaction InTx opened. Begin on a transaction opens a savepoint, so a store whose own write spans tables works unchanged inside a caller's transaction.

type PoolOption added in v0.9.3

type PoolOption func(*poolSettings)

PoolOption tunes the connection pool. A zero/negative value is ignored, so callers can pass config knobs directly and unset ones keep the defaults.

func WithMaxConns added in v0.9.3

func WithMaxConns(n int) PoolOption

WithMaxConns overrides the pool's maximum connections (ignored if n is not in 1..MaxInt32).

func WithMigrateLockTimeout added in v0.10.8

func WithMigrateLockTimeout(d time.Duration) PoolOption

WithMigrateLockTimeout bounds how long Open's migrations wait for a table lock; see MigrationDSN. The pool itself is not affected.

func WithMigrateOnBoot added in v0.10.0

func WithMigrateOnBoot(on bool) PoolOption

WithMigrateOnBoot(false) stops Open from running the up-migrations. An operator rolling back runs `relay migrate down` and needs the pods that restart meanwhile to leave the schema where they put it.

func WithMinConns added in v0.9.3

func WithMinConns(n int) PoolOption

WithMinConns overrides the pool's warm-floor connections (ignored if n is not in 1..MaxInt32). A higher floor keeps connections pre-established, so bursty load never pays cold-connection (dial + DNS) latency on the request path.

type Storage

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

Storage is the data-access handle the composition root passes around.

func Open

func Open(ctx context.Context, dsn string, opts ...PoolOption) (*Storage, error)

Open opens a connection pool, runs pending migrations, and returns a ready-to-use *Storage. The returned Storage must be closed with Close when no longer needed. Pool sizing defaults to MaxConns 10 / MinConns 2; pass WithMaxConns / WithMinConns to override, WithMigrateOnBoot(false) to skip the migrations.

func WrapPool

func WrapPool(pool *pgxpool.Pool) *Storage

WrapPool wraps an existing *pgxpool.Pool into a *Storage without opening a new pool or running migrations. Intended for tests that supply their own pool. The returned Storage must NOT be closed — the caller owns the pool's lifetime.

func (*Storage) Close

func (s *Storage) Close()

Close releases the connection pool.

func (*Storage) Ping

func (s *Storage) Ping(ctx context.Context) error

Ping checks database connectivity.

func (*Storage) Pool

func (s *Storage) Pool() *pgxpool.Pool

Pool returns the underlying pgxpool. Composition-root use only — domain code reaches Postgres via its own typed Store packages.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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