Documentation
¶
Overview ¶
Package embeddedpostgres runs an isolated PostgreSQL server for tests. Production packages use only the Go standard library. PostgreSQL and the companion supervisor are native executables acquired separately.
Index ¶
- Variables
- func PruneCache(ctx context.Context, dir string) (int, error)
- type BinaryProvider
- type BinaryRequest
- type Config
- func (c Config) BinariesPath(v string) Config
- func (c Config) BinaryRepositoryURL(v string) Config
- func (c Config) CachePath(v string) Config
- func (c Config) DataPath(v string) Config
- func (c Config) Database(v string) Config
- func (c Config) Encoding(v string) Config
- func (c Config) Environment(v ...string) Config
- func (c Config) GetConnectionURL() string
- func (c Config) Hooks(v Hooks) Config
- func (c Config) Locale(v string) Config
- func (c Config) Logger(v io.Writer) Config
- func (c Config) Password(v string) Config
- func (c Config) Port(v uint32) Config
- func (c Config) Provider(v BinaryProvider) Config
- func (c Config) RunAs(v User) Config
- func (c Config) RuntimePath(v string) Config
- func (c Config) StartParameters(v map[string]string) Config
- func (c Config) StartTimeout(v time.Duration) Config
- func (c Config) StopTimeout(v time.Duration) Config
- func (c Config) Storage(v Storage) Config
- func (c Config) Supervisor(path string) Config
- func (c Config) UnixSocket(dir string) Config
- func (c Config) Username(v string) Config
- func (c Config) Version(v PostgresVersion) Config
- type DownloadProvider
- type EmbeddedPostgres
- func (ep *EmbeddedPostgres) Close() error
- func (ep *EmbeddedPostgres) ConnectionURL() string
- func (ep *EmbeddedPostgres) Done() <-chan struct{}
- func (ep *EmbeddedPostgres) Err() error
- func (ep *EmbeddedPostgres) GetConnectionURL() string
- func (ep *EmbeddedPostgres) GetPort() uint32
- func (ep *EmbeddedPostgres) Info() InstanceInfo
- func (ep *EmbeddedPostgres) Logs() string
- func (ep *EmbeddedPostgres) Start() error
- func (ep *EmbeddedPostgres) StartContext(ctx context.Context) (err error)
- func (ep *EmbeddedPostgres) Stop() error
- func (ep *EmbeddedPostgres) StopContext(ctx context.Context) error
- type Hook
- type Hooks
- type Installation
- type InstanceInfo
- type LocalProvider
- type PostgresVersion
- type ProviderFunc
- type Storage
- type User
Constants ¶
This section is empty.
Variables ¶
var ( ErrServerNotStarted = errors.New("server has not been started") ErrServerAlreadyStarted = errors.New("server is already started") ErrDataVersion = errors.New("persistent data belongs to a different PostgreSQL major version") )
var ReleaseVersion = "v2.0.0-dev"
ReleaseVersion is set for CLI builds; Go consumers use module build metadata.
Functions ¶
Types ¶
type BinaryProvider ¶
type BinaryProvider interface {
Acquire(context.Context, BinaryRequest) (*Installation, error)
}
BinaryProvider supplies PostgreSQL plus initdb, pg_ctl, psql, and createdb. Implementations must support concurrent calls and honour cancellation.
type BinaryRequest ¶
type BinaryRequest struct {
Version PostgresVersion
GOOS, GOARCH string
}
BinaryRequest identifies a requested PostgreSQL installation.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config uses value builders. Maps and slices are copied at API boundaries.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig retains port 5432 for existing callers. Use Port(0) for automatic allocation. Test helpers and the CLI choose Port(0) by default.
func (Config) BinariesPath ¶
BinariesPath is deprecated: use LocalProvider. The distribution must include psql and createdb; legacy minimal Zonky bundles do not contain them.
func (Config) BinaryRepositoryURL ¶
BinaryRepositoryURL is deprecated: use DownloadProvider.BaseURL. v2 mirrors use /VERSION/ASSET.tar.gz, not the v1 Maven/JAR layout.
func (Config) Environment ¶
Environment appends explicit subprocess environment overrides. Identity does not inherit root-only HOME/PG variables; connection settings are set separately.
func (Config) GetConnectionURL ¶
GetConnectionURL describes static configuration. For Port(0), use the running instance's ConnectionURL instead; this method deliberately does no I/O.
func (Config) Provider ¶
func (c Config) Provider(v BinaryProvider) Config
func (Config) RunAs ¶
RunAs sets the Unix identity for the supervisor and every PostgreSQL command. Root callers must set both UID and GID to nonzero values explicitly.
func (Config) RuntimePath ¶
RuntimePath is deprecated: use Storage.WorkDir. The path is now a parent and is never erased; each instance receives a uniquely named child.
func (Config) Supervisor ¶
Supervisor selects the companion CLI executable. Released library versions can download their matching companion automatically. Source builds must set it.
func (Config) UnixSocket ¶
UnixSocket selects a private Unix socket directory and disables TCP.
func (Config) Version ¶
func (c Config) Version(v PostgresVersion) Config
type DownloadProvider ¶
DownloadProvider downloads pinned gzip bundles and shares extracted binaries. BaseURL mirrors the upstream /VERSION/ASSET layout. Target overrides automatic platform detection (for custom musl systems, for example). Client supports authenticated mirrors. Offline refuses network requests.
func (DownloadProvider) Acquire ¶
func (p DownloadProvider) Acquire(ctx context.Context, r BinaryRequest) (*Installation, error)
type EmbeddedPostgres ¶
type EmbeddedPostgres struct {
// contains filtered or unexported fields
}
EmbeddedPostgres serialises lifecycle transitions. Separate instances run in parallel and may share immutable binaries. Inspection methods serialise behind an in-flight Start or Close; StopContext bounds its caller's wait even then. Do not copy after first use.
func NewDatabase ¶
func NewDatabase(config ...Config) *EmbeddedPostgres
NewDatabase constructs an instance without doing I/O. If supplied, the first config is used, preserving the v1 calling convention.
func (*EmbeddedPostgres) Close ¶
func (ep *EmbeddedPostgres) Close() error
func (*EmbeddedPostgres) ConnectionURL ¶
func (ep *EmbeddedPostgres) ConnectionURL() string
ConnectionURL returns the actual endpoint after Start succeeds.
func (*EmbeddedPostgres) Done ¶
func (ep *EmbeddedPostgres) Done() <-chan struct{}
Done closes when an instance has been fully cleaned up. Before Start it is already closed.
func (*EmbeddedPostgres) Err ¶
func (ep *EmbeddedPostgres) Err() error
Err reports the most recent lifecycle or unexpected-process error.
func (*EmbeddedPostgres) GetConnectionURL ¶
func (ep *EmbeddedPostgres) GetConnectionURL() string
func (*EmbeddedPostgres) GetPort ¶
func (ep *EmbeddedPostgres) GetPort() uint32
func (*EmbeddedPostgres) Info ¶
func (ep *EmbeddedPostgres) Info() InstanceInfo
func (*EmbeddedPostgres) Logs ¶
func (ep *EmbeddedPostgres) Logs() string
Logs returns the recent server output with the configured password redacted. A bounded tail remains available after Close for test-failure diagnostics. Temporary files are still removed. Errors also include startup diagnostics.
func (*EmbeddedPostgres) Start ¶
func (ep *EmbeddedPostgres) Start() error
func (*EmbeddedPostgres) StartContext ¶
func (ep *EmbeddedPostgres) StartContext(ctx context.Context) (err error)
StartContext starts a server whose lifetime is bound to ctx. Cancellation after readiness also closes the server, using an independent cleanup timeout.
func (*EmbeddedPostgres) Stop ¶
func (ep *EmbeddedPostgres) Stop() error
Stop preserves the v1 error when no server is active. Close is idempotent.
func (*EmbeddedPostgres) StopContext ¶
func (ep *EmbeddedPostgres) StopContext(ctx context.Context) error
StopContext requests cleanup and bounds how long the caller waits. If ctx expires, bounded process shutdown and resource cleanup continue independently.
type Hook ¶
Hook runs synchronously. It must honour ctx and must not call Start/Stop/Close on its own instance. Return cleanup for resources acquired before any error.
type Hooks ¶
type Hooks struct{ BeforeStart, Ready []Hook }
Hooks are ordered phases. BeforeStart runs after initdb, Ready after an authenticated query. Returned cleanups run in reverse order after shutdown, including when a later hook or startup fails. Panics are cleaned up then rethrown.
type Installation ¶
Installation is immutable while acquired. Release is called once, after all owned processes stop. It must release leases, not delete caller-owned data.
type InstanceInfo ¶
InstanceInfo is a snapshot. Hooks may change only their instance's private work/data files, never the shared binary installation.
type LocalProvider ¶
type LocalProvider string
LocalProvider uses a preinstalled distribution without modifying it.
func (LocalProvider) Acquire ¶
func (p LocalProvider) Acquire(ctx context.Context, _ BinaryRequest) (*Installation, error)
type PostgresVersion ¶
type PostgresVersion string
PostgresVersion identifies a pinned distribution release. Its first two components are the PostgreSQL version; the third is the packaging revision.
const ( V18 PostgresVersion = "18.6.0" V17 PostgresVersion = "17.11.0" V16 PostgresVersion = "16.15.0" V15 PostgresVersion = "15.19.0" )
type ProviderFunc ¶
type ProviderFunc func(context.Context, BinaryRequest) (*Installation, error)
ProviderFunc adapts a function to BinaryProvider.
func (ProviderFunc) Acquire ¶
func (f ProviderFunc) Acquire(ctx context.Context, r BinaryRequest) (*Installation, error)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
embedded-postgres
command
embedded-postgres runs PostgreSQL for tests and supervises its process tree.
|
embedded-postgres runs PostgreSQL for tests and supervises its process tree. |
|
Package eptest binds an embedded PostgreSQL instance to Go tests and suites.
|
Package eptest binds an embedded PostgreSQL instance to Go tests and suites. |
|
examples
|
|
|
custom
Package custom contains compiled extension patterns; it adds no core dependencies.
|
Package custom contains compiled extension patterns; it adds no core dependencies. |
|
internal
|
|
|
archive
Package archive extracts bounded gzip archives beneath a private directory.
|
Package archive extracts bounded gzip archives beneath a private directory. |
|
binaries
Package binaries contains the audited, release-pinned PostgreSQL catalogue.
|
Package binaries contains the audited, release-pinned PostgreSQL catalogue. |
|
cli
Package cli implements the stable command-line and JSON protocol surface.
|
Package cli implements the stable command-line and JSON protocol surface. |
|
filelock
Package filelock provides process-scoped shared/exclusive cache leases.
|
Package filelock provides process-scoped shared/exclusive cache leases. |
|
platform
Package platform contains the small OS boundary for process ownership.
|
Package platform contains the small OS boundary for process ownership. |
|
supervisor
Package supervisor owns a PostgreSQL process independently of its Go caller.
|
Package supervisor owns a PostgreSQL process independently of its Go caller. |
