pgcommon

package
v0.0.0-...-5c79d48 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AzureBeforeConnect

func AzureBeforeConnect(log logrus.FieldLogger) (func(ctx context.Context, config *pgx.ConnConfig) error, error)

AzureBeforeConnect will return a pgx BeforeConnect function suitable for Azure AD authentication. The returned function will set the password of the connection to a token for the relevant scope.

func ConnectPostgres

func ConnectPostgres(ctx context.Context, poolConfig *pgxpool.Config) (*pgx.Conn, error)

ConnectPostgres will open a single connection to the "postgres" database in the database cluster specified in poolConfig.

func IsCode

func IsCode(err error, code string) bool

IsCode checks if the passed error is a Postgres error with the given code.

func Retry

func Retry[T any](ctx context.Context, log logrus.FieldLogger, f func() (T, error)) (T, error)

Retry runs the closure potentially more than once, retrying quickly on serialization or deadlock errors, and backing off more on other retryable errors. It will not retry on network errors or other ambiguous errors after any data has been sent. It will retry unique constraint violation and exclusion constraint violations, so the closure should not rely on those for normal behavior.

func RetryIdempotent

func RetryIdempotent[T any](ctx context.Context, log logrus.FieldLogger, f func() (T, error)) (T, error)

RetryIdempotent runs the closure potentially more than once, retrying quickly on serialization or deadlock errors, and backing off more on other errors. It assumes that f is idempotent, so it will retry even in ambiguous situations. It will retry unique constraint violation and exclusion constraint violations, so the closure should not rely on those for normal behavior.

func RetryTx

func RetryTx(
	ctx context.Context,
	log logrus.FieldLogger,
	db interface {
		BeginTx(context.Context, pgx.TxOptions) (pgx.Tx, error)
	},
	txOptions pgx.TxOptions,
	isIdempotent bool,
	f func(tx pgx.Tx) error,
) error

RetryTx runs a closure like Retry or RetryIdempotent, wrapped in pgx.BeginTxFunc.

func SetupAndMigrate

func SetupAndMigrate(
	ctx context.Context,
	log logrus.FieldLogger,
	db interface {
		BeginTx(context.Context, pgx.TxOptions) (pgx.Tx, error)
		Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
	},
	tableName string,
	schemas []string,
) error

SetupAndMigrate sets up the database schema, applying the migrations in the schemas slice in order, starting from the first non-applied one. tableName is the name of a table used to hold schema version numbers.

func TryEnsureDatabase

func TryEnsureDatabase(ctx context.Context, poolConfig *pgxpool.Config, log logrus.FieldLogger)

TryEnsureDatabase will connect to the "postgres" database and attempt to create the database named in the pool's configuration.

Types

This section is empty.

Jump to

Keyboard shortcuts

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