|
9 | 9 | "sync"
|
10 | 10 | "time"
|
11 | 11 |
|
| 12 | + "github.com/cenkalti/backoff/v4" |
12 | 13 | "github.com/ory/dockertest/v3"
|
13 | 14 | "github.com/ory/dockertest/v3/docker"
|
14 | 15 | "golang.org/x/xerrors"
|
@@ -123,27 +124,38 @@ func Open() (string, func(), error) {
|
123 | 124 | }
|
124 | 125 |
|
125 | 126 | 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 |
126 | 132 | err = pool.Retry(func() error {
|
127 | 133 | db, err := sql.Open("postgres", dbURL)
|
128 | 134 | if err != nil {
|
129 |
| - return xerrors.Errorf("open postgres: %w", err) |
| 135 | + retryErr = xerrors.Errorf("open postgres: %w", err) |
| 136 | + return retryErr |
130 | 137 | }
|
131 | 138 | defer db.Close()
|
132 | 139 |
|
133 | 140 | err = db.Ping()
|
134 | 141 | if err != nil {
|
135 |
| - return xerrors.Errorf("ping postgres: %w", err) |
| 142 | + retryErr = xerrors.Errorf("ping postgres: %w", err) |
| 143 | + return retryErr |
136 | 144 | }
|
| 145 | + |
137 | 146 | err = database.MigrateUp(db)
|
138 | 147 | 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) |
140 | 151 | }
|
141 | 152 |
|
142 | 153 | return nil
|
143 | 154 | })
|
144 | 155 | if err != nil {
|
145 |
| - return "", nil, err |
| 156 | + return "", nil, retryErr |
146 | 157 | }
|
| 158 | + |
147 | 159 | return dbURL, func() {
|
148 | 160 | _ = pool.Purge(resource)
|
149 | 161 | _ = os.RemoveAll(tempDir)
|
|
0 commit comments