-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
48 lines (42 loc) · 1.12 KB
/
sql.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
39
40
41
42
43
44
45
46
47
48
package healthcheck
import (
"database/sql"
"context"
)
// SQL is a type that fills the Checker interface
// for an *sql.DB. ID is used as the identifying
// information returned by LogInfo, which is useful
// if more than one *sql.DB is needed for a service's
// health check.
type SQL struct {
DB *sql.DB
ID string
}
// NewSQL returns an SQL instance using the passed
// *sql.DB and identifier.
func NewSQL(db *sql.DB, id string) SQL {
return SQL{
DB: db,
ID: id,
}
}
// Check verifies that a connection to the database
// can be obtained, using the Ping method of the
// underlying *sql.DB. It then makes sure the database
// is still reachable, by sending a simple query.
func (s SQL) Check(ctx context.Context) error {
err := s.DB.Ping()
if err != nil {
return err
}
var one int
err = s.DB.QueryRow("SELECT 1;").Scan(&one)
return err
}
// LogInfo returns the ID string associated with the
// SQL it is called on. It should usually be set to a
// connection string, a database name, or anything else
// that would be useful to have in the log output.
func (s SQL) LogInfo(ctx context.Context) string {
return s.ID
}