{"modulePath":"github.com/coder/coder/v2","version":"v2.32.1","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package dbtestutil\n\n## Constants\n\n```go\nconst CoderTestingDBName = \"coder_testing\"\n```\n\n## Variables\n\n```go\nvar (\n\tDefaultBroker = Broker{}\n)\n```\nThese variables are global because all tests share them.\n\n```go\nvar DefaultTimezone = \"America/St_Johns\"\n```\n\n## Functions\n\n```go\nfunc DisableForeignKeysAndTriggers(t *testing.T, db database.Store)\n```\nDeprecated: disable foreign keys was created to aid in migrating off of the test-only in-memory database. Do not use this in new code.\n\n```go\nfunc DumpOnFailure(t testing.TB, connectionURL string)\n```\nDumpOnFailure 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):\n\n  - Create a new test database: go run ./scripts/migrate-ci/main.go and note the database name it outputs\n  - Import the file into the above database: psql 'postgres://postgres:postgres@127.0.0.1:5432/\\\u003cdbname\u003e?sslmode=disable' -f \\\u003cpath to file.test.sql\u003e\n  - Run a dev server against that database: ./scripts/coder-dev.sh server --postgres-url='postgres://postgres:postgres@127.0.0.1:5432/\\\u003cdbname\u003e?sslmode=disable'\n\n```go\nfunc NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub)\n```\n\n```go\nfunc NewDBWithSQLDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub, *sql.DB)\n```\n\n```go\nfunc NowInDefaultTimezone() time.Time\n```\nNowInDefaultTimezone returns the current time rounded to the nearest microsecond in the default timezone used by postgres in tests. Useful for object equality checks.\n\n```go\nfunc Open(t TBSubset, opts ...OpenOption) (string, error)\n```\nOpen 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.\n\n```go\nfunc OpenContainerized(t TBSubset, opts DBContainerOptions) (string, func(), error)\n```\nOpenContainerized 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.\n\n```go\nfunc PGDump(dbURL string) ([]byte, error)\n```\nPGDump runs pg\\_dump against dbURL and returns the output. It is used by DumpOnFailure().\n\n```go\nfunc PGDumpSchemaOnly(dbURL string) ([]byte, error)\n```\nPGDumpSchemaOnly is for use by gen/dump only. It runs pg\\_dump against dbURL and sets a consistent timezone and encoding.\n\n```go\nfunc RunCleaner()\n```\nRunCleaner runs the test database cleaning process. It takes no arguments but uses stdio and environment variables for its operation.\n\nThe 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](https://github.com/coder/internal/issues/927)\n\n## Types\n\n```go\ntype Broker struct {\n\tsync.Mutex\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (b *Broker) Create(t TBSubset, opts ...OpenOption) (ConnectionParams, error)\n```\n\n```go\ntype ConnectionParams struct {\n\tUsername string\n\tPassword string\n\tHost     string\n\tPort     string\n\tDBName   string\n}\n```\n\n```go\nfunc (p ConnectionParams) DSN() string\n```\n\n```go\ntype Connector struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (c *Connector) Connect(_ context.Context) (driver.Conn, error)\n```\n\n```go\nfunc (c *Connector) Dialer(dialer pq.Dialer)\n```\n\n```go\nfunc (c *Connector) Driver() driver.Driver\n```\n\n```go\ntype DBContainerOptions struct {\n\tPort int\n\tName string\n}\n```\n\n```go\ntype DBTx struct {\n\tdatabase.Store\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc StartTx(t *testing.T, db database.Store, opts *database.TxOptions) *DBTx\n```\nStartTx starts a transaction and returns a DBTx object. This allows running 2 transactions concurrently in a test more easily. Example:\n\n\ta := StartTx(t, db, opts)\n\tb := StartTx(t, db, opts)\n\n\ta.GetUsers(...)\n\tb.GetUsers(...)\n\n\trequire.NoError(t, a.Done()\n\n```go\nfunc (tx *DBTx) Done() error\n```\nDone can only be called once. If you call it twice, it will panic.\n\n```go\ntype Driver struct {\n\tConnections chan driver.Conn\n}\n```\n\n```go\nfunc NewDriver() *Driver\n```\n\n```go\nfunc (d *Driver) Close()\n```\n\n```go\nfunc (d *Driver) Connector(name string) (driver.Connector, error)\n```\n\n```go\nfunc (d *Driver) Open(name string) (driver.Conn, error)\n```\n\n```go\ntype OpenOption func(*OpenOptions)\n```\n\n```go\nfunc WithDBFrom(dbFrom string) OpenOption\n```\nWithDBFrom sets the template database to use when creating a new database. Overrides the DB\\_FROM environment variable.\n\n```go\nfunc WithLogDSN(logDSN bool) OpenOption\n```\nWithLogDSN sets whether the DSN should be logged during testing. This provides an ergonomic way to connect to test databases during debugging.\n\n```go\ntype OpenOptions struct {\n\tDBFrom *string\n\tLogDSN bool\n}\n```\n\n```go\ntype Option func(*options)\n```\n\n```go\nfunc WithDumpOnFailure() Option\n```\nWithDumpOnFailure will dump the entire database on test failure.\n\n```go\nfunc WithLogger(logger slog.Logger) Option\n```\n\n```go\nfunc WithTimezone(tz string) Option\n```\nWithTimezone sets the database to the defined timezone.\n\n```go\nfunc WithURL(u string) Option\n```\n\n```go\ntype TBSubset interface {\n\tName() string\n\tCleanup(func())\n\tHelper()\n\tLogf(format string, args ...any)\n\tTempDir() string\n}\n```\nTBSubset is a subset of the testing.TB interface. It allows to use dbtestutil.Open outside of tests.\n\n","path":"github.com/coder/coder/v2/coderd/database/dbtestutil","name":"dbtestutil","synopsis":"","isRedistributable":true}
