{"modulePath":"github.com/coder/coder/v2","version":"v2.18.3","isLatest":false,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package dbtestutil\n\n## Functions\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 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 WillUsePostgres() bool\n```\nWillUsePostgres returns true if a call to NewDB() will return a real, postgres-backed Store and Pubsub.\n\n## Types\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\ntype OpenOptions struct {\n\tDBFrom *string\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\tCleanup(func())\n\tHelper()\n\tLogf(format string, args ...any)\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}
