Drop unsupported pg_dump options from connection string
This commit is contained in:
parent
d5e4a10137
commit
b6f01da232
@ -4,19 +4,36 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
unsupportedDumpOptions = []string{
|
||||||
|
"search_path",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Dump represents a database dump
|
||||||
type Dump struct {
|
type Dump struct {
|
||||||
Table string
|
Table string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanExport returns true if database dump tool could be used without an error
|
||||||
func (d *Dump) CanExport() bool {
|
func (d *Dump) CanExport() bool {
|
||||||
err := exec.Command("pg_dump", "--version").Run()
|
err := exec.Command("pg_dump", "--version").Run()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dump) Export(url string, writer io.Writer) error {
|
// Export streams the database dump to the specified writer
|
||||||
|
func (d *Dump) Export(connstr string, writer io.Writer) error {
|
||||||
|
if str, err := removeUnsupportedOptions(connstr); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
connstr = str
|
||||||
|
}
|
||||||
|
|
||||||
errOutput := bytes.NewBuffer(nil)
|
errOutput := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
opts := []string{
|
opts := []string{
|
||||||
@ -29,7 +46,7 @@ func (d *Dump) Export(url string, writer io.Writer) error {
|
|||||||
opts = append(opts, []string{"--table", d.Table}...)
|
opts = append(opts, []string{"--table", d.Table}...)
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = append(opts, url)
|
opts = append(opts, connstr)
|
||||||
|
|
||||||
cmd := exec.Command("pg_dump", opts...)
|
cmd := exec.Command("pg_dump", opts...)
|
||||||
cmd.Stdout = writer
|
cmd.Stdout = writer
|
||||||
@ -40,3 +57,20 @@ func (d *Dump) Export(url string, writer io.Writer) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeUnsupportedOptions removes any options unsupported for making a db dump
|
||||||
|
func removeUnsupportedOptions(input string) (string, error) {
|
||||||
|
uri, err := url.Parse(input)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
q := uri.Query()
|
||||||
|
for _, opt := range unsupportedDumpOptions {
|
||||||
|
q.Del(opt)
|
||||||
|
q.Del(strings.ToUpper(opt))
|
||||||
|
}
|
||||||
|
uri.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
return uri.String(), nil
|
||||||
|
}
|
||||||
|
@ -34,8 +34,8 @@ func testDumpExport(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Test nonexistent database
|
// Test nonexistent database
|
||||||
invalidUrl := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, "foobar")
|
invalidURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, "foobar")
|
||||||
err = dump.Export(invalidUrl, saveFile)
|
err = dump.Export(invalidURL, saveFile)
|
||||||
assert.Contains(t, err.Error(), `database "foobar" does not exist`)
|
assert.Contains(t, err.Error(), `database "foobar" does not exist`)
|
||||||
|
|
||||||
// Test dump of non existent db
|
// Test dump of non existent db
|
||||||
@ -43,4 +43,10 @@ func testDumpExport(t *testing.T) {
|
|||||||
err = dump.Export(url, saveFile)
|
err = dump.Export(url, saveFile)
|
||||||
assert.NotNil(t, err)
|
assert.NotNil(t, err)
|
||||||
assert.Contains(t, err.Error(), "pg_dump: no matching tables were found")
|
assert.Contains(t, err.Error(), "pg_dump: no matching tables were found")
|
||||||
|
|
||||||
|
// Should drop "search_path" param from URI
|
||||||
|
dump = Dump{}
|
||||||
|
searchPathURL := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable&search_path=private", serverUser, serverHost, serverPort, serverDatabase)
|
||||||
|
err = dump.Export(searchPathURL, saveFile)
|
||||||
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user