Movatterモバイル変換


[0]ホーム

URL:


dbtestutil

package
v2.23.0Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2025 License:AGPL-3.0Imports:35Imported by:0

Details

Repository

github.com/coder/coder

Links

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimezone = "Canada/Newfoundland"

Functions

funcDisableForeignKeysAndTriggersdeprecatedadded inv2.19.0

func DisableForeignKeysAndTriggers(t *testing.T, dbdatabase.Store)

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

funcDumpOnFailureadded inv2.2.0

func DumpOnFailure(ttesting.TB, connectionURLstring)

DumpOnFailure exports the database referenced by connectionURL to a filecorresponding to the current test, with a suffix indicating the time thetest 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'

funcNewDB

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

funcNewDBWithSQLDBadded inv2.3.1

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

funcNowInDefaultTimezoneadded inv2.19.0

func NowInDefaultTimezone()time.Time

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

funcOpenadded inv2.10.0

func Open(tTBSubset, 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.

funcOpenContainerizedadded inv2.10.0

func OpenContainerized(tTBSubset, optsDBContainerOptions) (string, func(),error)

OpenContainerized creates a new PostgreSQL server using a Docker container. If port is nonzero, forward host trafficto 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.

funcPGDumpadded inv2.11.0

func PGDump(dbURLstring) ([]byte,error)

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

funcPGDumpSchemaOnlyadded inv2.11.0

func PGDumpSchemaOnly(dbURLstring) ([]byte,error)

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

funcWillUsePostgres

func WillUsePostgres()bool

WillUsePostgres returns true if a call to NewDB() will return a real, postgres-backed Store and Pubsub.

Types

typeConnectionParamsadded inv2.18.0

type ConnectionParams struct {UsernamestringPasswordstringHoststringPortstringDBNamestring}

func (ConnectionParams)DSNadded inv2.18.0

typeConnectoradded inv2.15.0

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

func (*Connector)Connectadded inv2.15.0

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

func (*Connector)Dialeradded inv2.15.0

func (c *Connector) Dialer(dialerpq.Dialer)

func (*Connector)Driveradded inv2.15.0

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

typeDBContainerOptionsadded inv2.18.0

type DBContainerOptions struct {PortintNamestring}

typeDBTxadded inv2.17.0

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

funcStartTxadded inv2.17.0

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

StartTx starts a transaction and returns a DBTx object. This allows running2 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)Doneadded inv2.17.0

func (tx *DBTx) Done()error

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

typeDriveradded inv2.15.0

type Driver struct {Connections chandriver.Conn}

funcNewDriveradded inv2.15.0

func NewDriver() *Driver

func (*Driver)Closeadded inv2.15.0

func (d *Driver) Close()

func (*Driver)Connectoradded inv2.15.0

func (d *Driver) Connector(namestring) (driver.Connector,error)

func (*Driver)Openadded inv2.15.0

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

typeOpenOptionadded inv2.18.0

type OpenOption func(*OpenOptions)

funcWithDBFromadded inv2.18.0

func WithDBFrom(dbFromstring)OpenOption

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

typeOpenOptionsadded inv2.18.0

type OpenOptions struct {DBFrom *string}

typeOptionadded inv2.2.0

type Option func(*options)

funcWithDumpOnFailureadded inv2.2.0

func WithDumpOnFailure()Option

WithDumpOnFailure will dump the entire database on test failure.

funcWithLoggeradded inv2.8.0

func WithLogger(loggerslog.Logger)Option

funcWithTimezoneadded inv2.2.0

func WithTimezone(tzstring)Option

WithTimezone sets the database to the defined timezone.

funcWithURLadded inv2.15.0

func WithURL(ustring)Option

typeTBSubsetadded inv2.18.0

type TBSubset interface {Cleanup(func())Helper()Logf(formatstring, args ...any)}

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

Source Files

View all Source files

Directories

PathSynopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp