Documentation ¶
Index ¶
- Variables
- func ContextWithTransaction(ctx context.Context, tx *sqlx.Tx) context.Context
- func ErrIsForeignKeyViolation(err error) bool
- func ErrIsUniqueViolation(err error) bool
- func GuessDriverName() string
- func TransactionFromContext(ctx context.Context) *sqlx.Tx
- func WithTransaction(db TxStarter, fn func(tx *sqlx.Tx) error) (err error)deprecated
- func WithTransactionAutoCommitContext(ctx context.Context, db TxStarter, operation TransactionFn) (err error)
- func WithTransactionFromContext(ctx context.Context, operation func(tx *sqlx.Tx) error) error
- type Initializer
- type PostgresOptions
- type TransactionCommitFn
- type TransactionFn
- type TxHelper
- type TxStarter
Constants ¶
This section is empty.
Variables ¶
var ErrNoTransaction = errors.New("no transaction in context")
var WithTransactionContext = func(ctx context.Context, db TxStarter, operation TransactionCommitFn) (err error) { var tx *sqlx.Tx var commit bool tx, err = db.BeginTxx(ctx, nil) if err != nil { return errors.WithMessage(err, "begin transaction") } defer func() { r := recover() if r == nil && err == nil && commit { if err = tx.Commit(); err != nil { err = errors.WithMessage(err, "commit") } } else { if err := tx.Rollback(); err != nil { logrus.Warnf("Could not rollback transaction: %s", err) } if r != nil { var ok bool if err, ok = r.(error); !ok { err = fmt.Errorf("%#v", err) } } err = errors.WithMessage(err, "transaction") } }() commit, err = operation(ContextWithTransaction(ctx, tx), tx) return }
Creates a new transaction, puts it into the context and runs the given operation with the context and the transaction. This is a var so that you are able to replace it with your own function to enable tracing during initialization of your application.
Functions ¶
func ContextWithTransaction ¶
Puts a transaction into a context and returns the new context with the transaction
func ErrIsUniqueViolation ¶
func GuessDriverName ¶ added in v2.1.6
func GuessDriverName() string
func TransactionFromContext ¶
Gets the current transaction from the context or nil, if the context does not contain a transaction.
func WithTransaction
deprecated
Ends the given transaction. This method will either commit the transaction if the given recoverValue is nil, or rollback the transaction if it is non nil.
Deprecated: Use a variant of this function that includes a context argument.
func WithTransactionAutoCommitContext ¶
func WithTransactionAutoCommitContext(ctx context.Context, db TxStarter, operation TransactionFn) (err error)
Types ¶
type Initializer ¶
func DefaultMigration ¶
func DefaultMigration(table string) Initializer
Creates an Initializer that performs a database migration by looking for sql files in the default directories.
func Migration ¶
func Migration(table, directory string) Initializer
Runs a migration with the sql files from the given directory. The directory must exist. The migration library will use the given table name to store the migration progress
type PostgresOptions ¶
type PostgresOptions struct { URL string `long:"postgres" default:"postgres://postgres:postgres@localhost:5432?sslmode=disable" description:"Postgres server url."` PoolSize int `` /* 133-byte string literal not displayed */ ConnectionLifetime time.Duration `long:"postgres-lifetime" default:"10m" description:"Maximum time a connection in the pool can be used."` Inputs struct { // the driver name to use. If this is empty, // we select the default of 'postgres' or 'pgx', // depending on availability DriverName string // An optional initializer. This might be used to do // database migration or stuff. Initializer Initializer } // contains filtered or unexported fields }
func (*PostgresOptions) Connection ¶
func (opts *PostgresOptions) Connection() *sqlx.DB
type TransactionCommitFn ¶
type TxHelper ¶
func NewTxHelper ¶
func (*TxHelper) WithTransactionContext ¶
func (h *TxHelper) WithTransactionContext(ctx context.Context, operation TransactionCommitFn) error
func (*TxHelper) WithTransactionContextAutoCommit ¶
func (h *TxHelper) WithTransactionContextAutoCommit(ctx context.Context, operation TransactionFn) error