postgres

package
v0.0.0-...-5106b98 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyMigrationCatalog

func ApplyMigrationCatalog(databaseURL string, catalog fs.FS) (bool, error)

ApplyMigrationCatalog applies one catalog to an explicitly selected ledger URL. Construct databaseURL with MigrationDatabaseURL before calling this function.

func AsPgxTx

func AsPgxTx(tx DBTX) (pgx.Tx, error)

AsPgxTx unwraps a DBTX into the concrete pgx transaction type required by libraries that integrate directly with pgx transaction semantics.

func BuildCursorValues

func BuildCursorValues(
	cursor, sortBy string,
) (pgtype.Text, pgtype.Timestamptz, pgtype.Text, error)

BuildCursorValues parses an encoded cursor into SQLC-friendly typed values.

func EmbeddedMigrationCatalog

func EmbeddedMigrationCatalog() (fs.FS, error)

EmbeddedMigrationCatalog returns the core catalog compiled into this module version.

func EmbeddedMigrationCatalogIdentity

func EmbeddedMigrationCatalogIdentity() (uint, string, error)

EmbeddedMigrationCatalogIdentity identifies the catalog compiled into this binary.

func FromNullableInt32

func FromNullableInt32(i pgtype.Int4) *int32

FromNullableInt32 converts pgtype.Int4 to *int32, returning nil if not valid.

func FromNullableInt64

func FromNullableInt64(i pgtype.Int8) *int64

FromNullableInt64 converts nullable database identities and revisions.

func FromNullableText

func FromNullableText(t pgtype.Text) *string

FromNullableText converts pgtype.Text to *string, returning nil if not valid.

func FromNullableTimestamptz

func FromNullableTimestamptz(ts pgtype.Timestamptz) *time.Time

FromNullableTimestamptz converts pgtype.Timestamptz to *time.Time, returning nil if not valid.

func InRead

func InRead[T any](
	ctx context.Context,
	txm TransactionManager,
	fn func(ctx context.Context, db DBTX) (T, error),
) (T, error)

InRead executes fn with a non-transactional database handle and returns the function result without forcing callers to manage an outer return variable.

func InTx

func InTx[T any](
	ctx context.Context,
	txm TransactionManager,
	fn func(ctx context.Context, tx DBTX) (T, error),
) (T, error)

InTx executes fn in a transaction and returns the function result without forcing callers to manage an outer return variable.

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation checks if the error is a foreign key constraint violation

func IsUniqueViolation

func IsUniqueViolation(err error, constraintName ...string) bool

IsUniqueViolation checks if the error is a unique constraint violation. If constraintName is provided, it also checks if the constraint name matches.

func LatestEmbeddedMigrationVersion

func LatestEmbeddedMigrationVersion() (uint, error)

LatestEmbeddedMigrationVersion returns the highest up migration version embedded in the running binary.

func LatestMigrationVersion

func LatestMigrationVersion(migrationNames []string) (uint, error)

LatestMigrationVersion returns the highest numeric version from up migration filenames.

func MigrationCatalogIdentity

func MigrationCatalogIdentity(files fs.FS, directory string) (uint, string, error)

MigrationCatalogIdentity matches the raw-byte catalog identity used by release manifests.

func MigrationDatabaseURL

func MigrationDatabaseURL(databaseURL, ledger string) (string, error)

MigrationDatabaseURL selects the same public ledger used by readiness checks.

func OptionalSlugToText

func OptionalSlugToText(s *slug.Slug) pgtype.Text

OptionalSlugToText converts a *slug.Slug to pgtype.Text, returning invalid if nil.

func SlugToText

func SlugToText(s slug.Slug) pgtype.Text

SlugToText converts a slug.Slug to pgtype.Text.

func ToNullableInt32

func ToNullableInt32(i *int32) pgtype.Int4

ToNullableInt32 converts *int32 to pgtype.Int4, returning invalid pgtype.Int4 if nil.

func ToNullableInt64

func ToNullableInt64(i *int64) pgtype.Int8

func ToNullableText

func ToNullableText(s *string) pgtype.Text

ToNullableText converts *string to pgtype.Text, returning invalid pgtype.Text if nil.

func ToNullableTimestamptz

func ToNullableTimestamptz(ts *time.Time) pgtype.Timestamptz

ToNullableTimestamptz converts *time.Time to pgtype.Timestamptz, returning invalid if nil.

func ToText

func ToText(s string) pgtype.Text

ToText converts a string to pgtype.Text.

func ToTimestamptz

