-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.go
114 lines (96 loc) · 2.63 KB
/
db.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package db
import (
"database/sql"
"fmt"
"os"
"time"
migraterice "github.com/atrox/go-migrate-rice"
mysqldriver "github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
"github.com/prometheus/client_golang/prometheus"
)
// Connect returns the database handler which is safe for concurrent access.
func Connect(ds string) (db *sql.DB, err error) {
config, err := mysqldriver.ParseDSN(ds)
if err != nil {
return nil, fmt.Errorf("error parsing dsn: %w (%s)", err, ds)
}
config.Collation = "utf8mb4_unicode_ci"
config.Loc = time.UTC
config.ParseTime = true
config.MultiStatements = true
config.Params = map[string]string{
"time_zone": "UTC",
}
conn, err := mysqldriver.NewConnector(config)
if err != nil {
return nil, fmt.Errorf("error creating connector: %w", err)
}
db = sql.OpenDB(conn)
// Set reasonable sizes on the built-in pool.
db.SetMaxOpenConns(30)
db.SetMaxIdleConns(30)
db.SetConnMaxLifetime(time.Minute)
// Register Prometheus collector.
c := prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "src",
Subsystem: "mysql_app",
Name: "open_connections",
Help: "Number of open connections to MySQL DB, as reported by mysql.DB.Stats()",
},
func() float64 {
s := db.Stats()
return float64(s.OpenConnections)
},
)
prometheus.MustRegister(c)
// Database migration.
m, err := newMigrate(db)
if err != nil {
return nil, fmt.Errorf("error creating migrate object: %v", err)
}
if err := doMigrate(m); err != nil {
return nil, fmt.Errorf("error during database migration: %v", err)
}
return db, nil
}
func newMigrate(db *sql.DB) (*migrate.Migrate, error) {
driver, err := mysql.WithInstance(db, &mysql.Config{})
if err != nil {
return nil, fmt.Errorf("error creating migrate driver: %w", err)
}
box, err := migrations()
if err != nil {
return nil, fmt.Errorf("error opening embedded migrations: %w", err)
}
sourceDriver, err := migraterice.WithInstance(box)
if err != nil {
return nil, fmt.Errorf("error creating migrage source driver: %w", err)
}
m, err := migrate.NewWithInstance("go-bindata", sourceDriver, "mysql", driver)
if err != nil {
return nil, fmt.Errorf("error creating migrate instance: %w", err)
}
// Wait up to five minutes is another process is already on it.
m.LockTimeout = 5 * time.Minute
return m, nil
}
func doMigrate(m *migrate.Migrate) error {
err := m.Up()
if err == nil || err == migrate.ErrNoChange {
return nil
}
if os.IsNotExist(err) {
_, dirty, verr := m.Version()
if verr != nil {
return verr
}
if dirty {
return err
}
return nil
}
return err
}