-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdata.go
86 lines (73 loc) · 1.85 KB
/
data.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
package main
import (
"fmt"
"log"
"strings"
"time"
)
// DataDiff ...
func DataDiff(srcDB, dstDB *DB, conf *Config) bool {
var isUpdate bool
out := &Output{}
if err := out.Init(conf.Output); err != nil {
log.Fatal(err, conf.Output)
}
defer func() {
out.Close(isUpdate)
}()
out.Printf("-- Create Date : %s\n", time.Now())
// diff data
// get table list.
tableList, err := srcDB.GetObjectList(TABLE, conf.Include, conf.Exclude)
if err != nil {
log.Fatal(err)
}
// check target db.
for _, t := range tableList {
// log.Printf("compare table.....%s", t)
// check target db.
srcTable := srcDB.GetTableInfo(t)
if srcTable == nil {
log.Fatal("load table info error", err)
}
// check primary key.
if _, ok := srcTable.Indexs["PRIMARY"]; !ok {
log.Println("not exist primary key", t)
continue
}
dstTable := dstDB.GetTableInfo(t)
if srcTable == nil {
log.Fatal("load table info error", err)
}
if dd := srcTable.compare(dstTable); dd != "" {
log.Fatal("table desc is different!", t, dd)
}
query := fmt.Sprintf("SELECT * FROM %s", t)
// load src db rows
srcData, err := srcDB.GetData(query, srcTable.Cols)
if err != nil {
log.Fatal(err)
}
// load dst db rows
dstData, err := dstDB.GetData(query, dstTable.Cols)
if err != nil {
log.Fatal(err)
}
// Compare data...
result := CompareRows(srcData, dstData, srcTable.GetPrimaryKey(), strings.Split(conf.IgnoreColumn, ","))
if len(result) > 0 {
out.Printf("\n-- ----------------------------------------- --\n")
out.Printf("-- GENERATE UPDATE TABLE DATA %s\n", t)
out.Printf("-- ----------------------------------------- --\n")
}
for _, r := range result {
out.Println(r.GenerateSQL(t))
isUpdate = true
}
log.Printf("compare table %s ... count=%d\n", t, len(result))
}
if isUpdate {
out.Printf("\n\nCOMMIT;")
}
return isUpdate
}