sqlutils

package
v0.0.0-...-1b6ad0c Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const TestDB = "test"

TestDB is the name of the database created for test tables.

Variables

This section is empty.

Functions

func CreateTable

func CreateTable(
	tb testing.TB, sqlDB *gosql.DB, tableName, schema string, numRows int, fn GenRowFn,
)

CreateTable creates a table in the "test" database with the given number of rows and using the given row generation function.

func CreateTableDebug

func CreateTableDebug(
	tb testing.TB,
	sqlDB *gosql.DB,
	tableName, schema string,
	numRows int,
	fn GenRowFn,
	shouldPrint bool,
)

CreateTableDebug is identical to debug, but allows for the added option of printing the table and its contents upon creation.

func CreateTableInterleaved

func CreateTableInterleaved(
	tb testing.TB,
	sqlDB *gosql.DB,
	tableName, schema, interleaveSchema string,
	numRows int,
	fn GenRowFn,
)

CreateTableInterleaved is identical to CreateTable with the added option of specifying an interleave schema for interleaving the table.

func CreateTableInterleavedDebug

func CreateTableInterleavedDebug(
	tb testing.TB,
	sqlDB *gosql.DB,
	tableName, schema, interleaveSchema string,
	numRows int,
	fn GenRowFn,
	shouldPrint bool,
)

CreateTableInterleavedDebug is identical to CreateTableInterleaved with the option of printing the table being created.

func CreateTestInterleavedHierarchy

func CreateTestInterleavedHierarchy(t *testing.T, sqlDB *gosql.DB)

CreateTestInterleavedHierarchy generates the following interleaved hierarchy for testing:

<table>		  <primary index/interleave prefix>   <nrows>
parent1		  (pid1)			      100
  child1		  (pid1, cid1, cid2)		      250
    grandchild1	  (pid1, cid1, cid2, gcid1)	      1000
  child2		  (pid1, cid3, cid4)		      50
parent2		  (pid1)			      20

func DeleteZoneConfig

func DeleteZoneConfig(t testing.TB, sqlDB *SQLRunner, target string)

DeleteZoneConfig deletes the specified zone config through the SQL interface.

func IntToEnglish

func IntToEnglish(val int) string

IntToEnglish returns an English (pilot style) string for the given integer, for example:

IntToEnglish(135) = "one-three-five"

func MatrixToStr

func MatrixToStr(rows [][]string) string

MatrixToStr converts a set of rows into a single string where each row is on a separate line and the columns with a row are comma separated.

func PGUrl

func PGUrl(t testing.TB, servingAddr, prefix string, user *url.Userinfo) (url.URL, func())

PGUrl returns a postgres connection url which connects to this server with the given user, and a cleanup function which must be called after all connections created using the connection url have been closed.

In order to connect securely using postgres, this method will create temporary on-disk copies of certain embedded security certificates. The certificates will be created in a new temporary directory. The returned cleanup function will delete this temporary directory. Note that two calls to this function for the same `user` will generate different copies of the certificates, so the cleanup function must always be called.

Args:

prefix: A prefix to be prepended to the temp file names generated, for debugging.

func PGUrlWithOptionalClientCerts

func PGUrlWithOptionalClientCerts(
	t testing.TB, servingAddr, prefix string, user *url.Userinfo, withClientCerts bool,
) (url.URL, func())

PGUrlWithOptionalClientCerts is like PGUrl but the caller can customize whether the client certificates are loaded on-disk and in the URL.

func QueryDatabaseID

func QueryDatabaseID(t testing.TB, sqlDB DBHandle, dbName string) uint32

QueryDatabaseID returns the database ID of the specified database using the system.namespace table.

func QueryTableID

func QueryTableID(
	t testing.TB, sqlDB DBHandle, dbName, schemaName string, tableName string,
) uint32

QueryTableID returns the table ID of the specified database.table using the system.namespace table.

func RemoveAllZoneConfigs

func RemoveAllZoneConfigs(t testing.TB, sqlDB *SQLRunner)

RemoveAllZoneConfigs removes all installed zone configs.

func RowEnglishFn

func RowEnglishFn(row int) ast.Datum

RowEnglishFn is a GenValueFn which returns an English representation of the row number, as a DString

func RowIdxFn

func RowIdxFn(row int) ast.Datum

RowIdxFn is a GenValueFn that returns the row number as a DInt

func RowsToStrMatrix

func RowsToStrMatrix(rows *gosql.Rows) ([][]string, error)

RowsToStrMatrix converts the given result rows to a string matrix; nulls are represented as "NULL". Empty results are represented by an empty (but non-nil) slice.

func RunResolveColumnItemTest

func RunResolveColumnItemTest(t *testing.T, ct ColumnItemResolverTester)

RunResolveColumnItemTest tests that the given ColumnItemResolverTester correctly resolves column names.

func RunResolveQualifiedStarTest

