forked from polothy/pg2mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
71 lines (59 loc) · 1.59 KB
/
validator.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
package pg2mysql
import (
"fmt"
)
type Validator interface {
Validate() ([]ValidationResult, error)
}
func NewValidator(src, dst DB) Validator {
return &validator{
src: src,
dst: dst,
}
}
type validator struct {
src, dst DB
}
func (v *validator) Validate() ([]ValidationResult, error) {
srcSchema, err := BuildSchema(v.src)
if err != nil {
return nil, fmt.Errorf("failed to build source schema: %s", err)
}
dstSchema, err := BuildSchema(v.dst)
if err != nil {
return nil, fmt.Errorf("failed to build destination schema: %s", err)
}
var results []ValidationResult
for _, srcTable := range srcSchema.Tables {
dstTable, err := dstSchema.GetTable(srcTable.Name)
if err != nil {
return nil, fmt.Errorf("failed to get table from destination schema: %s", err)
}
if srcTable.HasColumn("id") {
rowIDs, err := GetIncompatibleRowIDs(v.src, srcTable, dstTable)
if err != nil {
return nil, fmt.Errorf("failed getting incompatible row ids: %s", err)
}
results = append(results, ValidationResult{
TableName: srcTable.Name,
IncompatibleRowIDs: rowIDs,
IncompatibleRowCount: int64(len(rowIDs)),
})
} else {
rowCount, err := GetIncompatibleRowCount(v.src, srcTable, dstTable)
if err != nil {
return nil, fmt.Errorf("failed getting incompatible row count: %s", err)
}
results = append(results, ValidationResult{
TableName: srcTable.Name,
IncompatibleRowCount: rowCount,
})
}
}
return results, nil
}
type ValidationResult struct {
TableName string
IncompatibleRowIDs []int
IncompatibleRowCount int64
}