pgbranchtest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package pgbranchtest gives every Go test its own disposable Postgres database: Acquire creates a copy-on-write branch on a running pgbranch server (branchd), waits until it is ready, and destroys it when the test finishes.

func TestOrders(t *testing.T) {
	b := pgbranchtest.Acquire(t)
	db, _ := sql.Open("pgx", b.DSN)
	// full production-shaped data, isolated writes
}

Configuration comes from the environment: PGBRANCH_SERVER (base URL of branchd; tests are skipped when unset — the SDK is integration-only by nature), PGBRANCH_TOKEN (API bearer token), PGBRANCH_TEST_SOURCE (default source name, else "main"), and PGBRANCH_PASSWORD (database password used in the returned DSNs; branch credentials are inherited from the source unless the server rotates them per branch and returns one).

The package is intentionally self-contained (stdlib only): it speaks the branchd REST API directly and never imports pgbranch internals.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Branch

type Branch struct {
	Name     string
	Host     string
	Port     int
	User     string
	Password string
	Database string
	DSN      string
	ProxyDSN string
}

Branch is an acquired database branch. DSN targets the branch's Postgres directly; ProxyDSN goes through the pgbranch wire-protocol router on the server host (port 6432, database "db@branch").

func Acquire

func Acquire(t testing.TB, opts ...Option) *Branch

Acquire creates a branch named t-<test name>-<random> and registers its destruction with t.Cleanup. It is safe for parallel tests: every call gets its own branch. The test is skipped when PGBRANCH_SERVER is unset.

type Option

type Option func(*config)

Option customizes Acquire.

func WithSource

func WithSource(name string) Option

WithSource selects the source to branch from. Default: PGBRANCH_TEST_SOURCE, else "main".

func WithTTL

func WithTTL(d time.Duration) Option

WithTTL sets the branch TTL — a server-side safety net in case the process dies before t.Cleanup runs. Default 1h; explicit destroy on test end is the primary cleanup.

Jump to

Keyboard shortcuts

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