Documentation
¶
Index ¶
- Constants
- Variables
- func GetContext(ctx context.Context) (bun.IDB, error)
- func NewContext(ctx context.Context, config Config) (context.Context, error)
- func NewContextSchema(ctx context.Context, config Config, schema string, create bool) (context.Context, error)
- func NewContextTest(ctx context.Context, config Config) (context.Context, error)
- func Ping(ctx context.Context, client *bun.DB) error
- func RunInTx(ctx context.Context, opts *sql.TxOptions, ...) error
- func RunIsolatedTransactionalTest(t *testing.T, config Config, migrations fs.FS, callback TransactionalTestFunc)
- func RunMigrations(ctx context.Context, db *bun.DB, migrations fs.FS) error
- func RunMigrationsContext(ctx context.Context, migrations fs.FS) error
- func RunTransactionalTest(t *testing.T, config Config, callback TransactionalTestFunc)
- func TransferContext(baseCtx, destCtx context.Context) context.Context
- type Config
- type ContextKey
- type PassthroughTx
- type TransactionalTestFunc
Constants ¶
const NameLen = 31
const PingTimeout = 10 * time.Second
Variables ¶
var ErrNoDbInContext = errors.New("context does not contain a bun.DB")
Functions ¶
func NewContextSchema ¶
func NewContextTest ¶
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 ¶
RunMigrations runs all the migrations found in the provided filesystem.
func RunMigrationsContext ¶
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.
Types ¶
type ContextKey ¶
type ContextKey struct{}
type PassthroughTx ¶
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) Commit ¶
func (tx *PassthroughTx) Commit() error
func (*PassthroughTx) Rollback ¶
func (tx *PassthroughTx) Rollback() error