From 301727d1fc42216dc2c468bdda59ebc4135be442 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Fri, 12 Aug 2022 22:15:13 -0500 Subject: [PATCH] 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. --- coderd/database/postgres/postgres.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/coderd/database/postgres/postgres.go b/coderd/database/postgres/postgres.go index d1ef7b3084..a1637cfb02 100644 --- a/coderd/database/postgres/postgres.go +++ b/coderd/database/postgres/postgres.go @@ -9,6 +9,7 @@ import ( "sync" "time" + "github.com/cenkalti/backoff/v4" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" "golang.org/x/xerrors" @@ -123,27 +124,38 @@ func Open() (string, func(), error) { } pool.MaxWait = 120 * time.Second + + // Record the error that occurs during the retry. + // The 'pool' pkg hardcodes a deadline error devoid + // of any useful context. + var retryErr error err = pool.Retry(func() error { db, err := sql.Open("postgres", dbURL) if err != nil { - return xerrors.Errorf("open postgres: %w", err) + retryErr = xerrors.Errorf("open postgres: %w", err) + return retryErr } defer db.Close() err = db.Ping() if err != nil { - return xerrors.Errorf("ping postgres: %w", err) + retryErr = xerrors.Errorf("ping postgres: %w", err) + return retryErr } + err = database.MigrateUp(db) if err != nil { - return xerrors.Errorf("migrate db: %w", err) + retryErr = xerrors.Errorf("migrate db: %w", err) + // Only try to migrate once. + return backoff.Permanent(retryErr) } return nil }) if err != nil { - return "", nil, err + return "", nil, retryErr } + return dbURL, func() { _ = pool.Purge(resource) _ = os.RemoveAll(tempDir)