@@ -47,7 +47,6 @@ type PostgresDB struct {
47
47
containerID string
48
48
hostAddress string
49
49
containerAddress string
50
- address string
51
50
52
51
mutex sync.Mutex
53
52
stopped bool
@@ -155,10 +154,8 @@ func (c *PostgresDB) Run(sigCh <-chan os.Signal, ready chan<- struct{}) error {
155
154
return errors .Wrapf (ctx .Err (), "database in container %s did not start" , c .containerID )
156
155
case <- containerExit :
157
156
return errors .New ("container exited before ready" )
158
- case <- c .ready (ctx , c .hostAddress ):
159
- c .address = c .hostAddress
160
- case <- c .ready (ctx , c .containerAddress ):
161
- c .address = c .containerAddress
157
+ case <- c .ready (ctx ):
158
+ break
162
159
}
163
160
164
161
cancel ()
@@ -178,14 +175,8 @@ func (c *PostgresDB) Run(sigCh <-chan os.Signal, ready chan<- struct{}) error {
178
175
}
179
176
}
180
177
181
- func (c * PostgresDB ) endpointReady (ctx context.Context , addr string ) bool {
182
- dataSource := fmt .Sprintf ("host=%s port=%d user=postgres dbname=postgres sslmode=disable" , c .HostIP , c .HostPort )
183
- db , err := sqlx .Open ("postgres" , dataSource )
184
- if err != nil {
185
- return false
186
- }
187
-
188
- _ , err = db .Conn (ctx )
178
+ func (c * PostgresDB ) endpointReady (ctx context.Context , db * sqlx.DB ) bool {
179
+ _ , err := db .Conn (ctx )
189
180
if err != nil {
190
181
return false
191
182
}
@@ -194,13 +185,20 @@ func (c *PostgresDB) endpointReady(ctx context.Context, addr string) bool {
194
185
return true
195
186
}
196
187
197
- func (c * PostgresDB ) ready (ctx context.Context , addr string ) <- chan struct {} {
188
+ func (c * PostgresDB ) ready (ctx context.Context ) <- chan struct {} {
198
189
readyCh := make (chan struct {})
190
+
191
+ connStr , _ := c .GetConnectionString ()
192
+ db , err := sqlx .Open ("postgres" , connStr )
193
+ if err != nil {
194
+ ctx .Done ()
195
+ }
196
+
199
197
go func () {
200
198
ticker := time .NewTicker (100 * time .Millisecond )
201
199
defer ticker .Stop ()
202
200
for {
203
- if c .endpointReady (ctx , addr ) {
201
+ if c .endpointReady (ctx , db ) {
204
202
close (readyCh )
205
203
return
206
204
}
@@ -253,11 +251,6 @@ func (c *PostgresDB) streamLogs(ctx context.Context) {
253
251
stdcopy .StdCopy (c .OutputStream , c .ErrorStream , out )
254
252
}
255
253
256
- // Address returns the address successfully used by the readiness check.
257
- func (c * PostgresDB ) Address () string {
258
- return c .address
259
- }
260
-
261
254
// HostAddress returns the host address where this PostgresDB instance is available.
262
255
func (c * PostgresDB ) HostAddress () string {
263
256
return c .hostAddress
@@ -303,3 +296,12 @@ func (c *PostgresDB) Stop() error {
303
296
304
297
return nil
305
298
}
299
+
300
+ // GetConnectionString returns the sql connection string for connecting to the DB
301
+ func (c * PostgresDB ) GetConnectionString () (string , error ) {
302
+ if c .HostIP != "" && c .HostPort != 0 {
303
+ return fmt .Sprintf ("host=%s port=%d user=postgres dbname=postgres sslmode=disable" ,
304
+ c .HostIP , c .HostPort ), nil
305
+ }
306
+ return "" , fmt .Errorf ("DB not initialized" )
307
+ }
0 commit comments