pgkit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package pgkit gives each test its own pristine, migrated Postgres database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FSMigrator

func FSMigrator(fsys fs.FS, run func(dsn string) error) pgtestdb.Migrator

FSMigrator adapts an embedded migration directory plus the project's own migration runner to pgtestdb.Migrator.

pgtestdb's bundled adapters take migration source URLs. Projects here embed their migrations with go:embed and run them through a function taking a DSN, so neither shipped adapter fits.

Types

type Config

type Config struct {
	// Migrator is required. Use FSMigrator.
	Migrator pgtestdb.Migrator
	// ComposeDSN is tried when DSNEnv is unset. Defaults to the docker-compose service
	// on port 55433.
	ComposeDSN string
	// DSNEnv names the environment variable checked first. Defaults to
	// TESTKIT_POSTGRES_DSN.
	DSNEnv string
	// Image is the fallback container image. Defaults to postgres:16-alpine.
	Image string
	// ReachableTimeout budgets the ComposeDSN login probe. Defaults to 3s. Raise it on
	// a slow or heavily loaded machine, where an over-tight budget quietly costs a
	// container boot instead of reusing a healthy compose server.
	ReachableTimeout time.Duration
}

Config declares how to reach a Postgres server and how to migrate it.

type Server

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

Server resolves one Postgres server per test-binary process and hands out a pristine database per test.

func New

func New(cfg Config) *Server

New returns a Server. Call it once at package level; resolution is lazy and happens on the first Fresh call.

func (*Server) Fresh

func (s *Server) Fresh(t *testing.T) string

Fresh returns the DSN of a pristine database cloned from the migrated template. It is safe to call from parallel tests. The database is dropped at test cleanup when the test passes, and deliberately left alive when it fails so it can be inspected with psql.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL