Documentation
¶
Overview ¶
Package postgres provides PostgreSQL testcontainer utilities for Go tests.
This package offers utilities for starting PostgreSQL containers in tests, with support for PostGIS, automatic migration detection and running, and cleanup helpers for test isolation.
Index ¶
- Variables
- func FindMigrationsPath() string
- func SkipIfDockerUnavailable() (shouldSkip bool, skipMessage string)
- type DockerAvailabilityResult
- type PostgreSQLConfig
- type PostgreSQLTestContainer
- func StartPostgreSQLContainer(ctx context.Context, config *PostgreSQLConfig) (*PostgreSQLTestContainer, error)
- func StartPostgreSQLContainerWithCheck(ctx context.Context, config *PostgreSQLConfig) (*PostgreSQLTestContainer, error)
- func StartPostgreSQLContainerWithMigrations(ctx context.Context, migrationsPath string) (*PostgreSQLTestContainer, error)
- func StartSimplePostgreSQLContainer(ctx context.Context) (*PostgreSQLTestContainer, error)
- func (tc *PostgreSQLTestContainer) CleanAllTables(ctx context.Context) error
- func (tc *PostgreSQLTestContainer) CleanSpecificTables(ctx context.Context, tableNames ...string) error
- func (tc *PostgreSQLTestContainer) Close() error
- func (tc *PostgreSQLTestContainer) GetConnectionString() string
- func (tc *PostgreSQLTestContainer) GetContainer() *postgres.PostgresContainer
- func (tc *PostgreSQLTestContainer) GetPool() *pgxpool.Pool
- func (tc *PostgreSQLTestContainer) NewTestDatabase(dbName string) (string, error)
- func (tc *PostgreSQLTestContainer) WithCleanup() func()
- func (tc *PostgreSQLTestContainer) WithTableCleanup(tables ...string) func()
Constants ¶
This section is empty.
Variables ¶
var ( ErrDockerNotAvailable = errors.New("Docker is not available or running") ErrContainerStartTimeout = errors.New("container failed to start within timeout period") ErrContainerPortConflict = errors.New("container port conflict detected") ErrDatabaseConnFailed = errors.New("failed to connect to container database") ErrMigrationsFailed = errors.New("database migrations failed") )
Error types for better error handling
Functions ¶
func FindMigrationsPath ¶
func FindMigrationsPath() string
FindMigrationsPath attempts to find the migrations directory This looks for common migration paths relative to the project root.
First checks for MIGRATIONS_PATH environment variable, then falls back to walking up the directory tree from the caller's location to find the project root (marked by .git directory), then searches for common migration paths: database/migrations, migrations, sql/migrations, db/migrations.
Uses .git instead of go.mod for monorepo awareness (multiple go.mod files exist).
Returns "database/migrations" as fallback if no migrations directory found.
func SkipIfDockerUnavailable ¶
SkipIfDockerUnavailable checks Docker availability and returns a skip message if unavailable This is useful for test suites that should gracefully skip when Docker is not available
Types ¶
type DockerAvailabilityResult ¶
DockerAvailabilityResult holds information about Docker availability
func CheckDockerAvailability ¶
func CheckDockerAvailability() DockerAvailabilityResult
CheckDockerAvailability checks if Docker is available and running
type PostgreSQLConfig ¶
type PostgreSQLConfig struct {
// Database configuration
DatabaseName string
Username string
Password string
// Image configuration
PostgreSQLVersion string // e.g., "16-3.4", "15-3.4" (version-postgis_version)
// Connection configuration
MaxConns int32
MinConns int32
MaxConnLife time.Duration
MaxConnIdle time.Duration
// Container configuration
StartupTimeout time.Duration
// Migration configuration
RunMigrations bool
MigrationsPath string // Relative to the calling test file or absolute path
}
PostgreSQLConfig provides configuration options for the PostgreSQL test container
func DefaultPostgreSQLConfig ¶
func DefaultPostgreSQLConfig() *PostgreSQLConfig
DefaultPostgreSQLConfig returns a sensible default configuration
type PostgreSQLTestContainer ¶
type PostgreSQLTestContainer struct {
Container *postgres.PostgresContainer
Pool *pgxpool.Pool
DatabaseURL string
Context context.Context
DatabaseName string
Username string
Password string
}
PostgreSQLTestContainer holds the PostgreSQL test container and related resources
func StartPostgreSQLContainer ¶
func StartPostgreSQLContainer(ctx context.Context, config *PostgreSQLConfig) (*PostgreSQLTestContainer, error)
StartPostgreSQLContainer creates and starts a PostgreSQL test container
func StartPostgreSQLContainerWithCheck ¶
func StartPostgreSQLContainerWithCheck(ctx context.Context, config *PostgreSQLConfig) (*PostgreSQLTestContainer, error)
StartPostgreSQLContainerWithCheck creates and starts a PostgreSQL test container with Docker availability checks
func StartPostgreSQLContainerWithMigrations ¶
func StartPostgreSQLContainerWithMigrations(ctx context.Context, migrationsPath string) (*PostgreSQLTestContainer, error)
StartPostgreSQLContainerWithMigrations creates a PostgreSQL container and runs migrations with Docker check
func StartSimplePostgreSQLContainer ¶
func StartSimplePostgreSQLContainer(ctx context.Context) (*PostgreSQLTestContainer, error)
StartSimplePostgreSQLContainer creates a PostgreSQL container with default settings This is a convenience function for simple test setups with Docker availability check
func (*PostgreSQLTestContainer) CleanAllTables ¶
func (tc *PostgreSQLTestContainer) CleanAllTables(ctx context.Context) error
CleanAllTables truncates all tables in the database for test isolation WARNING: This removes ALL data from ALL tables
func (*PostgreSQLTestContainer) CleanSpecificTables ¶
func (tc *PostgreSQLTestContainer) CleanSpecificTables(ctx context.Context, tableNames ...string) error
CleanSpecificTables truncates specific tables for test isolation Only truncates tables that actually exist to avoid errors
func (*PostgreSQLTestContainer) Close ¶
func (tc *PostgreSQLTestContainer) Close() error
Close closes the connection pool and terminates the container
func (*PostgreSQLTestContainer) GetConnectionString ¶
func (tc *PostgreSQLTestContainer) GetConnectionString() string
GetConnectionString returns the database connection string
func (*PostgreSQLTestContainer) GetContainer ¶
func (tc *PostgreSQLTestContainer) GetContainer() *postgres.PostgresContainer
GetContainer returns the testcontainers instance
func (*PostgreSQLTestContainer) GetPool ¶
func (tc *PostgreSQLTestContainer) GetPool() *pgxpool.Pool
GetPool returns the connection pool
func (*PostgreSQLTestContainer) NewTestDatabase ¶
func (tc *PostgreSQLTestContainer) NewTestDatabase(dbName string) (string, error)
NewTestDatabase creates a new database within the container for isolation This is useful when you need multiple isolated databases in the same container
func (*PostgreSQLTestContainer) WithCleanup ¶
func (tc *PostgreSQLTestContainer) WithCleanup() func()
WithCleanup returns a cleanup function that can be deferred
func (*PostgreSQLTestContainer) WithTableCleanup ¶
func (tc *PostgreSQLTestContainer) WithTableCleanup(tables ...string) func()
WithTableCleanup returns a cleanup function that truncates specific tables