-
Notifications
You must be signed in to change notification settings - Fork 13
/
db_provider_concrete.go
152 lines (126 loc) · 3.47 KB
/
db_provider_concrete.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package store
import (
"fmt"
"github.com/BurntSushi/migration"
"github.com/cloudfoundry/bosh-utils/errors"
// blank import to load database drivers
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"os"
"os/signal"
"syscall"
"github.com/cloudfoundry/config-server/config"
"github.com/cloudfoundry/config-server/store/db_migrations"
)
type concreteDbProvider struct {
config config.DBConfig //nolint:unused
sql ISql //nolint:unused
db IDb
}
func NewConcreteDbProvider(sql ISql, config config.DBConfig) (DbProvider, error) {
connectionString, err := connectionString(config)
if err != nil {
return nil, errors.WrapError(err, "Failed to generate DB connection string")
}
versionGet := getVersionImpl(config)
versionSet := setVersionImpl(config)
db, err := sql.OpenWith(
config.Adapter,
connectionString,
db_migrations.GetMigrations(config.Adapter),
versionGet,
versionSet,
)
if err != nil {
return nil, errors.WrapError(err, "Failed to open connection to DB")
}
go closeDBOnSignal(db)
db.SetMaxOpenConns(config.ConnectionOptions.MaxOpenConnections)
db.SetMaxIdleConns(config.ConnectionOptions.MaxIdleConnections)
provider := concreteDbProvider{db: db}
return provider, err
}
func (p concreteDbProvider) Db() (IDb, error) {
if p.db == nil {
return nil, errors.Error("Database not initialized")
}
return p.db, nil
}
func connectionString(config config.DBConfig) (string, error) {
var connectionString string
var err error
switch config.Adapter {
case "postgres":
connectionString = fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
config.User, config.Password, config.Name)
case "mysql":
connectionString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s",
config.User, config.Password, config.Host, config.Port, config.Name)
default:
err = errors.Errorf("Unsupported adapter: %s", config.Adapter)
}
return connectionString, err
}
func getVersionImpl(config config.DBConfig) migration.GetVersion {
switch config.Adapter {
case "mysql":
return MysqlGetVersion
default:
return migration.DefaultGetVersion
}
}
func setVersionImpl(config config.DBConfig) migration.SetVersion {
switch config.Adapter {
case "mysql":
return MysqlSetVersion
default:
return migration.DefaultSetVersion
}
}
func closeDBOnSignal(db IDb) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
fmt.Printf("Shutting down DB connection")
db.Close()
os.Exit(1)
}
func MysqlGetVersion(tx migration.LimitedTx) (int, error) {
v, err := getVersion(tx)
if err != nil {
if err := createVersionTable(tx); err != nil {
return 0, err
}
return getVersion(tx)
}
return v, nil
}
func MysqlSetVersion(tx migration.LimitedTx, version int) error {
if err := setVersion(tx, version); err != nil {
if err := createVersionTable(tx); err != nil {
return err
}
return setVersion(tx, version)
}
return nil
}
func getVersion(tx migration.LimitedTx) (int, error) {
var version int
r := tx.QueryRow("SELECT version FROM migration_version")
if err := r.Scan(&version); err != nil {
return 0, err
}
return version, nil
}
func setVersion(tx migration.LimitedTx, version int) error {
_, err := tx.Exec("UPDATE migration_version SET version = ?", version)
return err
}
func createVersionTable(tx migration.LimitedTx) error {
_, err := tx.Exec(`CREATE TABLE migration_version ( version INTEGER);`)
if err != nil {
return err
}
_, err = tx.Exec(`INSERT INTO migration_version (version) VALUES (0)`)
return err
}