Merge pull request #358 from sosedoff/check-for-pgdump

Check if pg_dump is available before running database export
This commit is contained in:
Dan Sosedoff 2018-04-26 00:14:17 -05:00 committed by GitHub
commit b18af0b907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -472,6 +472,13 @@ func DataExport(c *gin.Context) {
Table: strings.TrimSpace(c.Request.FormValue("table")), Table: strings.TrimSpace(c.Request.FormValue("table")),
} }
// If pg_dump is not available the following code will not show an error in browser.
// This is due to the headers being written first.
if !dump.CanExport() {
c.JSON(400, Error{"pg_dump is not found"})
return
}
formattedInfo := info.Format()[0] formattedInfo := info.Format()[0]
filename := formattedInfo["current_database"].(string) filename := formattedInfo["current_database"].(string)
if dump.Table != "" { if dump.Table != "" {

View File

@ -11,6 +11,11 @@ type Dump struct {
Table string Table string
} }
func (d *Dump) CanExport() bool {
err := exec.Command("pg_dump", "--version").Run()
return err == nil
}
func (d *Dump) Export(url string, writer io.Writer) error { func (d *Dump) Export(url string, writer io.Writer) error {
errOutput := bytes.NewBuffer(nil) errOutput := bytes.NewBuffer(nil)

View File

@ -24,8 +24,12 @@ func test_DumpExport(t *testing.T) {
os.Remove(savePath) os.Remove(savePath)
}() }()
// Test full db dump
dump := Dump{} dump := Dump{}
// Test for pg_dump presence
assert.True(t, dump.CanExport())
// Test full db dump
err = dump.Export(url, saveFile) err = dump.Export(url, saveFile)
assert.NoError(t, err) assert.NoError(t, err)