pg_commands

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 6 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 (
	// DumpCmd is the path to the `pg_dump` executable
	DumpCmd           = "pg_dump"
	DumpStdOpts       = []string{}
	DumpDefaultFormat = "p" // p  c  d  t
)
View Source
var (
	// RestoreCmd is the path to the `pg_restore` executable
	RestoreCmd           = "pg_restore"
	RestoreStdOpts       = []string{"--exit-on-error"}
	RestoreDefaultFormat = "p" // p  c  d  t
)

Functions

This section is empty.

Types

type Dump

type Dump struct {
	*Postgres

	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) Exec

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

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

func (*Dump) GetFile added in v0.2.0

func (x *Dump) GetFile() string

func (*Dump) GetFormat added in v0.2.0

func (x *Dump) GetFormat() string

func (*Dump) GetOptions added in v0.2.0

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

func (*Dump) GetPath added in v0.2.0

func (x *Dump) GetPath() string

func (*Dump) GetVerbose added in v0.2.0

func (x *Dump) GetVerbose() bool

func (*Dump) IgnoreTableDataToString

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

func (*Dump) SetFile added in v0.2.0

func (x *Dump) SetFile(filename string)

func (*Dump) SetFormat added in v0.2.0

func (x *Dump) SetFormat(f string)

func (*Dump) SetOptions added in v0.2.0

func (x *Dump) SetOptions(o []string)

func (*Dump) SetPath

func (x *Dump) SetPath(path string)

func (*Dump) SetVerbose added in v0.2.0

func (x *Dump) SetVerbose(verbose bool)

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
	// contains filtered or unexported fields
}

func NewRestore

func NewRestore(pg *Postgres) *Restore

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) GetFormat added in v0.2.0

func (x *Restore) GetFormat() string

func (*Restore) GetOptions added in v0.2.0

func (x *Restore) GetOptions() []string

func (*Restore) GetPath added in v0.2.0

func (x *Restore) GetPath() string

func (*Restore) GetSchemas added in v0.2.0

func (x *Restore) GetSchemas() []string

func (*Restore) GetVerbose added in v0.2.0

func (x *Restore) GetVerbose() bool

func (*Restore) SetFormat added in v0.2.0

func (x *Restore) SetFormat(f string)

func (*Restore) SetOptions added in v0.2.0

func (x *Restore) SetOptions(o []string)

func (*Restore) SetPath

func (x *Restore) SetPath(path string)

func (*Restore) SetSchemas

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

func (*Restore) SetVerbose added in v0.2.0

func (x *Restore) SetVerbose(verbose bool)

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