-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.go
More file actions
50 lines (37 loc) · 902 Bytes
/
postgres.go
File metadata and controls
50 lines (37 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package postgres
import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
_ "github.com/lib/pq"
"github.com/go-waitfor/waitfor"
)
const Scheme = "postgres"
type Postgres struct {
url *url.URL
}
func Use() waitfor.ResourceConfig {
return waitfor.ResourceConfig{
Scheme: []string{Scheme},
Factory: New,
}
}
func New(u *url.URL) (waitfor.Resource, error) {
if u == nil {
return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument)
}
return &Postgres{u}, nil
}
func (s *Postgres) Test(ctx context.Context) error {
// Always use "postgres" as the driver name for sql.Open, regardless of the URL scheme
// Remove the scheme and "://" from the URL to get the connection string
connStr := strings.TrimPrefix(s.url.String(), s.url.Scheme+"://")
db, err := sql.Open(Scheme, connStr)
if err != nil {
return err
}
defer db.Close()
return db.PingContext(ctx)
}