forked from cloudquery/cloudquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges.go
More file actions
258 lines (232 loc) · 7.21 KB
/
changes.go
File metadata and controls
258 lines (232 loc) · 7.21 KB
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
package changes
import (
"fmt"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
var (
columnRegex = regexp.MustCompile(`^\|(?P<name>.*)\|(?P<dataType>.*)\|`)
pkRegex = regexp.MustCompile(`^The composite primary key for this table is \(([^)]+)\)\.`)
)
type change struct {
Text string `json:"text"`
Breaking bool `json:"breaking"`
}
type columnType int
const (
columnTypePK columnType = 1 << iota
columnTypeIncremental
)
type column struct {
dataType string
columnType columnType
}
func (c column) pk() bool {
return c.columnType&columnTypePK != 0
}
func (c column) incremental() bool {
return c.columnType&columnTypeIncremental != 0
}
func backtickStrings(strings ...string) []interface{} {
backticked := make([]interface{}, len(strings))
for i, s := range strings {
backticked[i] = fmt.Sprintf("`%s`", s)
}
return backticked
}
func parseColumnChange(line string) (name string, dataType string, columnType columnType) {
match := columnRegex.FindStringSubmatch(line)
if match == nil {
return "", "", columnType
}
result := make(map[string]string)
for i, name := range columnRegex.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
if strings.Contains(result["name"], " (PK)") {
columnType |= columnTypePK
}
if strings.Contains(result["name"], " (Incremental Key)") {
columnType |= columnTypeIncremental
}
cleanName := strings.Split(result["name"], " (")[0]
return cleanName, result["dataType"], columnType
}
func parsePKChange(line string) (names []string) {
match := pkRegex.FindStringSubmatch(line)
if len(match) != 2 {
return nil
}
for _, part := range strings.Split(match[1], ", ") {
names = append(names, strings.Trim(part, "*"))
}
return
}
func getColumnChanges(file *gitdiff.File, table string) (changes []change) {
addedColumns := make(map[string]column)
deletedColumns := make(map[string]column)
var addedPK, deletedPK []string
for _, fragment := range file.TextFragments {
for _, line := range fragment.Lines {
pkChanges := parsePKChange(line.Line)
if len(pkChanges) > 0 {
switch line.Op {
case gitdiff.OpAdd:
addedPK = pkChanges
case gitdiff.OpDelete:
deletedPK = pkChanges
}
continue
}
name, dataType, columnType := parseColumnChange(line.Line)
if name == "" || dataType == "" {
continue
}
column := column{dataType: dataType, columnType: columnType}
switch line.Op {
case gitdiff.OpAdd:
addedColumns[name] = column
case gitdiff.OpDelete:
deletedColumns[name] = column
}
}
}
for deletedName, deletedColumn := range deletedColumns {
if addedColumn, ok := addedColumns[deletedName]; ok {
if deletedColumn.dataType == addedColumn.dataType && deletedColumn.columnType == addedColumn.columnType {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column order changed for %s", backtickStrings(table, deletedName)...),
Breaking: false,
})
continue
}
if addedColumn.dataType != deletedColumn.dataType {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column type changed from %s to %s for %s", backtickStrings(table, deletedColumn.dataType, addedColumn.dataType, deletedName)...),
Breaking: true,
})
}
if addedColumn.pk() && !deletedColumn.pk() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: primary key constraint added to column %s", backtickStrings(table, deletedName)...),
Breaking: true,
})
}
if !addedColumn.pk() && deletedColumn.pk() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: primary key constraint removed from column %s", backtickStrings(table, deletedName)...),
Breaking: true,
})
}
if addedColumn.incremental() && !deletedColumn.incremental() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s added to cursor for incremental syncs", backtickStrings(table, deletedName)...),
Breaking: true,
})
}
if !addedColumn.incremental() && deletedColumn.incremental() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s removed from cursor for incremental syncs", backtickStrings(table, deletedName)...),
Breaking: true,
})
}
} else {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s removed from table", backtickStrings(table, deletedName)...),
Breaking: true,
})
}
}
for addedName, addedColumn := range addedColumns {
if _, ok := deletedColumns[addedName]; !ok {
name := addedName
if addedColumn.pk() {
name += " (PK)"
}
if addedColumn.incremental() {
name += " (Incremental Key)"
}
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column added with name %s and type %s", backtickStrings(table, name, addedColumn.dataType)...),
Breaking: addedColumn.pk(),
})
}
}
// check PK:
// Only if all of the Columns are the same before and after the change should
// we consider this a "primary key order" change
ordering := func(a, b string) bool { return a < b }
diff := cmp.Diff(addedPK, deletedPK, cmpopts.SortSlices(ordering))
if len(addedPK) > 0 && diff == "" {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: primary key order changed from %s to %s",
backtickStrings(
table,
strings.Join(deletedPK, ", "),
strings.Join(addedPK, ", "),
)...,
),
Breaking: true,
})
}
sort.SliceStable(changes, func(i, j int) bool {
chI := changes[i]
chJ := changes[j]
switch {
case chI.Breaking && !chJ.Breaking:
return true
case !chI.Breaking && chJ.Breaking:
return false
default:
return chI.Text < chJ.Text
}
})
return changes
}
func getFileChanges(file *gitdiff.File) (changes []change, err error) {
oldTableName := strings.TrimSuffix(filepath.Base(file.OldName), filepath.Ext(file.OldName))
newTableName := strings.TrimSuffix(filepath.Base(file.NewName), filepath.Ext(file.NewName))
switch {
case file.IsDelete:
changes = append(changes, change{
Text: fmt.Sprintf("Table %s was removed", backtickStrings(oldTableName)...),
Breaking: true,
})
case file.IsRename && oldTableName != newTableName:
changes = append(changes, change{
Text: fmt.Sprintf("Table %s was renamed to %s", backtickStrings(oldTableName, newTableName)...),
Breaking: true,
})
case file.IsNew:
changes = append(changes, change{
Text: fmt.Sprintf("Table %s was added", backtickStrings(newTableName)...),
Breaking: false,
})
case file.IsCopy:
return nil, fmt.Errorf("unhandled IsCopy table diff, %s -> %s", backtickStrings(oldTableName, newTableName)...)
}
checkColumnChanges := !file.IsDelete && !file.IsNew
// Don't report column changes for deleted or new tables to avoid noise
if checkColumnChanges {
changes = append(changes, getColumnChanges(file, newTableName)...)
}
return changes, nil
}
func GetChanges(files []*gitdiff.File) (changes []change, err error) {
changes = make([]change, 0)
for _, file := range files {
fileChanges, err := getFileChanges(file)
if err != nil {
return nil, err
}
changes = append(changes, fileChanges...)
}
return changes, nil
}