func ToTimestamptz(ts time.Time) pgtype.Timestamptz

ToTimestamptz converts time.Time to pgtype.Timestamptz.

func WithConsistentSnapshot

func WithConsistentSnapshot(ctx context.Context) context.Context

WithConsistentSnapshot requests one database view for a multi-query plan. Callers must select this before opening their outer unit of work.

func WithoutTransaction

func WithoutTransaction(ctx context.Context) context.Context

WithoutTransaction preserves caller context values while ensuring database work starts outside any active service unit of work.

Types

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
	Query(context.Context, string, ...any) (pgx.Rows, error)
	QueryRow(context.Context, string, ...any) pgx.Row
}

type MigrationPreflight

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

MigrationPreflight checks that the target database schema matches the running release before the application serves traffic.

func NewMigrationPreflight

func NewMigrationPreflight(database MigrationStateReader, ledger string, requiredVersion uint) (*MigrationPreflight, error)

NewMigrationPreflight checks one explicitly selected release catalog and ledger.

func (*MigrationPreflight) Check

func (p *MigrationPreflight) Check(ctx context.Context) error

Check verifies that the database migration state is safe for this release.

func (*MigrationPreflight) Status

Status loads and evaluates migration state for operator startup and readiness checks.

type MigrationPreflightStatus

type MigrationPreflightStatus struct {
	Ready           bool
	CurrentVersion  uint
	RequiredVersion uint
	Reason          string
}

MigrationPreflightStatus describes whether a database matches this release's migration contract without exposing database connection or query details.

func EvaluateMigrationPreflight

func EvaluateMigrationPreflight(
	state MigrationState,
	requiredVersion uint,
) (MigrationPreflightStatus, error)

EvaluateMigrationPreflight compares database migration metadata to the release-required version and rejects every ambiguous state.

type MigrationState

type MigrationState struct {
	Present bool
	Version uint
	Dirty   bool
}

MigrationState is the database migration state recorded by golang-migrate.

func ReadMigrationState

func ReadMigrationState(
	ctx context.Context,
	database MigrationStateReader,
	ledger string,
) (MigrationState, error)

ReadMigrationState reads golang-migrate metadata without exposing migration table details outside the postgres package.

type MigrationStateReader

type MigrationStateReader interface {
	QueryRow(context.Context, string, ...any) pgx.Row
}

MigrationStateReader is the narrow pgx query boundary needed by migration preflight checks.

type SessionLocker

type SessionLocker interface {
	WithLock(context.Context, string, func(context.Context) error) (bool, error)
}

SessionLocker holds an advisory lock without keeping a transaction open during external work.

func NewSessionLocker

func NewSessionLocker(pool *pgxpool.Pool) SessionLocker

type TransactionManager

type TransactionManager interface {
	// RunInTx executes fn inside a database transaction.
	// If fn returns an error the transaction is rolled back; otherwise it is committed.
	// A panic rolls back the owned transaction and propagates to the caller.
	RunInTx(ctx context.Context, fn func(ctx context.Context, tx DBTX) error) error

	// RunRead executes fn against a read handle without opening a transaction.
	// When the context already carries a transaction, RunRead reuses it so nested
	// service calls remain inside the caller's unit of work.
	RunRead(ctx context.Context, fn func(ctx context.Context, db DBTX) error) error
}

TransactionManager abstracts database unit-of-work access. Services depend on this interface instead of *pgxpool.Pool directly, making them testable without a real database connection.

func NewTransactionManager

func NewTransactionManager(pool *pgxpool.Pool) TransactionManager

NewTransactionManager creates a TransactionManager backed by a pgxpool.Pool.

Directories

Path Synopsis
Package abusecontrols persists shared HTTP abuse-control windows.
Package abusecontrols persists shared HTTP abuse-control windows.
Package credentialrekey exposes operator-only ciphertext census and replacement.
Package credentialrekey exposes operator-only ciphertext census and replacement.
Package pipelinecreations persists proposals for pipelines not yet imported from Git.
Package pipelinecreations persists proposals for pipelines not yet imported from Git.
Package pipelinesinkeffects persists durable pipeline sink side-effect decisions.
Package pipelinesinkeffects persists durable pipeline sink side-effect decisions.
Package proofobservations persists the backend-owned Proof observation ledger.
Package proofobservations persists the backend-owned Proof observation ledger.
Package repositorysetups persists resumable project repository setup state.
Package repositorysetups persists resumable project repository setup state.

Jump to

Keyboard shortcuts

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