postgres

package
v0.19.11 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const NameLen = 31
View Source
const PingTimeout = 10 * time.Second

Variables

View Source
var ErrNoDbInContext = errors.New("context does not contain a bun.DB")

Functions

func GetContext

func GetContext(ctx context.Context) (bun.IDB, error)

func NewContext

func NewContext(ctx context.Context, config Config) (context.Context, error)

func NewContextSchema

func NewContextSchema(ctx context.Context, config Config, schema string, create bool) (context.Context, error)

func NewContextTest

func NewContextTest(ctx context.Context, config Config) (context.Context, error)

func Ping

func Ping(ctx context.Context, client *bun.DB) error

Ping a database connection until it succeeds or the timeout is reached.

func RunInTx

func RunInTx(ctx context.Context, opts *sql.TxOptions, callback func(ctx context.Context, tx bun.IDB) error) error

func RunIsolatedTransactionalTest

func RunIsolatedTransactionalTest(t *testing.T, config Config, migrations fs.FS, callback TransactionalTestFunc)

RunIsolatedTransactionalTest runs test in a temporary throwaway schema. This allows for operations that cannot be performed concurrently in a transactional context, such as refreshing materialized views.

This method uses a separate schema, rather than a new database, so existing extensions are still available. It still requires to rerun the whole migration process, so unless needed, RunTransactionalTest should be preferred.

func RunMigrations

func RunMigrations(ctx context.Context, db *bun.DB, migrations fs.FS) error

RunMigrations runs all the migrations found in the provided filesystem.

func RunMigrationsContext

func RunMigrationsContext(ctx context.Context, migrations fs.FS) error

RunMigrationsContext runs all the migrations found in the provided filesystem, using the database connection from the context.

func RunTransactionalTest

func RunTransactionalTest(t *testing.T, config Config, callback TransactionalTestFunc)

RunTransactionalTest creates a special transactional context for testing. This context uses the PassthroughTx implementation, that allows for concurrent tests with the same database connection. It discards sub-transactions to prevent deadlocks.

func TransferContext

func TransferContext(baseCtx, destCtx context.Context) context.Context

TransferContext transfers the current postgres context into another. If the source context is not a postgres context, this is a no-op.

Types

type Config

type Config interface {
	DB(ctx context.Context) (*bun.DB, error)
	DBSchema(ctx context.Context, schema string, create bool) (*bun.DB, error)
}

type ContextKey

type ContextKey struct{}

type PassthroughTx

type PassthroughTx struct {
	bun.Tx
}

PassthroughTx is an extension of bun.Tx that prevents sub contexts from creating new sub-transactions.

Because postgresql does not support nested transactions, bun will create savepoints instead. Unlike top-level transactions, savepoints don't support parallelism (as they are part of the same query).

For testing, where the whole application is wrapped in a transaction, this can cause issues where multiple parallel calls to a method will attempt concurrent write on the same transaction.

To get around it, this package provides an alternative bun.IDB implementation, that does not create new transactions.

func NewPassthroughTx

func NewPassthroughTx(tx bun.Tx) *PassthroughTx

func (*PassthroughTx) Begin

func (tx *PassthroughTx) Begin() (bun.Tx, error)

func (*PassthroughTx) BeginTx

func (tx *PassthroughTx) BeginTx(_ context.Context, _ *sql.TxOptions) (bun.Tx, error)

func (*PassthroughTx) Commit

func (tx *PassthroughTx) Commit() error

func (*PassthroughTx) Rollback

func (tx *PassthroughTx) Rollback() error

func (*PassthroughTx) RunInTx

func (tx *PassthroughTx) RunInTx(
	ctx context.Context, _ *sql.TxOptions, fn func(ctx context.Context, tx bun.Tx) error,
) error

type TransactionalTestFunc

type TransactionalTestFunc func(context.Context, *testing.T)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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