dbtestutil

package
v2.28.4 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: AGPL-3.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const CoderTestingDBName = "coder_testing"

Variables

View Source
var (
	DefaultBroker = Broker{}
)

These variables are global because all tests share them.

View Source
var DefaultTimezone = "Canada/Newfoundland"

Functions

func DisableForeignKeysAndTriggers deprecated added in v2.19.0

func DisableForeignKeysAndTriggers(t *testing.T, db database.Store)

Deprecated: disable foreign keys was created to aid in migrating off of the test-only in-memory database. Do not use this in new code.

func DumpOnFailure added in v2.2.0

func DumpOnFailure(t testing.TB, connectionURL string)

DumpOnFailure exports the database referenced by connectionURL to a file corresponding to the current test, with a suffix indicating the time the test was run. To import this into a new database (assuming you have already run make test-postgres-docker):

  • Create a new test database: go run ./scripts/migrate-ci/main.go and note the database name it outputs
  • Import the file into the above database: psql 'postgres://postgres:postgres@127.0.0.1:5432/<dbname>?sslmode=disable' -f <path to file.test.sql>
  • Run a dev server against that database: ./scripts/coder-dev.sh server --postgres-url='postgres://postgres:postgres@127.0.0.1:5432/<dbname>?sslmode=disable'

func NewDB

func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub)

func NewDBWithSQLDB added in v2.3.1

func NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub, *sql.DB)

func NowInDefaultTimezone added in v2.19.0

func NowInDefaultTimezone() time.Time

NowInDefaultTimezone returns the current time rounded to the nearest microsecond in the default timezone used by postgres in tests. Useful for object equality checks.

func Open added in v2.10.0

func Open(t TBSubset, opts ...OpenOption) (string, error)

Open creates a new PostgreSQL database instance. If there's a database running at localhost:5432, it will use that. Otherwise, it will start a new postgres container.

func OpenContainerized added in v2.10.0

func OpenContainerized(t TBSubset, opts DBContainerOptions) (string, func(), error)

OpenContainerized creates a new PostgreSQL server using a Docker container. If port is nonzero, forward host traffic to that port to the database. If port is zero, allocate a free port from the OS. The user is responsible for calling the returned cleanup function.

func PGDump added in v2.11.0

func PGDump(dbURL string) ([]byte, error)

PGDump runs pg_dump against dbURL and returns the output. It is used by DumpOnFailure().

func PGDumpSchemaOnly added in v2.11.0

func PGDumpSchemaOnly(dbURL string) ([]byte, error)

PGDumpSchemaOnly is for use by gen/dump only. It runs pg_dump against dbURL and sets a consistent timezone and encoding.

func RunCleaner added in v2.27.0

func RunCleaner()

RunCleaner runs the test database cleaning process. It takes no arguments but uses stdio and environment variables for its operation.

The cleaner is designed to run in a separate process from the main test suite, connected over stdio. If the main test process ends (panics, times out, or is killed) without explicitly discarding the databases it clones, the cleaner removes them so they don't leak beyond the test session. c.f. https://github.com/coder/internal/issues/927

Types

type Broker added in v2.27.0

type Broker struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*Broker) Create added in v2.27.0

func (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error)

type ConnectionParams added in v2.18.0

type ConnectionParams struct {
	Username string
	Password string
	Host     string
	Port     string
	DBName   string
}

func (ConnectionParams) DSN added in v2.18.0

func (p ConnectionParams) DSN() string

type Connector added in v2.15.0

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

func (*Connector) Connect added in v2.15.0

func (c *Connector) Connect(_ context.Context) (driver.Conn, error)

func (*Connector) Dialer added in v2.15.0

func (c *Connector) Dialer(dialer pq.Dialer)

func (*Connector) Driver added in v2.15.0

func (c *Connector) Driver() driver.Driver

type DBContainerOptions added in v2.18.0

type DBContainerOptions struct {
	Port int
	Name string
}

type DBTx added in v2.17.0

type DBTx struct {
	database.Store
	// contains filtered or unexported fields
}

func StartTx added in v2.17.0

func StartTx(t *testing.T, db database.Store, opts *database.TxOptions) *DBTx

StartTx starts a transaction and returns a DBTx object. This allows running 2 transactions concurrently in a test more easily. Example:

a := StartTx(t, db, opts)
b := StartTx(t, db, opts)

a.GetUsers(...)
b.GetUsers(...)

require.NoError(t, a.Done()

func (*DBTx) Done added in v2.17.0

func (tx *DBTx) Done() error

Done can only be called once. If you call it twice, it will panic.

type Driver added in v2.15.0

type Driver struct {
	Connections chan driver.Conn
}

func NewDriver added in v2.15.0

func NewDriver() *Driver

func (*Driver) Close added in v2.15.0

func (d *Driver) Close()

func (*Driver) Connector added in v2.15.0

func (d *Driver) Connector(name string) (driver.Connector, error)

func (*Driver) Open added in v2.15.0

func (d *Driver) Open(name string) (driver.Conn, error)

type OpenOption added in v2.18.0

type OpenOption func(*OpenOptions)

func WithDBFrom added in v2.18.0

func WithDBFrom(dbFrom string) OpenOption

WithDBFrom sets the template database to use when creating a new database. Overrides the DB_FROM environment variable.

func WithLogDSN added in v2.26.0

func WithLogDSN(logDSN bool) OpenOption

WithLogDSN sets whether the DSN should be logged during testing. This provides an ergonomic way to connect to test databases during debugging.

type OpenOptions added in v2.18.0

type OpenOptions struct {
	DBFrom *string
	LogDSN bool
}

type Option added in v2.2.0

type Option func(*options)

func WithDumpOnFailure added in v2.2.0

func WithDumpOnFailure() Option

WithDumpOnFailure will dump the entire database on test failure.

func WithLogger added in v2.8.0

func WithLogger(logger slog.Logger) Option

func WithTimezone added in v2.2.0

func WithTimezone(tz string) Option

WithTimezone sets the database to the defined timezone.

func WithURL added in v2.15.0

func WithURL(u string) Option

type TBSubset added in v2.18.0

type TBSubset interface {
	Name() string
	Cleanup(func())
	Helper()
	Logf(format string, args ...any)
	TempDir() string
}

TBSubset is a subset of the testing.TB interface. It allows to use dbtestutil.Open outside of tests.

Jump to

Keyboard shortcuts

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