storage

package
v0.0.0-...-3f4bbdd Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TimeoutPingWaiter func(context.Context, time.Duration) WaiterFunc = func(parentCtx context.Context, timeout time.Duration) WaiterFunc {
	var err error
	ctx, cancel := context.WithTimeout(parentCtx, timeout)
	return func(db *sql.DB) (bool, error) {
		for {
			select {
			case <-ctx.Done():
				cancel()
				if err != nil {
					err = errors.New(ctx.Err().Error() + ": " + err.Error())
				}
				return false, err
			case <-time.After(1 * time.Second):
				if err = db.PingContext(ctx); err == nil {
					cancel()
					return false, nil
				}
				return true, err
			}
		}
	}
}

TimeoutPingWaiter waits until the DB is available or fails on timeout

Functions

func Migrate

func Migrate(source string, db *sql.DB) error

func NewMysqlConfig

func NewMysqlConfig() *mysql.Config

NewMysqlConfig initializes new MySQL connection configuration with sane defaults

func NewMysqlStorage

func NewMysqlStorage(config *mysql.Config) (*mysqlStorage, error)

NewMysqlStorage creates and initializes new MySQL storage instance

func NewTestDatabase

func NewTestDatabase(ctx context.Context, t *testing.T) (*mysqlStorage, string)

Types

type Account

type Account struct {
	ID           int64
	Name         string
	BalanceCents int64
	BIC          string
	IBAN         string
}

type Querier

type Querier interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

type Storage

type Storage interface {
	// WithTransaction wraps functions in transaction and rolls it back if function returns error
	WithTransaction(context.Context, func(context.Context, Querier) error) error
	WithTransactionStorage(context.Context, func(context.Context, Storage) error) error

	CreateAccount(ctx context.Context, name, iban, bic string, initialBalanceCents int64) (int64, error)
	FindAccount(ctx context.Context, id int64) (Account, error)
	UpdateAccountBalance(ctx context.Context, id, balance int64) error
	FindAccountByIBAN(ctx context.Context, iban string) (Account, error)

	FindAccountTransactions(ctx context.Context, id int64) ([]*Transaction, error)
	AppendAccountTransactions(ctx context.Context, transactions []*Transaction) error

	// Wait runs provided wait function until it returns true without error
	Wait(f WaiterFunc) error
}

Storage defines interface to be satisfied by concrete storage implementation

type Transaction

type Transaction struct {
	ID               int64
	CounterpartyName string
	CounterpartyIBAN string
	CounterpartyBIC  string
	AmountCents      int64
	AmountCurrency   string
	BankAccountID    int64
	Description      string
}

type WaiterFunc

type WaiterFunc func(db *sql.DB) (bool, error)

Jump to

Keyboard shortcuts

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