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 ¶
- Variables
- func Dump(ctx context.Context, dsn, mode, dir string) (string, error)
- func DumpWithExecutor(ctx context.Context, exec Executor, dsn, mode, dir string) (string, error)
- func Export(ctx context.Context, dsn, mode, dir string) (string, error)
- func ExportWithExecutor(ctx context.Context, exec Executor, dsn, mode, dir string) (string, error)
- func Import(ctx context.Context, dsn, sqlFile string) error
- func ImportCommand(dsn, sqlFile string) (string, error)
- func ImportWithExecutor(ctx context.Context, exec Executor, dsn, sqlFile string) error
- func MigrateVersion(ctx context.Context, dsn string) (current, target int64, err error)
- func Restore(ctx context.Context, dsn, mode, dumpFile string) error
- func RestoreCommand(dsn, mode, dumpFile string) (string, error)
- func RestoreWithExecutor(ctx context.Context, exec Executor, dsn, mode, dumpFile string) error
- type Executor
- type LocalExecutor
- type MigrationOutcome
- func MigrateDown(ctx context.Context, dsn string) (*MigrationOutcome, error)
- func MigrateDownTo(ctx context.Context, dsn string, version int64) ([]MigrationOutcome, error)
- func MigrateUp(ctx context.Context, dsn string) ([]MigrationOutcome, error)
- func MigrateUpTo(ctx context.Context, dsn string, version int64) ([]MigrationOutcome, error)
- type MigrationStatus
Constants ¶
This section is empty.
Variables ¶
var DatabaseMigrations embed.FS
Functions ¶
func Dump ¶
Dump writes a custom-format backup. all = schema+data, data = data only. Returns the file path.
func DumpWithExecutor ¶
DumpWithExecutor writes a custom-format backup using the provided executor.
func Export ¶
Export writes a plain-SQL backup. all = schema+data with clean statements, data = inserts only. Returns the file path.
func ExportWithExecutor ¶
ExportWithExecutor writes a plain-SQL backup using the provided executor.
func Import ¶
Import runs a SQL file with ON_ERROR_STOP=on: the first failure aborts, so partial imports never report success. Destructive.
func ImportCommand ¶
ImportCommand renders the psql call for --dry-run.
func ImportWithExecutor ¶
ImportWithExecutor runs a SQL file using the provided executor.
func MigrateVersion ¶
MigrateVersion reports applied and target versions.
func Restore ¶
Restore loads a custom-format dump. all = schema+data (--clean), data = data only, schema = schema only. Destructive.
func RestoreCommand ¶
RestoreCommand renders the pg_restore call for --dry-run.
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.
type MigrationOutcome ¶
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 ¶
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 ¶
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.