func RunResolveQualifiedStarTest(t *testing.T, ct ColumnItemResolverTester)

RunResolveQualifiedStarTest tests that the given ColumnItemResolverTester correctly resolves names of the form "<tableName>.*".

func RunScrub

func RunScrub(sqlDB *gosql.DB, database string, table string) error

RunScrub will run execute an exhaustive scrub check for a table.

func RunScrubWithOptions

func RunScrubWithOptions(sqlDB *gosql.DB, database string, table string, options string) error

RunScrubWithOptions will run a SCRUB check for a table with the specified options string.

func SetZoneConfig

func SetZoneConfig(t testing.TB, sqlDB *SQLRunner, target string, config string)

SetZoneConfig updates the specified zone config through the SQL interface.

func TxnSetZoneConfig

func TxnSetZoneConfig(t testing.TB, sqlDB *SQLRunner, txn *gosql.Tx, target string, config string)

TxnSetZoneConfig updates the specified zone config through the SQL interface using the provided transaction.

func VerifyAllZoneConfigs

func VerifyAllZoneConfigs(t testing.TB, sqlDB *SQLRunner, rows ...ZoneRow)

VerifyAllZoneConfigs verifies that the specified ZoneRows exactly match the list of active zone configs.

func VerifyStatementPrettyRoundtrip

func VerifyStatementPrettyRoundtrip(t *testing.T, sql string)

VerifyStatementPrettyRoundtrip verifies that the SQL statements in s correctly round trip through the pretty printer.

func VerifyZoneConfigForTarget

func VerifyZoneConfigForTarget(t testing.TB, sqlDB *SQLRunner, target string, row ZoneRow)

VerifyZoneConfigForTarget verifies that the specified zone matches the specified ZoneRow.

func ZoneConfigExists

func ZoneConfigExists(t testing.TB, sqlDB *SQLRunner, name string) bool

ZoneConfigExists returns whether a zone config with the provided name exists.

Types

type ColumnItemResolverTester

type ColumnItemResolverTester interface {
	// GetColumnItemResolver returns the ast.ColumnItemResolver. Since any
	// struct implementing ColumnItemResolverTester should also implement
	// ast.ColumnItemResolver, this is basically an identity function.
	GetColumnItemResolver() ast.ColumnItemResolver

	// AddTable adds a table with the given column names to the
	// ast.ColumnItemResolver.
	AddTable(tabName ast.TableName, colNames []ast.Name)

	// ResolveQualifiedStarTestResults returns the results of running
	// RunResolveQualifiedStarTest on the ast.ColumnItemResolver.
	ResolveQualifiedStarTestResults(
		srcName *ast.TableName, srcMeta ast.ColumnSourceMeta,
	) (string, string, error)

	// ResolveColumnItemTestResults returns the results of running
	// RunResolveColumnItemTest on the ast.ColumnItemResolver.
	ResolveColumnItemTestResults(colRes ast.ColumnResolutionResult) (string, error)
}

ColumnItemResolverTester is an interface that should be implemented by any struct that also implements ast.ColumnItemResolver. It is used to test that the implementation of ast.ColumnItemResolver is correct.

type DBHandle

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

DBHandle is an interface that applies to *gosql.DB, *gosql.Conn, and *gosql.Tx, as well as *RoundRobinDBHandle.

type GenRowFn

type GenRowFn func(row int) []ast.Datum

GenRowFn is a function that takes a (1-based) row index and returns a row of Datums that will be converted to strings to form part of an INSERT statement.

func ToRowFn

func ToRowFn(fn ...GenValueFn) GenRowFn

ToRowFn creates a GenRowFn that returns rows of values generated by the given GenValueFns (one per column).

type GenValueFn

type GenValueFn func(row int) ast.Datum

GenValueFn is a function that takes a (1-based) row index and returns a Datum which will be converted to a string to form part of an INSERT statement.

func RowModuloFn

func RowModuloFn(modulo int) GenValueFn

RowModuloFn creates a GenValueFn that returns the row number modulo a given value as a DInt

func RowModuloShiftedFn

func RowModuloShiftedFn(modulo ...int) GenValueFn

RowModuloShiftedFn creates a GenValueFn that uses the following recursive function definition F(row, modulo), where modulo is []int

F(row, [])      = row
F(row, modulo)  = F((row - 1) % modulo[0] + 1, modulo[1:])

and returns the result as a DInt.

type RoundRobinDBHandle

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

RoundRobinDBHandle aggregates multiple DBHandles into a single one; each time a query is issued, a handle is selected in round-robin fashion.

func MakeRoundRobinDBHandle

func MakeRoundRobinDBHandle(handles ...DBHandle) *RoundRobinDBHandle

MakeRoundRobinDBHandle creates a RoundRobinDBHandle.

func (*RoundRobinDBHandle) ExecContext

