Skip to content

Commit 301727d

Browse files
authored
chore: improve dump error output (#3499)
* chore: improve dump error output - Properly report the error that occurs during the DB connection retry loop. - Fail fatally if migration is unsuccessful.
1 parent 8cf8211 commit 301727d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

coderd/database/postgres/postgres.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/cenkalti/backoff/v4"
1213
"github.com/ory/dockertest/v3"
1314
"github.com/ory/dockertest/v3/docker"
1415
"golang.org/x/xerrors"
@@ -123,27 +124,38 @@ func Open() (string, func(), error) {
123124
}
124125

125126
pool.MaxWait = 120 * time.Second
127+
128+
// Record the error that occurs during the retry.
129+
// The 'pool' pkg hardcodes a deadline error devoid
130+
// of any useful context.
131+
var retryErr error
126132
err = pool.Retry(func() error {
127133
db, err := sql.Open("postgres", dbURL)
128134
if err != nil {
129-
return xerrors.Errorf("open postgres: %w", err)
135+
retryErr = xerrors.Errorf("open postgres: %w", err)
136+
return retryErr
130137
}
131138
defer db.Close()
132139

133140
err = db.Ping()
134141
if err != nil {
135-
return xerrors.Errorf("ping postgres: %w", err)
142+
retryErr = xerrors.Errorf("ping postgres: %w", err)
143+
return retryErr
136144
}
145+
137146
err = database.MigrateUp(db)
138147
if err != nil {
139-
return xerrors.Errorf("migrate db: %w", err)
148+
retryErr = xerrors.Errorf("migrate db: %w", err)
149+
// Only try to migrate once.
150+
return backoff.Permanent(retryErr)
140151
}
141152

142153
return nil
143154
})
144155
if err != nil {
145-
return "", nil, err
156+
return "", nil, retryErr
146157
}
158+
147159
return dbURL, func() {
148160
_ = pool.Purge(resource)
149161
_ = os.RemoveAll(tempDir)

0 commit comments

Comments
 (0)