-
Notifications
You must be signed in to change notification settings - Fork 4
/
differ.go
116 lines (100 loc) · 2.52 KB
/
differ.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
package dbdiffer
import (
"reflect"
)
var DriverList []string = []string{}
type Differ interface {
Close() error
Diff(prefix string) (*Result, error)
Generate(*Result) ([]string, error)
}
type Result struct {
Drop []Table
Create []Table
Change []Table
}
func (r Result) IsEmpty() bool {
return len(r.Drop) == 0 && len(r.Create) == 0 && len(r.Change) == 0
}
type ResultFields struct {
Create []Field // used for creating table
Drop []Field
Change []Field
Add []Field
}
func (f ResultFields) IsEmpty() bool {
return len(f.Create) == 0 && len(f.Drop) == 0 && len(f.Change) == 0 && len(f.Add) == 0
}
type ResultIndexes struct {
Create []Index // used for creating table
Add []Index
Drop []Index
}
func (f ResultIndexes) IsEmpty() bool {
return len(f.Create) == 0 && len(f.Drop) == 0 && len(f.Add) == 0
}
type Table struct {
Name string
Engine string
Version string
RowFormat string
Options string
Comment string
Collation string
Fields ResultFields
Indexes ResultIndexes
}
func (t Table) Equal(t2 Table) bool {
return t.Name == t2.Name &&
t.Engine == t2.Engine &&
t.Version == t2.Version &&
t.RowFormat == t2.RowFormat &&
t.Options == t2.Options &&
t.Comment == t2.Comment &&
t.Collation == t2.Collation
}
func (t Table) IsEmpty() bool {
return t.Engine == "" && t.Version == "" && t.RowFormat == "" && t.Options == "" && t.Comment == "" && t.Collation == "" &&
t.Fields.IsEmpty() && t.Indexes.IsEmpty()
}
type Field struct {
Field string
Type string
Collation *string
Null string
Key string
Default *string
Extra string
Comment string
After string
}
func (f Field) Equal(f2 Field) bool {
return f.Field == f2.Field &&
f.Type == f2.Type &&
((f.Collation == nil && f2.Collation == nil) || *f.Collation == *f2.Collation) &&
f.Null == f2.Null &&
// f.Key == f2.Key &&
((f.Default == nil && f2.Default == nil) || (f.Default != nil && f2.Default != nil && *f.Default == *f2.Default)) &&
f.Extra == f2.Extra &&
f.Comment == f2.Comment
}
type Index struct {
Table string
NonUnique int
KeyName string
ColumnName []string
Collation string
IndexType string
Comment string
IndexComment string
}
func (i Index) Equal(i2 Index) bool {
return i.Table == i2.Table &&
i.NonUnique == i2.NonUnique &&
i.KeyName == i2.KeyName &&
reflect.DeepEqual(i.ColumnName, i2.ColumnName) &&
i.Collation == i2.Collation &&
i.IndexType == i2.IndexType &&
i.Comment == i2.Comment &&
i.IndexComment == i2.IndexComment
}