pgcommands

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 8 Imported by: 13

README

pg-commands

codecov Release Go version CircleCI License Go Reference

install

go get -t github.com/habx/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"
)
View Source
var (
	// PGRestoreCmd is the path to the `pg_restore` executable
	PGRestoreCmd = "pg_restore"
)

Functions

func CommandExist added in v0.4.0

func CommandExist(command string) bool

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, error)

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 added in v0.3.0

func (x *Dump) GetFileName() string

func (*Dump) IgnoreTableDataToString added in v0.1.2

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

func (*Dump) ResetOptions

func (x *Dump) ResetOptions()

func (*Dump) SetFileName added in v0.3.0

func (x *Dump) SetFileName(filename string)

func (*Dump) SetPath added in v0.1.0

func (x *Dump) SetPath(path string)

func (*Dump) SetupFormat

func (x *Dump) SetupFormat(f string)

type ErrCommandNotFound added in v0.4.0

type ErrCommandNotFound struct {
	Command string
}

func (ErrCommandNotFound) Error added in v0.4.0

func (e ErrCommandNotFound) Error() string

type ExecOptions added in v0.1.3

type ExecOptions struct {
	StreamPrint       bool
	StreamDestination io.Writer
}

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, error)

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 added in v0.1.0

func (x *Restore) SetPath(path string)

func (*Restore) SetSchemas added in v0.2.0

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