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 https://github.com/xataio/pgroll/issues/133
This commit is contained in:
Andrew Farries 2023-11-02 10:48:07 +00:00 committed by GitHub
parent 3023c0e365
commit 2429a6be6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -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)

View File

@ -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)