postgres

package
v0.19.7 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: AGPL-3.0 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 added in v0.6.0

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

func NewContextSchema added in v0.6.0

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

func NewContextTest added in v0.6.0

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

func Ping added in v0.6.0

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

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

func RunInTx added in v0.3.0

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

func RunIsolatedTransactionalTest added in v0.3.1

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 added in v0.6.0

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

RunMigrations runs all the migrations found in the provided filesystem.

func RunMigrationsContext added in v0.6.0

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 added in v0.3.1

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 added in v0.16.0

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 added in v0.2.1

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 added in v0.2.1

func NewPassthroughTx(tx bun.Tx) *PassthroughTx

func (*PassthroughTx) Begin added in v0.2.1

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

func (*PassthroughTx) BeginTx added in v0.2.1

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

func (*PassthroughTx) Commit added in v0.2.1

func (tx *PassthroughTx) Commit() error

func (*PassthroughTx) Rollback added in v0.2.1

func (tx *PassthroughTx) Rollback() error

func (*PassthroughTx) RunInTx added in v0.2.1

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

type TransactionalTestFunc added in v0.3.1

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