-
Notifications
You must be signed in to change notification settings - Fork 230
/
utils.go
38 lines (33 loc) · 835 Bytes
/
utils.go
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
package sqldb
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5"
meta "encr.dev/proto/encore/parser/meta/v1"
)
// WaitForConn waits for a successful connection to uri to be established.
func WaitForConn(ctx context.Context, uri string) error {
var err error
for i := 0; i < 40; i++ {
var conn *pgx.Conn
conn, err = pgx.Connect(ctx, uri)
if err == nil {
err = conn.Ping(ctx)
_ = conn.Close(ctx)
if err == nil {
return nil
}
} else if ctx.Err() != nil {
// We'll never succeed once the context has been canceled.
// Give up straight away.
break
}
time.Sleep(250 * time.Millisecond)
}
return fmt.Errorf("database did not come up: %v", err)
}
// IsUsed reports whether the application uses SQL databases at all.
func IsUsed(md *meta.Data) bool {
return len(md.SqlDatabases) > 0
}