-
Notifications
You must be signed in to change notification settings - Fork 173
/
db.go
637 lines (550 loc) · 15.3 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package utils
import (
"database/sql"
"fmt"
"math"
"math/rand"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/moiot/gravity/pkg/config"
_ "github.com/go-sql-driver/mysql"
"github.com/juju/errors"
gomysql "github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/replication"
log "github.com/sirupsen/logrus"
)
func getIndexRowsName(db *sql.DB, statement string) ([]string, error) {
resultRows, err := GetIndexRows(db, statement)
if err != nil {
return nil, errors.Trace(err)
}
var result []string
for _, r := range resultRows {
result = append(result, r[6].String)
}
return result, nil
}
func IsTableEmpty(db *sql.DB, schema string, table string, condition string) bool {
where := ""
if condition != "" {
where = "WHERE " + condition
}
r := db.QueryRow(fmt.Sprintf("select count(*) from (select * from `%s`.`%s` %s limit 1)t", schema, table, where))
var cnt int
if err := r.Scan(&cnt); err != nil {
log.Fatalf("error IsTableEmpty, schema %s, table %s, err %s", schema, table, errors.Trace(err))
}
return cnt == 0
}
func GetIndexRows(db *sql.DB, statement string) ([][]sql.NullString, error) {
rows, err := db.Query(statement)
defer func() {
if rows != nil {
rows.Close()
}
}()
if err != nil {
return nil, errors.Trace(err)
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, errors.Trace(err)
}
/*
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: _gravity
CONSTRAINT_NAME: PRIMARY
TABLE_CATALOG: def
TABLE_SCHEMA: _gravity
TABLE_NAME: _gravity_txn_tags
COLUMN_NAME: id
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*/
var resultRows [][]sql.NullString
for rows.Next() {
values := make([]sql.NullString, len(columns))
scanArgs := make([]interface{}, len(columns))
for i := range values {
scanArgs[i] = &values[i]
}
if err := rows.Scan(scanArgs...); err != nil {
return nil, errors.Trace(err)
}
resultRows = append(resultRows, values)
}
return resultRows, nil
}
func GetPrimaryKeys(db *sql.DB, schemaName string, tableName string) ([]string, error) {
statement := fmt.Sprintf("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s' AND CONSTRAINT_NAME = 'PRIMARY' ORDER BY ORDINAL_POSITION", schemaName, tableName)
return getIndexRowsName(db, statement)
}
func GetUniqueIndexesWithoutPks(db *sql.DB, schemaName string, tableName string) ([]string, error) {
statement := fmt.Sprintf("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s' AND CONSTRAINT_NAME != 'PRIMARY' AND POSITION_IN_UNIQUE_CONSTRAINT IS NULL ORDER BY ORDINAL_POSITION", schemaName, tableName)
return getIndexRowsName(db, statement)
}
func EstimateRowsCount(db *sql.DB, schemaName string, tableName string) (int64, error) {
statement := fmt.Sprintf("SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", schemaName, tableName)
var rowsCount sql.NullInt64
row := db.QueryRow(statement)
if err := row.Scan(&rowsCount); err != nil {
return 0, errors.Trace(err)
}
if !rowsCount.Valid {
return 0, errors.Errorf("TABLE_ROWS null")
}
return rowsCount.Int64, nil
}
//
// We need to look at this file to check each type
// https://github.com/go-sql-driver/mysql/blob/master/fields.go
//
func IsColumnString(columnType *sql.ColumnType) bool {
typeName := columnType.DatabaseTypeName()
return strings.Contains(typeName, "TEXT") ||
strings.Contains(typeName, "CHAR") ||
strings.Contains(typeName, "JSON")
}
// float, double is already handled
// https://github.com/go-sql-driver/mysql/blob/master/fields.go#L166
func IsColumnFloat(columnType *sql.ColumnType) bool {
typeName := columnType.DatabaseTypeName()
return strings.Contains(typeName, "DECIMAL")
}
var nullString = reflect.TypeOf(sql.NullString{})
// GetScanType returns better scan type than go-sql-driver/mysql
func GetScanType(columnType *sql.ColumnType) reflect.Type {
if IsColumnString(columnType) {
return reflect.TypeOf(sql.NullString{})
} else if IsColumnFloat(columnType) {
return reflect.TypeOf(sql.NullFloat64{})
} else {
return columnType.ScanType()
}
}
func GetScanPtrSafe(columnIdx int, columnTypes []*sql.ColumnType, vPtrs []interface{}) (interface{}, error) {
scanType := GetScanType(columnTypes[columnIdx])
if scanType.String() == "sql.RawBytes" {
data := reflect.ValueOf(vPtrs[columnIdx]).Elem().Interface()
dataRawBytes, ok := data.(sql.RawBytes)
if !ok {
return nil, errors.Errorf("[GetScanPtrSafe] failed to convert sql.RawBytes")
}
var b sql.RawBytes
if dataRawBytes != nil {
b = make(sql.RawBytes, len(dataRawBytes))
copy(b, dataRawBytes)
}
return &b, nil
} else {
return vPtrs[columnIdx], nil
}
}
func ScanGeneralRowsWithDataPtrs(rows *sql.Rows, columnTypes []*sql.ColumnType, vPtrs []interface{}) ([]interface{}, error) {
if err := rows.Scan(vPtrs...); err != nil {
return nil, errors.Trace(err)
}
// copy sql.RawBytes from db to here
for i := range columnTypes {
p, err := GetScanPtrSafe(i, columnTypes, vPtrs)
if err != nil {
return nil, errors.Trace(err)
}
vPtrs[i] = p
}
return vPtrs, nil
}
// func ScanGeneraRowWithDataPtrs(row *sql.Row, columnTypes []*sql.ColumnType, vPtrs []interface{}) ([]interface{}, bool, error) {
// if err := row.Scan(vPtrs...); err != nil {
// if err == sql.ErrNoRows {
// return nil, false, nil
// }
// return nil, false, errors.Trace(err)
// }
//
// // copy sql.RawBytes from db to here
// for i, _ := range columnTypes {
// p, err := GetScanPtrSafe(i, columnTypes, vPtrs)
// if err != nil {
// return nil, true, errors.Trace(err)
// }
// vPtrs[i] = p
// }
// return vPtrs, true, nil
// }
func ScanGeneralRows(rows *sql.Rows, columnTypes []*sql.ColumnType) ([]interface{}, error) {
vPtrs := make([]interface{}, len(columnTypes))
for i := range columnTypes {
scanType := GetScanType(columnTypes[i])
vptr := reflect.New(scanType)
vPtrs[i] = vptr.Interface()
}
if err := rows.Scan(vPtrs...); err != nil {
return nil, errors.Trace(err)
}
// copy sql.RawBytes from db to here
for i := range columnTypes {
p, err := GetScanPtrSafe(i, columnTypes, vPtrs)
if err != nil {
return nil, errors.Trace(err)
}
vPtrs[i] = p
}
return vPtrs, nil
}
func QueryGeneralRowsDataWithSQL(db *sql.DB, statement string, args ...interface{}) ([][]interface{}, error) {
rows, err := db.Query(statement, args...)
if err != nil {
return nil, errors.Trace(err)
}
defer func() {
err = rows.Close()
if err != nil {
log.Fatalf("close error")
}
err = rows.Err()
if err != nil {
log.Fatalf("rows error")
}
}()
columnTypes, err := rows.ColumnTypes()
if err != nil {
return nil, errors.Trace(err)
}
var rowsPtr [][]interface{}
for rows.Next() {
vPtrs, err := ScanGeneralRows(rows, columnTypes)
if err != nil {
return nil, errors.Annotatef(err, "stmt: %s. args: %s", statement, args)
}
rowsPtr = append(rowsPtr, vPtrs)
}
return rowsPtr, nil
}
func CreateDBConnection(cfg *config.DBConfig) (*sql.DB, error) {
if err := cfg.ValidateAndSetDefault(); err != nil {
return nil, errors.Trace(err)
}
dbDSN := fmt.Sprintf(`%s:%s@tcp(%s:%d)/%s?interpolateParams=true&timeout=%s&readTimeout=%s&writeTimeout=%s&parseTime=true&collation=utf8mb4_general_ci`,
cfg.Username, cfg.Password, cfg.Host, cfg.Port, url.QueryEscape(cfg.Schema), cfg.Timeout, cfg.ReadTimeout, cfg.WriteTimeout)
if cfg.Location != "" {
dbDSN += "&loc=" + url.QueryEscape(cfg.Location)
}
log.Infof("DSN is %s", dbDSN)
db, err := sql.Open("mysql", dbDSN)
if err != nil {
return nil, errors.Trace(err)
}
err = db.Ping()
if err != nil {
return nil, errors.Trace(err)
}
db.SetMaxOpenConns(cfg.MaxOpen)
db.SetMaxIdleConns(cfg.MaxIdle)
db.SetConnMaxLifetime(cfg.MaxLifeTimeDuration)
return db, nil
}
func CloseDBConnection(db *sql.DB) error {
if db == nil {
return nil
}
return errors.Trace(db.Close())
}
func CloseDBConnections(dbs ...*sql.DB) {
for _, db := range dbs {
if err := CloseDBConnection(db); err != nil {
log.Errorf("close db failed: %v", err)
}
}
}
func TestConfig() *config.DBConfig {
host, ok := os.LookupEnv("DB_HOST")
if !ok {
host = "localhost"
}
port, ok := os.LookupEnv("DB_PORT")
if !ok {
port = "3306"
}
user, ok := os.LookupEnv("DB_USER")
if !ok {
user = "root"
}
pwd, ok := os.LookupEnv("DB_PWD")
if !ok {
pwd = ""
}
schema, ok := os.LookupEnv("DB_SCHEMA")
if !ok {
schema = ""
}
iport, _ := strconv.Atoi(port)
return &config.DBConfig{
Host: host,
Port: iport,
Username: user,
Password: pwd,
Schema: schema,
}
}
// CheckBinlogFormat checks binlog format
func CheckBinlogFormat(db *sql.DB) error {
rows, err := db.Query(`SHOW GLOBAL VARIABLES LIKE "binlog_format";`)
if err != nil {
return errors.Trace(err)
}
defer rows.Close()
// Show an example.
/*
mysql> SHOW GLOBAL VARIABLES LIKE "binlog_format";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
*/
for rows.Next() {
var (
variable string
value string
)
err = rows.Scan(&variable, &value)
if err != nil {
return errors.Trace(err)
}
if variable == "binlog_format" && value != "ROW" {
log.Fatalf("binlog_format is not 'ROW': %v", value)
}
}
if rows.Err() != nil {
return errors.Trace(rows.Err())
}
// make sure binlog_row_image is FULL
binlogRowImageRows, err := db.Query(`SHOW GLOBAL VARIABLES LIKE 'binlog_row_image';`)
if err != nil {
return errors.Trace(err)
}
defer binlogRowImageRows.Close()
for binlogRowImageRows.Next() {
var (
variable string
value string
)
err = binlogRowImageRows.Scan(&variable, &value)
if err != nil {
return errors.Trace(err)
}
if variable == "binlog_row_image" && value != "FULL" {
log.Fatalf("binlog_row_image is not 'FULL' : %v", value)
}
}
if binlogRowImageRows.Err() != nil {
return errors.Trace(binlogRowImageRows.Err())
}
return nil
}
type MySQLStatusGetter interface {
GetMasterStatus() (gomysql.Position, gomysql.MysqlGTIDSet, error)
GetServerUUID() (string, error)
}
type MySQLDB struct {
*sql.DB
}
func NewMySQLDB(db *sql.DB) MySQLDB {
return MySQLDB{db}
}
func (db MySQLDB) GetMasterStatus() (gomysql.Position, gomysql.MysqlGTIDSet, error) {
var (
binlogPos gomysql.Position
gs gomysql.MysqlGTIDSet
)
rows, err := db.Query(`SHOW MASTER STATUS`)
if err != nil {
return binlogPos, gs, errors.Trace(err)
}
defer rows.Close()
rowColumns, err := rows.Columns()
if err != nil {
return binlogPos, gs, errors.Trace(err)
}
var (
gtid string
binlogName string
pos uint32
nullPtr interface{}
)
for rows.Next() {
if len(rowColumns) == 5 {
err = rows.Scan(&binlogName, &pos, &nullPtr, &nullPtr, >id)
} else {
err = rows.Scan(&binlogName, &pos, &nullPtr, &nullPtr)
}
if err != nil {
return binlogPos, gs, errors.Trace(err)
}
binlogPos = gomysql.Position{Name: binlogName, Pos: pos}
generalGTIDSet, err := gomysql.ParseMysqlGTIDSet(gtid)
if err != nil {
return binlogPos, gs, errors.Trace(err)
}
gs = *(generalGTIDSet.(*gomysql.MysqlGTIDSet))
}
if rows.Err() != nil {
return binlogPos, gs, errors.Trace(rows.Err())
}
return binlogPos, gs, nil
}
func (db MySQLDB) GetServerUUID() (string, error) {
var masterUUID string
rows, err := db.Query(`select @@server_uuid;`)
if err != nil {
return "", errors.Trace(err)
}
defer rows.Close()
// Show an example.
/*
MySQL [test]> SHOW MASTER STATUS;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 53ea0ed1-9bf8-11e6-8bea-64006a897c73 |
+--------------------------------------+
*/
for rows.Next() {
err = rows.Scan(&masterUUID)
if err != nil {
return "", errors.Trace(err)
}
}
if rows.Err() != nil {
return "", errors.Trace(rows.Err())
}
return masterUUID, nil
}
// CompareBinlogPos Compare binlog positions.
// The result will be 0 if |a-b| < deviation, otherwise -1 if a < b, and +1 if a > b.
func CompareBinlogPos(a, b gomysql.Position, deviation float64) int {
if a.Name < b.Name {
return -1
}
if a.Name > b.Name {
return 1
}
if math.Abs(float64(a.Pos-b.Pos)) <= deviation {
return 0
}
if a.Pos < b.Pos {
return -1
}
return 1
}
func GenerateRandomServerID() uint32 {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
for {
id := r.Uint32() % 10000000
if id > 0 {
return id
}
}
}
// GetServerTimezone get timezone of the MySQL server.
func GetServerTimezone(db *sql.DB) (string, error) {
var tz string
rows, err := db.Query(`SELECT timediff(now(), UTC_TIMESTAMP);`)
if err != nil {
return "", errors.Trace(err)
}
defer rows.Close()
// Show server timezone.
/*
MySQL [test]> select timediff(now(), UTC_TIMESTAMP);
+--------------------------------------+
| time_zone |
+--------------------------------------+
| 08:00:00 |
+--------------------------------------+
*/
for rows.Next() {
err = rows.Scan(&tz)
if err != nil {
return "", errors.Trace(err)
}
}
if rows.Err() != nil {
return "", errors.Trace(rows.Err())
}
return tz, nil
}
func IsBinlogPurgedError(err error) bool {
mysqlErr, ok := errors.Cause(err).(*gomysql.MyError)
if !ok {
return false
}
errCode := uint16(mysqlErr.Code)
if errCode == gomysql.ER_MASTER_FATAL_ERROR_READING_BINLOG {
return true
}
return false
}
func TableIdentity(schemaName string, tableName string) string {
return fmt.Sprintf("`%s`.`%s`", schemaName, tableName)
}
func GetTables(db *sql.DB) ([]string, error) {
rows, err := db.Query("SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA NOT IN ('mysql','performance_schema')")
if err != nil {
return nil, errors.Annotatef(err, "error GetTables")
}
defer rows.Close()
var tableIdentities []string
for rows.Next() {
var schema string
var table string
err = rows.Scan(&schema, &table)
if err != nil {
return nil, errors.Annotatef(err, "error GetTables")
}
tableIdentities = append(tableIdentities, TableIdentity(schema, table))
}
err = rows.Err()
if err != nil {
return nil, errors.Annotatef(err, "error GetTables")
}
return tableIdentities, nil
}
func SQLWithAnnotation(annotation string, sql string) string {
return fmt.Sprintf("%s%s", annotation, sql)
}
func NewBinlogSyncer(serverID uint32, dbConfig *config.DBConfig, heartbeatPeriod time.Duration) *replication.BinlogSyncer {
syncerConfig := replication.BinlogSyncerConfig{
ServerID: serverID,
Flavor: "mysql",
Host: dbConfig.Host,
Port: uint16(dbConfig.Port),
User: dbConfig.Username,
Password: dbConfig.Password,
ParseTime: true,
}
if heartbeatPeriod > 0 {
syncerConfig.HeartbeatPeriod = heartbeatPeriod
}
return replication.NewBinlogSyncer(syncerConfig)
}
func IsTiDB(db *sql.DB) bool {
r := db.QueryRow("select version()")
var s string
err := r.Scan(&s)
if err != nil {
log.Panic(err)
}
return strings.Contains(strings.ToLower(s), "tidb")
}