postgres

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package postgres opens the PostgreSQL pool of a site, runs its migrations and hands its database to a plain role.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSuperuser refuses a hand-over on a connection that is no superuser.
	ErrNotSuperuser = errors.New("dbkit: handing over a database takes a superuser connection")
	// ErrNotPlainRole refuses a hand-over to a role that is a superuser or can reach the database server.
	ErrNotPlainRole = errors.New("dbkit: a database goes to a plain role, and this role can reach the database server")
	// ErrRoleName refuses a role name that is empty or longer than the server keeps.
	ErrRoleName = errors.New("dbkit: the role name is empty or longer than the server keeps")
)

Functions

func CheckAddress

func CheckAddress(address string) error

CheckAddress returns the error for an empty PostgreSQL address or one pgx 5.10 and 5.11 read apart with no PG* set.

func Classify

func Classify(err error) error

Classify returns err wrapped in its dbkit error class, or err unchanged when it has none or already carries it.

func HandOver added in v0.4.0

func HandOver(ctx context.Context, db *sql.DB, owner Owner) ([]string, error)

HandOver gives owner the database of db and what it holds, in one transaction, and returns the steps it ran.

func Migrate

func Migrate(ctx context.Context, db *sql.DB, m Migrations) error

Migrate applies every pending migration of m to db under goose's advisory lock, creating an absent schema of Table.

func OwnerSteps added in v0.4.0

func OwnerSteps(ctx context.Context, db *sql.DB, role string) ([]string, error)

OwnerSteps returns the statements HandOver would run for role on the database of db, and changes nothing.

func ReachesServer added in v0.4.0

func ReachesServer(ctx context.Context, db *sql.DB) (bool, error)

ReachesServer reports whether the role db logs in as is one HandOver refuses, because it can reach the server.

Types

type Handle

type Handle struct {
	// Pool owns every connection of the handle.
	Pool *pgxpool.Pool
	// DB is the database/sql view of Pool, with no idle connection of its own.
	DB *sql.DB
}

Handle is the one PostgreSQL pool of a site and a database/sql view that draws from it.

func Open

func Open(address string, opts Options) (*Handle, error)

Open returns a handle on the PostgreSQL database of address, capped at opts.MaxConns, and connects to nothing.

func (*Handle) Close

func (h *Handle) Close() error

Close closes the view, then the pool.

type Migrations

type Migrations struct {
	// Table is the version table, table or schema.table, lowercase parts of up to 63 bytes, none reserved, no pg_ schema.
	Table string
	// FS holds the owner's SQL migration files at its root, and nil means none.
	FS fs.FS
	// Go are the owner's Go migrations, each built with goose.NewGoMigration.
	Go []*goose.Migration
}

Migrations are the migrations of one owner.

type Options

type Options struct {
	// MaxConns caps the connections the pool and its database/sql view hold together, from 2 to 2147483647.
	MaxConns int
}

Options configures one PostgreSQL pool. MaxConns is required.

type Owner added in v0.4.0

type Owner struct {
	// Role is the name of the role, created when it is missing.
	Role string
	// Password is the password the role logs in with, sent to the server as text, left as it is when empty.
	Password string
}

Owner is the plain role a database goes to.

Directories

Path Synopsis
Package pgtest gives each test a fresh PostgreSQL database cut from a migrated template, and sweeps stale ones.
Package pgtest gives each test a fresh PostgreSQL database cut from a migrated template, and sweeps stale ones.

Jump to

Keyboard shortcuts

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