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

database

package
v0.43.15 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package database provides adapters for database template cloning. Supported adapters: PostgreSQL (server-side createdb --template) and SQLite (file copy). The adapter interface abstracts clone, drop, and existence checks across database types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Clone(template, target string) error
	Drop(target string) error
	Exists(name string) (bool, error)
	Rename(oldName, newName string) error
	Restore(target, dumpFile string) error
}

Adapter defines the interface for database template cloning and cleanup. Implementations handle database-specific operations:

  • Clone creates a new database from a template
  • Drop removes a database
  • Exists checks if a database already exists
  • Rename renames an existing database in place
  • Restore loads a dump file into a database

func ForAdapter

func ForAdapter(name string, connArgs []string) (Adapter, error)

ForAdapter returns the adapter for the given name. Defaults to PostgreSQL for empty string (backward compatibility). connArgs are connection flags (e.g. ["-h", "localhost"]) prepended to every pg tool invocation; pass nil when no explicit connection is needed.

type ExecError added in v0.43.8

type ExecError struct {
	Stage  string // "dump", "drop", "create", "extension", "list", "restore"
	Output string // captured combined stderr (best effort)
	Err    error
}

ExecError carries the stage and captured stderr of a failed pg tool call so the cmd layer can classify it into a user-facing hint.

func (*ExecError) Error added in v0.43.8

func (e *ExecError) Error() string

func (*ExecError) Unwrap added in v0.43.8

func (e *ExecError) Unwrap() error

type Extensions added in v0.43.8

type Extensions struct {
	Require []string
	Strip   []string
}

Extensions captures the extension handling for a restore: Require names are pre-created (CREATE EXTENSION IF NOT EXISTS), Strip names are commented out of the restore TOC (for cloud-only extensions absent locally).

type PostgreSQL

type PostgreSQL struct {
	// ConnArgs are prepended to every pg tool invocation (e.g. ["-h", "localhost", "-p", "5432"]).
	// Setting -h forces TCP instead of Unix socket, which avoids Postgres.app's
	// socket authorization dialog when gtl is invoked from a native macOS app.
	ConnArgs []string
	// contains filtered or unexported fields
}

PostgreSQL implements the Adapter interface for PostgreSQL databases. Clone uses createdb --template, Drop uses dropdb --if-exists.

func (*PostgreSQL) Clone

func (pg *PostgreSQL) Clone(template, target string) error

func (*PostgreSQL) Drop

func (pg *PostgreSQL) Drop(target string) error

func (*PostgreSQL) Exists

func (pg *PostgreSQL) Exists(name string) (bool, error)

func (*PostgreSQL) Rename added in v0.43.7

func (pg *PostgreSQL) Rename(oldName, newName string) error

func (*PostgreSQL) Restore

func (pg *PostgreSQL) Restore(target, dumpFile string) error

type Puller added in v0.43.8

type Puller struct {
	// LocalConnArgs are prepended to local pg tool calls (same as PostgreSQL.ConnArgs).
	LocalConnArgs []string
	// Logf, when set, receives a redacted line for each exec (for --debug).
	Logf func(format string, a ...any)
	// contains filtered or unexported fields
}

Puller runs the remote-pull / refresh pipeline against a LOCAL target db. It reuses the same exec-seam pattern as PostgreSQL but adds an env-aware variant so PGPASSWORD/PGSSLMODE can be passed to the remote pg_dump via the process environment — never on argv.

func NewPuller added in v0.43.8

func NewPuller(localConnArgs []string) *Puller

NewPuller returns a Puller wired to real exec, prepending localConnArgs to local pg tool calls.

func (*Puller) Dump added in v0.43.8

func (p *Puller) Dump(remote *RemoteConn, dumpPath string) error

Dump writes a custom-format dump of the remote database to dumpPath. The remote password and sslmode travel via the environment, so they never appear in argv. The dump file must exist and be non-empty before any caller drops the local db.

func (*Puller) Refresh added in v0.43.8

func (p *Puller) Refresh(target, dumpPath, listPath string, exts Extensions) error

Refresh drops and recreates the local target, pre-creates required extensions, builds a stripped restore list when needed, and restores the dump. It performs no network access — pull calls Dump first, refresh reuses an already-retained dump. listPath is where a stripped TOC may be written.

type RemoteConn added in v0.43.8

type RemoteConn struct {
	Host     string
	Port     string
	User     string
	Password string
	DBName   string
	SSLMode  string
}

RemoteConn is a resolved remote PostgreSQL connection. It mirrors dbsource.ConnInfo; the cmd layer maps between them so this package stays a leaf (no dbsource import).

type SQLite

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

func (*SQLite) Clone

func (s *SQLite) Clone(template, target string) error

func (*SQLite) Drop

func (s *SQLite) Drop(target string) error

func (*SQLite) Exists

func (s *SQLite) Exists(name string) (bool, error)

func (*SQLite) Rename added in v0.43.7

func (s *SQLite) Rename(oldPath, newPath string) error

func (*SQLite) Restore

func (s *SQLite) Restore(target, dumpFile string) error

Jump to

Keyboard shortcuts

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