Giter Club home page Giter Club logo

pg-commands's Introduction

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

pg-commands's People

Contributors

habxtech avatar clementlecorre avatar habx-auto-merge[bot] avatar renovate-bot avatar renovate[bot] avatar dependabot[bot] avatar tonsv2 avatar fclairamb avatar

Stargazers

raf avatar Luis Quiñones avatar Germano avatar George Lima avatar JCoupalK avatar Nilliam Cris Ometto avatar wubangbang avatar Yongtao Huang avatar Lowe Schmidt avatar Oliver Guggenbühl avatar Edson Vieira  avatar cocoonk1d avatar Junmin Ahn avatar Steven Kessler avatar Ulaş SAYGIN avatar Cuicheng Lu avatar Juan Luis Caro avatar Ruben Atinho H. avatar  avatar  avatar Mikhail avatar Marco Beierer avatar aughyvikrii avatar Igor Sannikov avatar yamadatt avatar Alex Spitzer avatar Pedro Moraes avatar Vinh Quốc Nguyễn avatar Jakob Gillich avatar Lee Cheung Hei avatar Karthik avatar almokhtar avatar Harry avatar Dustin Decker avatar Ian Foster avatar Chris Jelly avatar Cyan Ho avatar Andrei Kigai avatar tharuta avatar DY avatar ALi.w avatar Jean Dessane avatar Big Drum avatar  avatar Anonymous avatar Roberto Dedoro avatar zbv avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

pg-commands's Issues

Additional feature for pg_dump..

What if we need to take only schema backup without data? (pg_dump -s)

What if we need to take a specific schema backup with database?(pg_dump -n

pg_dump: illegal option -- blob Error

Hi, I am using postgres v14
And I have downloaded github.com/habx/pg-commands v0.6.1 in my golang backend project.
I have set the correct env path to postgresql bin directory.
But if I run the backend, I got pg_dump: illegal option -- blob error when I attempt to dump.
Please, help!

Race condition in Dump.Exec

I encountered a race condition detected from go test and after reviewing your CircleCI Orb i see you have it turned off. It might be worth enabling it to catch those cases.

Orb Source line 312+

- go/test:
                covermode: atomic
                coverprofile: coverage.out
                failfast: true
                race: false
                verbose: true

Race condition was encountered between

Read at 0x00c0003fc020 by goroutine 67:
  github.com/habx/pg-commands.(*Dump).Exec()
      /x/x/go/pkg/mod/github.com/habx/[email protected]/pg_dump.go:64 +0xb16

and

Previous write at 0x00c0003fc020 by goroutine 68:
  github.com/habx/pg-commands.streamOutput()
      /x/x/go/pkg/mod/github.com/habx/[email protected]/utils.go:45 +0x1d3
  github.com/habx/pg-commands.(*Dump).Exec.func1()
      /x/x/go/pkg/mod/github.com/habx/[email protected]/pg_dump.go:54 +0x8a

I was running a unit test which uses suite.run to sequentially trigger multiple database exports. In Exec the streamOutput function is started as a go routine and can not be observed from outside to know when it has finished. I believe that is where the race condition occurs as it is unmanaged writing and reading from the Result.Error variable in both routines

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Pending Status Checks

These updates await pending status checks. To force their creation now, click the checkbox below.

  • fix(deps): update module github.com/go-pg/pg/v10 to v10.11.0

Detected dependencies

circleci
.circleci/config.yml
  • go 0.14.0
gomod
go.mod
  • go 1.19
  • github.com/go-pg/pg/v10 v10.10.7
  • github.com/smartystreets/goconvey v1.7.2

  • Check this box to trigger a request for Renovate to run again on this repository

Stream logs to other destinations than stdout

As part of a system I'm working on we're dumping (and restoring) some fairly large databases (+50GB). Obviously that takes some time and we would like to keep the users aware of the progress.

The problem I'm encountering is that the output destination is hard coded to stdout as seen here.

Please let me know if there's a better way of solving the above issue. But I think this would be best solved by adding a io.Writer to the ExecOptions struct. It should obviously default to os.Stdout but it could be used to stream the output to multiple destinations ranging from a simple file to a database while the dump is taking place.

Would you guys be interested in a PR implementing the above feature?

pg_dump version mismatch

I am attempting to build an all in one utility to facilitate in backup/restore of various remote databases to/from object stores such as S3/MinIO/Azure Blob and storing all transactions in Redis. We recently updated our PostgresQL versions to 16, and I cannot use pg_dump in this package as the version is 14

2024/03/03 16:40:54 error: &{Err:exit status 1 CmdOutput:pg_dump: error: server version: 16.2 (Debian 16.2-1.pgdg120+2); pg_dump version: 14.11 (Ubuntu 14.11-0ubuntu0.22.04.1)

Can I update the version used in this package?

No output despite setting StreamPrint to true

As the title indicates, I get no output despite creating my object as below.

dumpExec := dump.Exec(pg.ExecOptions{StreamPrint: true})

I'm using version v0.3.0.

Please let me know if I should post the rest of my code or if there's anything else I can do to assist.

panic: command not found: pg_dump

Hi, I have a problem. Console error this like following
panic: command not found: pg_dump

What is problem?

go version go1.19.5 windows/amd64

CODE

package main

import (
"fmt"
pg "github.com/habx/pg-commands"
"os"
)

func main() {

fmt.Println("backupp")

dump, err := pg.NewDump(&pg.Postgres{
	Host:     "localhost",
	Port:     5432,
	DB:       "dev_example",
	Username: "example",
	Password: "example",
})
if err != nil {
	panic(err)
}

dump.EnableVerbose()

dumpExec := dump.Exec(pg.ExecOptions{StreamPrint: true, StreamDestination: os.Stdout})
if dumpExec.Error != nil {
	fmt.Println(dumpExec.Error.Err)
	fmt.Println(dumpExec.Output)

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

restore, err := pg.NewRestore(&pg.Postgres{
	Host:     "example",
	Port:     5432,
	DB:       "dev_example_new",
	Username: "example",
	Password: "example",
})
if err != nil {
	panic(err)
}
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)

}

}

Expose Method For Specifying Filename

I attempted to dump a few small tables. The actions occurred completed so fast that the timestamp used to ensure uniqueness were all the same. It would be great to expose a method that enables a user to specify the full filename

No error returned by dump.Exec() if pg_dump binary not found

As in the title.

Running the following code, based on the example:

func backupPostGres(host, user, pass, db, backupDir string, port int) error {
	filename := "backup.tar"
	dump := pg.NewDump(&pg.Postgres{
		Host:     host,
		Port:     port,
		DB:       db,
		Username: user,
		Password: pass,
	})
	dump.SetPath(backupDir)
	dump.SetFileName(filename)
	res := dump.Exec(pg.ExecOptions{StreamPrint: false})
	if res.Error != nil {
		return res.Error.Err
	}
	fmt.Println(res.Output)
	return nil
}

Yielded no errors and no log output.

When the binary is present and returns an error, the above code simply returns the error message exit status 1. It would be worth including wrapping the error with Error.CmdOutput and logging this in the example. e.g:

	if res.Error != nil {
		err := fmt.Errorf("%w: %s", res.Error.Err, res.Error.CmdOutput)
		log.Print(err)
		return err
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.