Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetupPostgres ¶
Types ¶
type PostgresContainer ¶
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 WithLogicalDB(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 ¶
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 WithLogicalDB(s) calls clone these templates.
At least one schema is required. Typically called once from a testify suite's SetupSuite.
func (*PostgresContainer) WithLogicalDB ¶
WithLogicalDB is a shorthand for WithLogicalDBs when the container holds a single schema.
func (*PostgresContainer) WithLogicalDBs ¶
func (c *PostgresContainer) WithLogicalDBs(t *testing.T, fn func(t *testing.T, pools map[string]*pgxpool.Pool))
WithLogicalDBs clones every template into a freshly named database, opens a pgxpool.Pool against each, and invokes fn with pools keyed by Schema.Name. When fn returns, every pool is closed and every database is dropped. Cleanups run LIFO, so any cleanups the test registers inside fn (e.g. tx rollback) run while the pools are still open.
type Schema ¶
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 WithLogicalDBs. Must be unique within a container.
Name string
// Migrations is the FS of migration files applied to the template.
Migrations fs.FS
}
Schema describes one template database to create on a PostgresContainer.