|
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 | +varretryErrerror |
126 | 132 | err=pool.Retry(func()error {
|
127 | 133 | db,err:=sql.Open("postgres",dbURL)
|
128 | 134 | iferr!=nil {
|
129 |
| -returnxerrors.Errorf("open postgres: %w",err) |
| 135 | +retryErr=xerrors.Errorf("open postgres: %w",err) |
| 136 | +returnretryErr |
130 | 137 | }
|
131 | 138 | deferdb.Close()
|
132 | 139 |
|
133 | 140 | err=db.Ping()
|
134 | 141 | iferr!=nil {
|
135 |
| -returnxerrors.Errorf("ping postgres: %w",err) |
| 142 | +retryErr=xerrors.Errorf("ping postgres: %w",err) |
| 143 | +returnretryErr |
136 | 144 | }
|
| 145 | + |
137 | 146 | err=database.MigrateUp(db)
|
138 | 147 | iferr!=nil {
|
139 |
| -returnxerrors.Errorf("migrate db: %w",err) |
| 148 | +retryErr=xerrors.Errorf("migrate db: %w",err) |
| 149 | +// Only try to migrate once. |
| 150 | +returnbackoff.Permanent(retryErr) |
140 | 151 | }
|
141 | 152 |
|
142 | 153 | returnnil
|
143 | 154 | })
|
144 | 155 | iferr!=nil {
|
145 |
| -return"",nil,err |
| 156 | +return"",nil,retryErr |
146 | 157 | }
|
| 158 | + |
147 | 159 | returndbURL,func() {
|
148 | 160 | _=pool.Purge(resource)
|
149 | 161 | _=os.RemoveAll(tempDir)
|
|