func (rr *RoundRobinDBHandle) ExecContext(
	ctx context.Context, query string, args ...interface{},
) (gosql.Result, error)

ExecContext is part of the DBHandle interface.

func (*RoundRobinDBHandle) QueryContext

func (rr *RoundRobinDBHandle) QueryContext(
	ctx context.Context, query string, args ...interface{},
) (*gosql.Rows, error)

QueryContext is part of the DBHandle interface.

func (*RoundRobinDBHandle) QueryRowContext

func (rr *RoundRobinDBHandle) QueryRowContext(
	ctx context.Context, query string, args ...interface{},
) *gosql.Row

QueryRowContext is part of the DBHandle interface.

type Row

type Row struct {
	testing.TB
	// contains filtered or unexported fields
}

Row is a wrapper around gosql.Row that kills the test on error.

func (*Row) Scan

func (r *Row) Scan(dest ...interface{})

Scan is a wrapper around (*gosql.Row).Scan that kills the test on error.

type SQLRunner

type SQLRunner struct {
	DB DBHandle
}

SQLRunner wraps a testing.TB and *gosql.DB connection and provides convenience functions to run SQL statements and fail the test on any errors.

func MakeRoundRobinSQLRunner

func MakeRoundRobinSQLRunner(dbs ...DBHandle) *SQLRunner

MakeRoundRobinSQLRunner returns a SQLRunner that uses a set of database connections, in a round-robin fashion.

func MakeSQLRunner

func MakeSQLRunner(db DBHandle) *SQLRunner

MakeSQLRunner returns a SQLRunner for the given database connection. The argument can be a *gosql.DB, *gosql.Conn, or *gosql.Tx object.

func (*SQLRunner) CheckQueryResults

func (sr *SQLRunner) CheckQueryResults(t testing.TB, query string, expected [][]string)

CheckQueryResults checks that the rows returned by a query match the expected response.

func (*SQLRunner) CheckQueryResultsRetry

func (sr *SQLRunner) CheckQueryResultsRetry(t testing.TB, query string, expected [][]string)

CheckQueryResultsRetry checks that the rows returned by a query match the expected response. If the results don't match right away, it will retry using testutils.SucceedsSoon.

func (*SQLRunner) Exec

func (sr *SQLRunner) Exec(t testing.TB, query string, args ...interface{}) gosql.Result

Exec is a wrapper around gosql.Exec that kills the test on error.

func (*SQLRunner) ExecRowsAffected

func (sr *SQLRunner) ExecRowsAffected(
	t testing.TB, expRowsAffected int, query string, args ...interface{},
)

ExecRowsAffected executes the statement and verifies that RowsAffected() matches the expected value. It kills the test on errors.

func (*SQLRunner) ExecSucceedsSoon

func (sr *SQLRunner) ExecSucceedsSoon(t testing.TB, query string, args ...interface{})

ExecSucceedsSoon is a wrapper around gosql.Exec that wraps the exec in a succeeds soon.

func (*SQLRunner) ExpectErr

func (sr *SQLRunner) ExpectErr(t testing.TB, errRE string, query string, args ...interface{})

ExpectErr runs the given statement and verifies that it returns an error matching the given regex.

func (*SQLRunner) ExpectErrSucceedsSoon

func (sr *SQLRunner) ExpectErrSucceedsSoon(
	t testing.TB, errRE string, query string, args ...interface{},
)

ExpectErrSucceedsSoon wraps ExpectErr with a SucceedsSoon.

func (*SQLRunner) Query

func (sr *SQLRunner) Query(t testing.TB, query string, args ...interface{}) *gosql.Rows

Query is a wrapper around gosql.Query that kills the test on error.

func (*SQLRunner) QueryRow

func (sr *SQLRunner) QueryRow(t testing.TB, query string, args ...interface{}) *Row

QueryRow is a wrapper around gosql.QueryRow that kills the test on error.

func (*SQLRunner) QueryStr

func (sr *SQLRunner) QueryStr(t testing.TB, query string, args ...interface{}) [][]string

QueryStr runs a Query and converts the result using RowsToStrMatrix. Kills the test on errors.

type ScrubResult

type ScrubResult struct {
	ErrorType  string
	Database   string
	Table      string
	PrimaryKey string
	Timestamp  time.Time
	Repaired   bool
	Details    string
}

ScrubResult is the go struct for the row results for an EXPERIMENTAL SCRUB query.

func GetScrubResultRows

func GetScrubResultRows(rows *gosql.Rows) (results []ScrubResult, err error)

GetScrubResultRows will scan and unmarshal ScrubResults from a Rows iterator. The Rows iterate must from an EXPERIMENTAL SCRUB query.

type ZoneRow

type ZoneRow struct {
	ID     uint32
	Config zonepb.ZoneConfig
}

ZoneRow represents a row returned by SHOW ZONE CONFIGURATION.

Jump to

Keyboard shortcuts

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