startup_postgres

package
v2.2.96 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTransaction = errors.New("no transaction in context")
View Source
var ErrTransactionExistInContext = errors.New("transaction already exists in context")
View Source
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("panic(%#v)", r)
				}
			}

			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

func ContextWithTransaction(ctx context.Context, tx *sqlx.Tx) context.Context

Puts a transaction into a context and returns the new context with the transaction

func ErrIsForeignKeyViolation

func ErrIsForeignKeyViolation(err error) bool

func ErrIsUniqueViolation

func ErrIsUniqueViolation(err error) bool

func ExecuteInAnyTransaction added in v2.2.91

func ExecuteInAnyTransaction[R any](ctx context.Context, db TxStarter, fun func(context.Context, *sqlx.Tx) (R, error)) (R, error)

ExecuteInAnyTransaction checks the context for an existing transaction. If a transaction exists it will run the given operation in the transaction context. If no transaction exists, a new transaction will be created.

See ExecuteInNewTransaction regarding error handling.

func ExecuteInExistingTransaction added in v2.2.91

func ExecuteInExistingTransaction[R any](ctx context.Context, fun func(context.Context, *sqlx.Tx) (R, error)) (R, error)

ExecuteInExistingTransaction runs the given operation in the transaction that is hidden in the provided Context instance. If the context does not contain any transaction, ErrNoTransaction will be returned.

See ExecuteInNewTransaction regarding error handling.

func ExecuteInNewTransaction added in v2.2.91

func ExecuteInNewTransaction[R any](ctx context.Context, db TxStarter, fun func(context.Context, *sqlx.Tx) (R, error)) (R, error)

ExecuteInNewTransaction creates a new transaction and executes the given function within that transaction. The method will automatically rollback the transaction if no error is returned or otherwise commit it. This excludes the 'ErrNoRows' error. This error never triggers a rollback.

This behaviour can be overwritten by wrapping the error into NoRollback. The transaction will be committed in spite of the error present.

The caller can also trigger a rollback with no error present by simply calling Rollback on the transaction.

If the context already contains a transaction then ErrTransactionExistInContext will be returned as error and the actual operation will not be executed

func GetContext added in v2.2.91

func GetContext[T any](ctx context.Context, tx sqlx.QueryerContext, query string, args ...interface{}) (T, error)

GetContext runs the given query and parses the result into an object of type T. If not object can be found the method will return sql.ErrNoRows and a default value

func GetContextOrNull added in v2.2.91

func GetContextOrNull[T any](ctx context.Context, tx sqlx.QueryerContext, query string, args ...interface{}) (*T, error)

GetContextOrNull runs the given query and parses the result into an object of type T. If not object can be found the method will return sql.ErrNoRows and a value of nil.

func NoRollback added in v2.2.91

func NoRollback(err error) error

func TransactionFromContext

func TransactionFromContext(ctx context.Context) *sqlx.Tx

Gets the current transaction from the context or nil, if the context does not contain a transaction.

func Use added in v2.2.18

func Use(middleware Middleware)

func WithTransaction deprecated

func WithTransaction(db TxStarter, fn func(tx *sqlx.Tx) error) (err error)

WithTransaction 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)

func WithTransactionFromContext

func WithTransactionFromContext(ctx context.Context, operation func(tx *sqlx.Tx) error) error

Calls the given operation with the transaction that is currently stored in the context. Will fail with ErrNoTransaction if there is no transaction stored in the context.

Types

type Initializer

type Initializer func(db *sqlx.DB) error

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

Migration 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 Middleware added in v2.2.18

type Middleware func(driver.Driver) driver.Driver

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 {
		// 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

func (*PostgresOptions) StartVacuumTask

func (opts *PostgresOptions) StartVacuumTask(db *sqlx.DB, table string, interval time.Duration, clock clock.Clock) io.Closer

type TransactionCommitFn

type TransactionCommitFn func(ctx context.Context, tx *sqlx.Tx) (commit bool, err error)

type TransactionFn

type TransactionFn func(ctx context.Context, tx *sqlx.Tx) error

type TxHelper

type TxHelper struct {
	*sqlx.DB
}

func NewTxHelper

func NewTxHelper(db *sqlx.DB) TxHelper

func (*TxHelper) WithTransaction deprecated

func (h *TxHelper) WithTransaction(fn func(tx *sqlx.Tx) error) (err error)

Deprecated: Stop using this and start using the WithTransaction function that includes the context argument

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

type TxStarter

type TxStarter interface {
	BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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