pgdump

package
v0.0.0-...-747e920 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpCommand

func DumpCommand(t Target) (args []string, env []string)

DumpCommand generates a pg_dump invocation that can be used for on-prem-to-Cloud migrations.

func FilterInvalidLines

func FilterInvalidLines(dst io.Writer, src io.ReadSeeker, progressFn func(int64)) (int64, error)

FilterInvalidLines copies the initial lines of the pg_dump-created .sql files, from src to dst (the GCS bucket), until it hits a line prefixed with a filterEndMarker, while commenting out the linesToFilter which cause `gcloud sql import` to error out. It then resets src to the position of the last contents written to dst.

Filtering requires reading entire lines into memory, this can be a very expensive operation, so when filtering is complete, the more efficient io.Copy is used to perform the remainder of the copy in the calling funciton

pg_dump writes these .sql files based on its own version, not based on the Postgres version of either the source or destination database; so self-hosted customers' diverse database environments have inserted a variety of statements into the .sql files which cause the import to fail For details, see https://cloud.google.com/sql/docs/postgres/import-export/import-export-dmp

func RestoreCommand

func RestoreCommand(t Target) (args []string, env []string)

RestoreCommand generates a psql invocation that can be used for migrations.

Types

type Command

type Command struct {
	// Env holds additional environment variables in "KEY=value" form.
	Env []string
	// Args is the argv to execute; Args[0] is the executable.
	Args []string
	// InputFile, if set, is opened and connected to the command's stdin.
	InputFile string
	// OutputFile, if set, receives the command's stdout. When empty, stdout is
	// discarded (matching the original "1>/dev/null" behaviour for restores).
	OutputFile string
}

Command represents a command to run as an argv array rather than a shell string. Values that originate from user-controlled targets are always passed as discrete arguments (or safely shell-quoted when they must be embedded in a nested shell command), never interpolated into a string that is handed to "bash -c". This prevents shell injection via malicious targets files.

func BuildCommands

func BuildCommands(outDir string, commandBuilder CommandBuilder, targets Targets, dump bool) ([]Command, error)

BuildCommands generates commands that output Postgres dumps and sends them to predefined files for each target database.

func (Command) Run

func (c Command) Run() ([]byte, error)

Run executes the command safely (without a shell), wiring up any input/output file redirection, and returns the combined stderr (and stdout when it is not redirected to a file) output.

func (Command) String

func (c Command) String() string

String renders the command as a copy-pasteable, safely shell-quoted string. It is intended for display only; execution always goes through Run.

type CommandBuilder

type CommandBuilder func(Target) (Command, error)

func Builder

func Builder(builder string, command PGCommand) (commandBuilder CommandBuilder, targetKey string)

Builder generates the CommandBuilder and targetKey for a given builder and PGCommand

type Output

type Output struct {
	Output string
	Target Target
}

func Outputs

func Outputs(dir string, targets Targets) []Output

Outputs generates a set of mappings between a pgdump.Target and the desired output path. It can be provided a zero-value Targets to just generate the output paths.

type PGCommand

type PGCommand func(Target) (args []string, env []string)

PGCommand generates the argv and environment for a base Postgres command (pg_dump or psql) targeting the given Target.

type Target

type Target struct {
	// Target is the DSN of the database deployment:
	//
	// - in docker, the name of the database container, e.g. pgsql, codeintel-db, codeinsights-db
	// - in k8s, the name of the deployment or statefulset, e.g. deploy/pgsql, sts/pgsql
	// - in plain pg_dump, the server host or socket directory
	Target string `yaml:"target"`

	DBName   string `yaml:"dbname"`
	Username string `yaml:"username"`

	// Only include password if non-sensitive
	Password string `yaml:"password"`
}

Target represents a database for pg_dump to export.

type Targets

type Targets struct {
	Pgsql        Target `yaml:"pgsql"`
	CodeIntel    Target `yaml:"codeintel"`
	CodeInsights Target `yaml:"codeinsights"`
}

Targets represents configuration for each of Sourcegraph's databases.

Jump to

Keyboard shortcuts

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