glsql

package
v14.3.4 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package glsql (Gitaly SQL) is a helper package to work with plain SQL queries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDBConfig

func GetDBConfig(t testing.TB, database string) config.DB

GetDBConfig returns the database configuration determined by environment variables. See NewDB() for the list of variables.

func Migrate

func Migrate(db *sql.DB, ignoreUnknown bool) (int, error)

Migrate will apply all pending SQL migrations.

func OpenDB

func OpenDB(conf config.DB) (*sql.DB, error)

OpenDB returns connection pool to the database.

func ScanAll

func ScanAll(rows *sql.Rows, in DestProvider) (err error)

ScanAll reads all data from 'rows' into holders provided by 'in'.

Types

type DB

type DB struct {
	*sql.DB
	// Name is a name of the database.
	Name string
}

DB is a helper struct that should be used only for testing purposes.

func NewDB added in v14.3.0

func NewDB(t testing.TB) DB

NewDB returns a wrapper around the database connection pool. Must be used only for testing. The new database with empty relations will be created for each call of this function. It uses env vars:

PGHOST - required, URL/socket/dir
PGPORT - required, binding port
PGUSER - optional, user - `$ whoami` would be used if not provided

Once the test is completed the database will be dropped on test cleanup execution.

func (DB) Begin added in v14.2.0

func (db DB) Begin(t testing.TB) *TxWrapper

Begin starts a new transaction and returns it wrapped into TxWrapper.

func (DB) Close

func (db DB) Close() error

Close removes schema if it was used and releases connection pool.

func (DB) MustExec

func (db DB) MustExec(t testing.TB, q string, args ...interface{})

MustExec executes `q` with `args` and verifies there are no errors.

func (DB) RequireRowsInTable

func (db DB) RequireRowsInTable(t *testing.T, tname string, n int)

RequireRowsInTable verifies that `tname` table has `n` amount of rows in it.

func (DB) Truncate

func (db DB) Truncate(t testing.TB, tables ...string)

Truncate removes all data from the list of tables and restarts identities for them.

func (DB) TruncateAll

func (db DB) TruncateAll(t testing.TB)

TruncateAll removes all data from known set of tables.

type DestProvider

type DestProvider interface {
	// To returns list of pointers.
	// It is not an idempotent operation and each call will return a new list.
	To() []interface{}
}

DestProvider returns list of pointers that will be used to scan values into.

type ListenHandler

type ListenHandler interface {
	// Notification would be triggered once a new notification received.
	Notification(Notification)
	// Disconnect would be triggered once a connection to remote service is lost.
	// Passed in error will never be nil and will contain cause of the disconnection.
	Disconnect(error)
	// Connected would be triggered once a connection to remote service is established.
	Connected()
}

ListenHandler contains a set of methods that would be called on corresponding notifications received.

type MockQuerier

type MockQuerier struct {
	ExecContextFunc  func(context.Context, string, ...interface{}) (sql.Result, error)
	QueryContextFunc func(context.Context, string, ...interface{}) (*sql.Rows, error)
	Querier
}

MockQuerier allows for mocking database operations out.

func (MockQuerier) ExecContext

func (m MockQuerier) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext runs the mock's ExecContextFunc.

func (MockQuerier) QueryContext

func (m MockQuerier) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext runs the mock's QueryContextFunc.

type Notification

type Notification struct {
	// Channel is a name of the receiving channel.
	Channel string
	// Payload is a payload of the notification.
	Payload string
}

Notification represent a notification from the database.

type Querier

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

Querier is an abstraction on *sql.DB and *sql.Tx that allows to use their methods without awareness about actual type.

type StringProvider

type StringProvider []*string

StringProvider allows ScanAll to read all rows and return the result as a slice.

func (*StringProvider) To

func (p *StringProvider) To() []interface{}

To returns a list of pointers that will be used as a destination for scan operation.

func (*StringProvider) Values

func (p *StringProvider) Values() []string

Values returns list of values read from *sql.Rows

type TxWrapper added in v14.2.0

type TxWrapper struct {
	*sql.Tx
}

TxWrapper is a simple wrapper around *sql.Tx.

func (*TxWrapper) Commit added in v14.2.0

func (txw *TxWrapper) Commit(t testing.TB)

Commit executes Commit operation on the wrapped *sql.Tx if it is set. After execution is sets Tx to nil to prevent errors on the deferred invocations (useful for testing when Rollback is deferred).

func (*TxWrapper) Rollback added in v14.2.0

func (txw *TxWrapper) Rollback(t testing.TB)

Rollback executes Rollback operation on the wrapped *sql.Tx if it is set. After execution is sets Tx to nil to prevent errors on the repeated invocations (useful for testing when Rollback is deferred).

type Uint64Provider

type Uint64Provider []*uint64

Uint64Provider allows to use it with ScanAll function to read all rows into it and return result as a slice.

func (*Uint64Provider) To

func (p *Uint64Provider) To() []interface{}

To returns a list of pointers that will be used as a destination for scan operation.

func (*Uint64Provider) Values

func (p *Uint64Provider) Values() []uint64

Values returns list of values read from *sql.Rows

Jump to

Keyboard shortcuts

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