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

database

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0, MIT Imports: 16 Imported by: 0

Documentation

Overview

Package database wraps pg_dump, pg_restore, and psql.

dump:     pg_dump in custom format
export:   pg_dump in plain SQL
restore:  pg_restore from a dump file
import:   psql from a SQL file

DSNs are parsed with pgx only. restore/import are destructive; callers gate them with confirm, --force, and --dry-run.

Goose runner shared by both variants. Builds a Provider over the source picked by the variant files, exposes package-owned types (goose never leaks). --dry-run reads via the same Provider API.

Package database applies/inspects compiled-in migrations. Scaffolding and reset are debug-only.

Index

Constants

This section is empty.

Variables

View Source
var DatabaseMigrations embed.FS

Functions

func Dump

func Dump(ctx context.Context, dsn, mode, dir string) (string, error)

Dump writes a custom-format backup. all = schema+data, data = data only. Returns the file path.

func DumpWithExecutor

func DumpWithExecutor(ctx context.Context, exec Executor, dsn, mode, dir string) (string, error)

DumpWithExecutor writes a custom-format backup using the provided executor.

func Export

func Export(ctx context.Context, dsn, mode, dir string) (string, error)

Export writes a plain-SQL backup. all = schema+data with clean statements, data = inserts only. Returns the file path.

func ExportWithExecutor

func ExportWithExecutor(ctx context.Context, exec Executor, dsn, mode, dir string) (string, error)

ExportWithExecutor writes a plain-SQL backup using the provided executor.

func Import

func Import(ctx context.Context, dsn, sqlFile string) error

Import runs a SQL file with ON_ERROR_STOP=on: the first failure aborts, so partial imports never report success. Destructive.

func ImportCommand

func ImportCommand(dsn, sqlFile string) (string, error)

ImportCommand renders the psql call for --dry-run.

func ImportWithExecutor

func ImportWithExecutor(ctx context.Context, exec Executor, dsn, sqlFile string) error

ImportWithExecutor runs a SQL file using the provided executor.

func MigrateVersion

func MigrateVersion(ctx context.Context, dsn string) (current, target int64, err error)

MigrateVersion reports applied and target versions.

func Restore

func Restore(ctx context.Context, dsn, mode, dumpFile string) error

Restore loads a custom-format dump. all = schema+data (--clean), data = data only, schema = schema only. Destructive.

func RestoreCommand

func RestoreCommand(dsn, mode, dumpFile string) (string, error)

RestoreCommand renders the pg_restore call for --dry-run.

func RestoreWithExecutor

func RestoreWithExecutor(ctx context.Context, exec Executor, dsn, mode, dumpFile string) error

RestoreWithExecutor loads a custom-format dump using the provided executor.

Types

type Executor

type Executor interface {
	PGDump(ctx context.Context, env []string, args []string, outputPath string) error
	PGRestore(ctx context.Context, env []string, args []string) error
	PSQL(ctx context.Context, env []string, args []string) error
}

Executor runs PostgreSQL client commands. env carries the process environment (including PGPASSWORD, which must never appear in argv). The default implementation uses host binaries; tests can provide a container-backed implementation.

var DefaultExecutor Executor = LocalExecutor{}

DefaultExecutor is the executor used by the package-level Dump, Export, Restore, and Import helpers. Tests override it.

type LocalExecutor

type LocalExecutor struct{}

LocalExecutor runs commands using host binaries.

func (LocalExecutor) PGDump

func (LocalExecutor) PGDump(ctx context.Context, env []string, args []string, outputPath string) error

func (LocalExecutor) PGRestore

func (LocalExecutor) PGRestore(ctx context.Context, env []string, args []string) error

func (LocalExecutor) PSQL

func (LocalExecutor) PSQL(ctx context.Context, env []string, args []string) error

type MigrationOutcome

type MigrationOutcome struct {
	Version  int64
	Path     string
	Duration time.Duration
}

MigrationOutcome is one applied or rolled-back migration.

func MigrateDown

func MigrateDown(ctx context.Context, dsn string) (*MigrationOutcome, error)

MigrateDown rolls back the most recent migration.

func MigrateDownTo

func MigrateDownTo(ctx context.Context, dsn string, version int64) ([]MigrationOutcome, error)

MigrateDownTo rolls back everything above version.

func MigrateUp

func MigrateUp(ctx context.Context, dsn string) ([]MigrationOutcome, error)

MigrateUp applies all pending embedded migrations.

func MigrateUpTo

func MigrateUpTo(ctx context.Context, dsn string, version int64) ([]MigrationOutcome, error)

MigrateUpTo applies pending migrations up to version.

type MigrationStatus

type MigrationStatus struct {
	Version   int64
	Path      string
	State     string // "applied" or "pending"
	AppliedAt time.Time
}

MigrationStatus is one migration file's state.

func MigrateDownTarget

func MigrateDownTarget(ctx context.Context, dsn string) (*MigrationStatus, error)

MigrateDownTarget reports what MigrateDown would roll back, read-only.

func MigrateStatus

func MigrateStatus(ctx context.Context, dsn string) ([]MigrationStatus, error)

MigrateStatus reports every embedded migration's state.

Directories

Path Synopsis
Package seeders builds database fixtures: the first-admin setup row and deterministic development users.
Package seeders builds database fixtures: the first-admin setup row and deterministic development users.

Jump to

Keyboard shortcuts

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