Skip to content

Commit 2429a6b

Browse files
Ping after creating connection (#195)
Ping the database after establishing a connection for better error messages. Without this change, an error establishing a connection would be prefixed with the message from the next failure. **before**: ``` Error: unable to set pgroll.internal to true: dial tcp [::1]:7432: connect: connection refused ``` **after**: ``` Error: dial tcp [::1]:7432: connect: connection refused ``` Fixes #133
1 parent 3023c0e commit 2429a6b

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pkg/roll/roll.go

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func New(ctx context.Context, pgURL, schema string, lockTimeoutMs int, state *st
3939
return nil, err
4040
}
4141

42+
if err := conn.PingContext(ctx); err != nil {
43+
return nil, err
44+
}
45+
4246
_, err = conn.ExecContext(ctx, "SET LOCAL pgroll.internal to 'TRUE'")
4347
if err != nil {
4448
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)

pkg/state/state.go

+4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ func New(ctx context.Context, pgURL, stateSchema string) (*State, error) {
225225
return nil, err
226226
}
227227

228+
if err := conn.PingContext(ctx); err != nil {
229+
return nil, err
230+
}
231+
228232
_, err = conn.ExecContext(ctx, "SET LOCAL pgroll.internal to 'TRUE'")
229233
if err != nil {
230234
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)

0 commit comments

Comments
 (0)