database

package
v0.0.0-...-51ec7a6 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package database owns Soro's PostgreSQL pool, Bun bridge, and transactions.

Index

Constants

This section is empty.

Variables

View Source
var ErrRollbackOnly = errors.New("database: transaction marked rollback-only")
View Source
var ErrTransactionCallbackPanic = errors.New("database: transaction callback panicked")

Functions

func CreateDatabase

func CreateDatabase(ctx context.Context, databaseURL string) error

CreateDatabase creates the database named by databaseURL by connecting to PostgreSQL's maintenance database. The database name is parsed by pgx and quoted as an identifier rather than accepted as SQL.

func DropDatabase

func DropDatabase(ctx context.Context, databaseURL string) error

DropDatabase physically removes the database named by databaseURL. Callers are expected to require an explicit confirmation before invoking it.

func IsCommitted

func IsCommitted(err error) bool

func IsRollbackFailure

func IsRollbackFailure(err error) bool

Types

type Callback

type Callback func(context.Context) error

type CommittedError

type CommittedError struct{ Err error }

CommittedError reports a failure in an after-commit callback. Database work is already durable and must not be retried as though it rolled back.

func (*CommittedError) Error

func (err *CommittedError) Error() string

func (*CommittedError) Unwrap

func (err *CommittedError) Unwrap() error

type DB

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

DB owns one pgx pool and the database/sql facade used by Bun.

func Open

func Open(ctx context.Context, settings config.DatabaseConfig) (*DB, error)

func Wrap

func Wrap(bunDB *bun.DB) (*DB, error)

Wrap creates a Soro database around an existing Bun database. The wrapper owns the Bun database but has no directly exposed pgx pool.

func (*DB) AfterCommit

func (db *DB) AfterCommit(ctx context.Context, callback Callback) error

func (*DB) AfterRollback

func (db *DB) AfterRollback(ctx context.Context, callback Callback) error

func (*DB) Bun

func (db *DB) Bun() *bun.DB

func (*DB) Close

func (db *DB) Close() error

func (*DB) Hooks

func (db *DB) Hooks() *lifecycle.Registry

func (*DB) IDB

func (db *DB) IDB(ctx context.Context) bun.IDB

func (*DB) InTransaction

func (db *DB) InTransaction(ctx context.Context) bool

func (*DB) IndependentRequestTransaction

func (db *DB) IndependentRequestTransaction(ctx context.Context, initialize RequestTransactionInitializer, fn func(context.Context) error) error

IndependentRequestTransaction starts a new request transaction even when ctx already carries one for the same DB. This is an explicit durability boundary for append-only journals and progressive execution records that must become visible before the surrounding HTTP transaction completes.

The new transaction uses another pooled connection and inherits cancellation and all non-transaction context values. Callers must avoid touching rows locked by the surrounding transaction.

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

func (*DB) Pool

func (db *DB) Pool() *pgxpool.Pool

Pool exposes the shared pool intended for pgx-native integrations such as River.

func (*DB) RequestTransaction

func (db *DB) RequestTransaction(ctx context.Context, initialize RequestTransactionInitializer, fn func(context.Context) error) error

RequestTransaction runs fn in a transaction initialized before any request work. Nested Repository.Transaction calls join this transaction and preserve the existing rollback-only behavior.

func (*DB) SQL

func (db *DB) SQL() *sql.DB

SQL exposes the shared database/sql facade used by Bun. Integrations must not close it independently of DB.

func (*DB) SQLTx

func (db *DB) SQLTx(ctx context.Context) (*sql.Tx, bool)

SQLTx returns the active database/sql transaction carried by ctx.

func (*DB) Transaction

func (db *DB) Transaction(ctx context.Context, fn func(context.Context) error) error

Transaction runs fn in a transaction. Nested calls join the current transaction and mark it rollback-only if they return an error.

func (*DB) Validator

func (db *DB) Validator() *validation.Engine

type LocalSettings

type LocalSettings interface {
	Set(context.Context, string, string) error
}

LocalSettings exposes the only database operation needed to initialize request-local PostgreSQL context. Values are always applied with set_config(..., true), so they cannot survive the transaction or leak through a pooled connection.

type RequestTransactionInitializer

type RequestTransactionInitializer func(context.Context, LocalSettings) (context.Context, error)

RequestTransactionInitializer configures application-owned database context immediately after a request transaction begins and may enrich the context passed to handlers.

type RollbackFailure

type RollbackFailure struct {
	Cause       error
	RollbackErr error
	HookErr     error
}

RollbackFailure reports that transaction work failed and rollback or an after-rollback callback also failed.

func (*RollbackFailure) Error

func (err *RollbackFailure) Error() string

func (*RollbackFailure) Unwrap

func (err *RollbackFailure) Unwrap() []error

Jump to

Keyboard shortcuts

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