Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetupPostgres ¶
Types ¶
type PostgresContainer ¶ added in v19.1.0
type PostgresContainer struct {
// contains filtered or unexported fields
}
PostgresContainer wraps a single Postgres testcontainer with one or more pre-migrated template databases. Each call to CloneLogicalDB(s) clones every template into a fresh logical database via CREATE DATABASE ... TEMPLATE, giving every test its own isolated schemas without paying the cost of starting a new container or rerunning migrations.
The container terminates when the testing.TB passed to NewPostgresContainer cleans up. Individual logical databases are dropped when their test ends so disk usage in the container stays bounded by concurrent (not cumulative) test count.
func NewPostgresContainer ¶ added in v19.1.0
func NewPostgresContainer(t testing.TB, schemas ...Schema) *PostgresContainer
NewPostgresContainer starts a Postgres container and creates one template database per schema, named "<schema.Name>_template", applying the schema's migrations to it. Subsequent CloneLogicalDB(s) calls clone these templates.
At least one schema is required. Typically called once from a testify suite's SetupSuite.
func (*PostgresContainer) CloneLogicalDB ¶ added in v19.2.0
func (c *PostgresContainer) CloneLogicalDB(t *testing.T) *pgxpool.Pool
CloneLogicalDB is a shorthand for CloneLogicalDBs when the container holds a single schema; it returns that schema's pool.
func (*PostgresContainer) CloneLogicalDBs ¶ added in v19.2.0
CloneLogicalDBs clones every template into a freshly named database, opens a pgxpool.Pool against each, and returns them keyed by Schema.Name. Every pool is closed and every database dropped when the test (t) ends, via t.Cleanup (registered by CloneNamedLogicalDB). Cleanups run LIFO, so any cleanups the test registers after this call (e.g. tx rollback) run while the pools are still open.
func (*PostgresContainer) CloneNamedLogicalDB ¶ added in v19.2.0
CloneNamedLogicalDB clones the named schema's template into a freshly named logical database via CREATE DATABASE ... TEMPLATE, opens a pgxpool.Pool against it, and registers cleanup (drop database + close pool) on t. Unlike CloneLogicalDBs, the same schema may be cloned multiple times, giving each clone an isolated database.
func (*PostgresContainer) ContainerIP ¶ added in v19.2.0
func (c *PostgresContainer) ContainerIP() string
ContainerIP returns the Postgres container's IP on the Docker network. Use it to build DSNs reachable from other containers; the pools handed out by CloneLogicalDB(s)/CloneNamedLogicalDB use the host-mapped port on port 5432 and are only reachable from the test process.
type Schema ¶ added in v19.1.0
type Schema struct {
// Name is used both as the migration tracking key (passed to
// pgtool.RunMigrations) and as the lookup key for the cloned pool returned
// by CloneLogicalDBs. Must be unique within a container.
Name string
// Migrations is the FS of migration files applied to the template.
Migrations fs.FS
// PostMigrate, if non-nil, runs against the freshly-migrated template before
// it is cloned, so every logical DB inherits whatever it seeds. The pool it
// receives is closed by the harness afterwards; it must not leave its own
// open sessions (CREATE DATABASE ... TEMPLATE rejects a template with active
// connections).
PostMigrate func(ctx context.Context, pool *pgxpool.Pool) error
}
Schema describes one template database to create on a PostgresContainer.