Skip to content

Commit

Permalink
Ping after creating connection (#195)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrew-farries authored Nov 2, 2023
1 parent 3023c0e commit 2429a6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/roll/roll.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func New(ctx context.Context, pgURL, schema string, lockTimeoutMs int, state *st
return nil, err
}

if err := conn.PingContext(ctx); err != nil {
return nil, err
}

_, err = conn.ExecContext(ctx, "SET LOCAL pgroll.internal to 'TRUE'")
if err != nil {
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func New(ctx context.Context, pgURL, stateSchema string) (*State, error) {
return nil, err
}

if err := conn.PingContext(ctx); err != nil {
return nil, err
}

_, err = conn.ExecContext(ctx, "SET LOCAL pgroll.internal to 'TRUE'")
if err != nil {
return nil, fmt.Errorf("unable to set pgroll.internal to true: %w", err)
Expand Down

0 comments on commit 2429a6b

Please sign in to comment.