-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnections.go
271 lines (225 loc) · 5.95 KB
/
connections.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package sql
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"github.com/gocql/gocql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/cassandra"
"github.com/golang-migrate/migrate/v4/database/mysql"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
cqlreflectx "github.com/scylladb/go-reflectx"
"github.com/scylladb/gocqlx/v2"
)
// TODO: Use master v4 version
// Current one is out of date and not supported
var dbs = &dbConnections{
m: map[string]*DB{},
}
type dbConnections struct {
sync.Mutex
m map[string]*DB
}
func deleteDB(db string) {
dbs.Lock()
defer dbs.Unlock()
delete(dbs.m, db)
}
func (dbc *dbConnections) GetSQLConnection(o *DB) error {
dbc.Lock()
defer dbc.Unlock()
dbSource, err := o.getDataSource()
if err != nil {
return err
}
o.Debugf("source %s: %s", o.DBSource, dbSource)
// Check if the connection exists
if val, ok := dbc.m[dbSource]; ok {
*o = *val
return val.err
}
var db *sqlx.DB
// Try to open a connection if it doesn't exist
db, o.err = sqlx.Open(o.DBSource.String(), dbSource)
if o.err == nil {
// Convert sql to sqlx
o.sql = db
o.sql.Mapper = reflectx.NewMapperTagFunc(
"json",
preMapFunc(o.mapFunc),
preMapFunc(o.tagMapFunc),
)
}
// Add it to the pool so that some other service can reference it
dbc.m[dbSource] = o
// Run migrations
if o.MigratePath != "" && o.Migrate {
if err := RunMigrations(o); err != nil {
return err
}
}
return err
}
// CQL connection currently does not support query string arguments
func (dbc *dbConnections) GetCQLConnection(o *DB) error {
dbc.Lock()
defer dbc.Unlock()
// Check if the connection exists
dbSource, err := o.getDataSource()
if err != nil {
return err
}
if val, ok := dbc.m[dbSource]; ok {
*o = *val
return val.err
}
cluster := gocql.NewCluster(o.Hosts...)
if o.Timeout != 0 {
cluster.Timeout = o.Timeout
}
o.Debugf("cql timeout %s", cluster.Timeout)
if o.ConnectTimeout != 0 {
cluster.ConnectTimeout = o.ConnectTimeout
}
o.Debugf("cql connection timeout %s", cluster.ConnectTimeout)
cluster.Port, err = strconv.Atoi(o.Port)
if err != nil {
return fmt.Errorf("atoi: %w", err)
}
if o.DisableInitialHostLookup {
o.Debugf("disable initial host lookup")
cluster.DisableInitialHostLookup = true
}
// Authentication
if o.Authenticator != nil {
cluster.Authenticator = o.Authenticator
} else {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: o.User,
Password: o.Password,
}
}
cluster.ProtoVersion = 3
// SSL
if o.CaPath != "" {
cluster.SslOpts = &gocql.SslOptions{
CaPath: o.CaPath,
}
}
// Consistency
cluster.Consistency = o.Consistency
// Create keyspace on migration, it should fail if we try to connect to an unmigrated db
if o.Migrate && o.AppEnv == development {
o.Debugf("creating keyspace name")
ts, err := cluster.CreateSession()
if err != nil {
return fmt.Errorf("create session: %v", err)
}
if err := ts.Query(CreateListingsDevKeyspaceStmt(o.DBName)).Exec(); err != nil {
return err
}
}
cluster.Keyspace = o.DBName
ts, err := cluster.CreateSession()
if err != nil {
return fmt.Errorf("create session: %v", err)
}
// Wrap session on creation, gocqlx session embeds gocql.Session pointer.
session := gocqlx.NewSession(ts)
session.Mapper = cqlreflectx.NewMapperTagFunc("json", preMapFunc(o.mapFunc), preMapFunc(o.tagMapFunc))
o.cql = &session
// Add it to the pool so that some other service can reference it
dbc.m[dbSource] = o
// Run migrations
if o.MigratePath != "" && o.Migrate {
o.Debugf("running migrations")
if err := RunMigrations(o); err != nil {
return err
}
}
return err
}
func CreateListingsDevKeyspaceStmt(keyspace string) string {
return `CREATE KEYSPACE IF NOT EXISTS ` + keyspace + ` WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : '1'}`
}
func RunMigrations(o *DB) error {
var driver database.Driver
var err error
switch o.DBSource {
case DBSource_sqlite:
driver, err = sqlite.WithInstance(o.sql.DB, &sqlite.Config{
DatabaseName: o.DBName,
})
if err != nil {
return fmt.Errorf("sqlite instance: %w", err)
}
case DBSource_postgres:
driver, err = postgres.WithInstance(o.sql.DB, &postgres.Config{
DatabaseName: o.DBName,
})
if err != nil {
return fmt.Errorf("postgres instance: %w", err)
}
case DBSource_mysql:
driver, err = mysql.WithInstance(o.sql.DB, &mysql.Config{
DatabaseName: o.DBName,
})
if err != nil {
return fmt.Errorf("mysql instance: %w", err)
}
case DBSource_cql:
driver, err = cassandra.WithInstance(o.cql.Session, &cassandra.Config{
// CQL connection currently does not support query string arguments
// Manually override the multi statments flag
MultiStatementEnabled: true,
KeyspaceName: o.DBName,
})
if err != nil {
return fmt.Errorf("cql instance: %w", err)
}
default:
return errors.New("db driver not supported")
}
var m *migrate.Migrate
if o.MigrateFS != nil {
source, err := iofs.New(o.MigrateFS, o.MigratePath)
if err != nil {
return fmt.Errorf("new fs: %w", err)
}
m, err = migrate.NewWithInstance("iofs", source, o.DBName, driver)
if err != nil {
return fmt.Errorf("fs instance: %w", err)
}
} else {
m, err = migrate.NewWithDatabaseInstance(
o.GetMigratePath(),
o.DBName,
driver,
)
if err != nil {
return fmt.Errorf("migrations instance: %v", err)
}
}
if err := m.Up(); err != nil {
if !errors.Is(err, migrate.ErrNoChange) {
return fmt.Errorf("migrations up: %v", err)
}
o.Debugf("migrate up: %v", err)
}
o.Debugf("migrate up: success")
return nil
}
func preMapFunc(f func(string) string) func(string) string {
return func(s string) string {
ss := strings.Split(s, ",")
out := f(ss[0])
return out
}
}