Skip to content

Commit 4408361

Browse files
tantovanwijheclaude
andcommitted
Surface psql errors and abort cleanly on failed Postgres restore
Run psql with ON_ERROR_STOP=1 and --single-transaction so a restore that hits an error rolls back instead of leaving a half-applied dump, and capture psql's stderr so the failure message reports the underlying cause and a schema-compatibility hint rather than a generic "couldn't connect". Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0c6af57 commit 4408361

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

pkg/restore/restore.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,24 +230,36 @@ func restoreBackupToPostgres(target util.TargetConfig, backup string) error {
230230
sp := util.StartSpinner(fmt.Sprintf("Restoring Postgres %s (%s:%s)", target.Database, target.Hostname, target.Port))
231231

232232
if target.Environment != "development" {
233-
cmd := exec.Command("psql", "-d", target.Database, "-h", target.Hostname, "-p", target.Port, "-U", target.Username, "-f", backup)
233+
cmd := exec.Command("psql", "-v", "ON_ERROR_STOP=1", "--single-transaction", "-d", target.Database, "-h", target.Hostname, "-p", target.Port, "-U", target.Username, "-f", backup)
234234
cmd.Env = os.Environ()
235235
cmd.Env = append(cmd.Env, "PGPASSWORD="+target.Password)
236+
var stderr bytes.Buffer
237+
cmd.Stderr = &stderr
236238
err := cmd.Run()
237239
if err != nil {
238240
sp.StopFail()
239-
return errors.Errorf("Couldn't connect to the target database. Please check that the proxy is running on port %s\n\n%s", target.Port, err.Error())
241+
detail := strings.TrimSpace(stderr.String())
242+
if detail == "" {
243+
detail = err.Error()
244+
}
245+
return errors.Errorf("Restore failed. Check that the proxy is running on port %s and that the dump is compatible with the target schema.\n\n%s", target.Port, detail)
240246
}
241247
} else {
242248
// Attempt to create the database in case it doesn't exist
243249
createCmd := exec.Command("createdb", target.Database, "-h", target.Hostname, "-p", target.Port)
244250
createCmd.Run()
245251

246-
cmd := exec.Command("psql", "-d", target.Database, "-h", target.Hostname, "-p", target.Port, "-f", backup)
252+
cmd := exec.Command("psql", "-v", "ON_ERROR_STOP=1", "--single-transaction", "-d", target.Database, "-h", target.Hostname, "-p", target.Port, "-f", backup)
253+
var stderr bytes.Buffer
254+
cmd.Stderr = &stderr
247255
err := cmd.Run()
248256
if err != nil {
249257
sp.StopFail()
250-
return errors.Errorf("Couldn't connect to the target database. Please check that your database server running on port %s\n\n%s", target.Port, err.Error())
258+
detail := strings.TrimSpace(stderr.String())
259+
if detail == "" {
260+
detail = err.Error()
261+
}
262+
return errors.Errorf("Restore failed. Check that your database server is running on port %s and that the dump is compatible with the target schema.\n\n%s", target.Port, detail)
251263
}
252264
}
253265
sp.Stop()

0 commit comments

Comments
 (0)