pg_commands

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2022 License: MIT Imports: 7 Imported by: 0

README

pg-commands

codecov Release Go version CircleCI License Go Reference

install

go get -t github.com/guru-golang/pg-commands

Example

Code
dump := pg.NewDump(&pg.Postgres{
    Host:     "localhost",
    Port:     5432,
    DB:       "dev_example",
    Username: "example",
    Password: "example",
})
dumpExec := dump.Exec(pg.ExecOptions{StreamPrint: false})
if dumpExec.Error != nil {
    fmt.Println(dumpExec.Error.Err)
    fmt.Println(dumpExec.Output)

} else {
    fmt.Println("Dump success")
    fmt.Println(dumpExec.Output)
}

restore := pg.NewRestore(&pg.Postgres{
    Host:     "localhost",
    Port:     5432,
    DB:       "dev_example",
    Username: "example",
    Password: "example",
})
restoreExec := restore.Exec(dumpExec.File, pg.ExecOptions{StreamPrint: false})
if restoreExec.Error != nil {
    fmt.Println(restoreExec.Error.Err)
    fmt.Println(restoreExec.Output)

} else {
    fmt.Println("Restore success")
    fmt.Println(restoreExec.Output)

}
Lab
$ cd examples
$ docker-compose up -d
$ cd ..
$ POSTGRES_USER=example POSTGRES_PASSWORD=example POSTGRES_DB=postgres  go run tests/fixtures/scripts/init-database/init-database.go

$ go run main.go
Dump success

Restore success

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// PGDumpCmd is the path to the `pg_dump` executable
	PGDumpCmd           = "pg_dump"
	PGDumpStdOpts       = []string{"--no-owner", "--no-acl", "--clean", "--blob"}
	PGDumpDefaultFormat = "c"
)
View Source
var (
	// PGRestoreCmd is the path to the `pg_restore` executable
	PGRestoreCmd      = "pg_restore"
	PGDRestoreStdOpts = []string{"--no-owner", "--no-acl", "--clean", "--exit-on-error"}
)

Functions

This section is empty.

Types

type Dump

type Dump struct {
	*Postgres
	// Verbose mode
	Verbose bool
	// Path: setup path dump out
	Path string
	// Format: output file format (custom, directory, tar, plain text (default))
	Format *string
	// Extra pg_dump x.FullOptions
	// e.g []string{"--inserts"}
	Options []string

	IgnoreTableData []string
	// contains filtered or unexported fields
}

Dump is an `Exporter` interface that backs up a Postgres database via the `pg_dump` command.

func NewDump

func NewDump(pg *Postgres) *Dump

func (*Dump) EnableVerbose

func (x *Dump) EnableVerbose()

func (*Dump) Exec

func (x *Dump) Exec(opts ExecOptions) Result

Exec `pg_dump` of the specified database, and creates a gzip compressed tarball archive.

func (*Dump) GetFileName

func (x *Dump) GetFileName() string

func (*Dump) IgnoreTableDataToString

func (x *Dump) IgnoreTableDataToString() []string

func (*Dump) ResetOptions

func (x *Dump) ResetOptions()

func (*Dump) SetFileName

func (x *Dump) SetFileName(filename string)

func (*Dump) SetPath

func (x *Dump) SetPath(path string)

func (*Dump) SetupFormat

func (x *Dump) SetupFormat(f string)

type ExecOptions

type ExecOptions struct {
	StreamPrint bool
}

type Postgres

type Postgres struct {
	// DB Host (e.g. 127.0.0.1)
	Host string
	// DB Port (e.g. 5432)
	Port int
	// DB Name
	DB string
	// Connection Username
	Username string
	// Connection Password
	Password string
	// Connection Password ENV format PGPASSWORD=
	EnvPassword string
}

func (*Postgres) Parse

func (x *Postgres) Parse() []string

type Restore

type Restore struct {
	*Postgres
	// Verbose mode
	Verbose bool
	// Role: do SET ROLE before restore
	Role string
	// Path: setup path for source restore
	Path string
	// Extra pg_dump options
	// e.g []string{"--inserts"}
	Options []string
	// Schemas: list of database schema
	Schemas []string
}

func NewRestore

func NewRestore(pg *Postgres) *Restore

func (*Restore) EnableVerbose

func (x *Restore) EnableVerbose()

func (*Restore) Exec

func (x *Restore) Exec(filename string, opts ExecOptions) Result

Exec `pg_restore` of the specified database, and restore from a gzip compressed tarball archive.

func (*Restore) ResetOptions

func (x *Restore) ResetOptions()

func (*Restore) SetPath

func (x *Restore) SetPath(path string)

func (*Restore) SetSchemas

func (x *Restore) SetSchemas(schemas []string)

type Result

type Result struct {
	Mine        string
	File        string
	Output      string
	Error       *ResultError
	FullCommand string
}

type ResultError

type ResultError struct {
	Err       error
	CmdOutput string
	ExitCode  int
}

type Results

type Results struct {
	Dump    Result
	Restore Result
}

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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