-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
52 lines (41 loc) · 1.15 KB
/
database.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
49
50
51
52
package main
import (
"database/sql"
_ "github.com/lib/pq"
"gopkg.in/mgutz/dat.v1"
"gopkg.in/mgutz/dat.v1/sqlx-runner"
"log"
"time"
)
// global database (pooling provided by SQL driver)
var DB *runner.DB
type Today struct {
Today dat.NullTime `db:"now"`
}
func _initDB() {
// create a normal database connection through database/sql
db, err := sql.Open("postgres", Config.DataSourceName)
if err != nil {
panic(err)
}
// ensures the database can be pinged with an exponential backoff (15 min)
runner.MustPing(db)
// set to reasonable values for production
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)
// set this to enable interpolation
dat.EnableInterpolation = true
// set to check things like sessions closing.
// Should be disabled in production/release builds.
dat.Strict = false
// Log any query over 10ms as warnings. (optional)
runner.LogQueriesThreshold = 10 * time.Millisecond
DB = runner.NewDB(db, "postgres")
// DoO a test run against the DB
var _res Today
dbErr := DB.SQL("select now()").QueryStruct(&_res)
if dbErr != nil {
log.Fatalln(dbErr.Error())
}
log.Println("... Test connection to DB, today =", _res.Today)
}