Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package postgres connects to PostgreSQL and applies namespaced migrations.

Index

Constants

This section is empty.

Variables

View Source
var ErrSchemaNotReady = errors.New("postgres: the database schema is not ready")

ErrSchemaNotReady means the database lacks migrations this binary needs.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB, sets []Set, logger *slog.Logger) error

Migrate applies the pending migrations of each set, in the order given. Each file runs in its own transaction, and one advisory lock serialises concurrent runs. A database that is ahead of this binary is reported and left alone.

func Open

func Open(ctx context.Context, databaseURL string, logger *slog.Logger) (*sql.DB, error)

Open connects to PostgreSQL and checks the connection.

For a host that is not a Unix socket or loopback address, Open requires verified TLS (sslmode=verify-full, with the system's trusted roots unless sslrootcert is set) when neither the URL nor PGSSLMODE chooses a mode. An explicit weaker mode is respected, with a warning.

Types

type Checker

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

Checker reports whether a database has every migration this binary needs. Newer migrations from a later release are tolerated, so older replicas keep serving during a rolling upgrade. Once satisfied, it stops querying: schemas only grow.

func NewChecker

func NewChecker(sets []Set) (*Checker, error)

NewChecker validates the sets once.

func (*Checker) Check

func (c *Checker) Check(ctx context.Context, db *sql.DB) error

Check returns ErrSchemaNotReady until every known migration is applied.

type Set

type Set struct {
	Namespace string
	Files     fs.FS
}

Set is one namespace's migrations: files named NNN_name.sql at the root of Files. Other files, such as a README, are ignored.

type Status

type Status struct {
	Namespace string
	Applied   []int
	Pending   []string // NNN_name of files not yet applied
	Newer     []int    // applied versions newer than this binary knows
}

Status describes one namespace's migration state.

func Report

func Report(ctx context.Context, db *sql.DB, sets []Set) ([]Status, error)

Report returns the migration state of each set without changing anything.

Jump to

Keyboard shortcuts

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