Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/postgres/pg_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,26 @@ func RunPGRestore(ctx context.Context, opts PGRestoreOptions, dump []byte) (stri

// TODO: add streaming support when large data output is required
out, err := cmd.CombinedOutput()
if err != nil || strings.Contains(string(out), "ERROR") {
return "", fmt.Errorf("error restoring dump: %w", parsePgRestoreOutputErrs(out))
if restoreErr := buildRestoreError(out, err); restoreErr != nil {
return "", restoreErr
}

return string(out), nil
}

func buildRestoreError(out []byte, execErr error) error {
if execErr == nil && !strings.Contains(string(out), "ERROR") {
return nil
}
if parseErr := parsePgRestoreOutputErrs(out); parseErr != nil {
return fmt.Errorf("error restoring dump: %w", parseErr)
}
if execErr != nil {
return fmt.Errorf("error restoring dump: %w", execErr)
}
return nil
}

func removeDatabaseFromConnectionString(url string) (string, error) {
dbName, err := extractDatabase(url)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions internal/postgres/pg_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,67 @@ pg_restore: finished`,
}
}

func TestBuildRestoreError(t *testing.T) {
t.Parallel()

execErr := errors.New("exit status 1")

tests := []struct {
name string
output []byte
execErr error

wantNil bool
wantContain string
}{
{
name: "no error - success",
output: []byte("pg_restore: finished\n"),
execErr: nil,
wantNil: true,
},
{
name: "exec error with no parseable output",
output: []byte("some unexpected output\n"),
execErr: execErr,
wantContain: "exit status 1",
},
{
name: "exec error with empty output",
output: []byte{},
execErr: execErr,
wantContain: "exit status 1",
},
{
name: "exec error with parseable ERROR lines",
output: []byte("pg_restore: error: could not execute query: ERROR: relation \"users\" already exists\n"),
execErr: execErr,
wantContain: "already exists",
},
{
name: "no exec error but output contains ERROR",
output: []byte("ERROR: relation \"users\" already exists\n"),
execErr: nil,
wantContain: "already exists",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

err := buildRestoreError(tc.output, tc.execErr)
if tc.wantNil {
require.NoError(t, err)
return
}
require.Error(t, err)
assert.Contains(t, err.Error(), tc.wantContain)
assert.NotContains(t, err.Error(), "%!w(<nil>)")
})
}
}

func TestIsErrorLine(t *testing.T) {
tests := []struct {
line string
Expand Down
Loading