-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathchanges.go
More file actions
283 lines (252 loc) · 7.75 KB
/
changes.go
File metadata and controls
283 lines (252 loc) · 7.75 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
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
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 \(([^)]+)\)\.`)
// There is a different message for single PKs
singlePKRegex = regexp.MustCompile(`^The 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
dataTypeRaw 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(strs ...string) []any {
backticked := make([]any, len(strs))
for i, s := range strs {
backticked[i] = "`" + s + "`"
}
return backticked
}
func parseColumnChange(line string) (name string, col column) {
match := columnRegex.FindStringSubmatch(line)
if match == nil {
return "", column{}
}
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)") {
col.columnType |= columnTypePK
}
if strings.Contains(result["name"], " (Incremental Key)") {
col.columnType |= columnTypeIncremental
}
cleanName := strings.Split(result["name"], " (")[0]
col.dataTypeRaw = result["dataType"]
col.dataType = strings.Trim(col.dataTypeRaw, "`")
return cleanName, col
}
func parsePKChange(line string) (names []string) {
matchMulti := pkRegex.FindStringSubmatch(line)
matchSingle := singlePKRegex.FindStringSubmatch(line)
if len(matchMulti) == 2 {
for _, part := range strings.Split(matchMulti[1], ", ") {
names = append(names, strings.Trim(part, "*"))
}
}
if len(matchSingle) == 2 {
for _, part := range strings.Split(matchSingle[1], ", ") {
names = append(names, strings.Trim(part, "*"))
}
}
return names
}
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, col := parseColumnChange(line.Line)
if name == "" || col.dataType == "" {
continue
}
switch line.Op {
case gitdiff.OpAdd:
addedColumns[name] = col
case gitdiff.OpDelete:
deletedColumns[name] = col
}
}
}
for name, deleted := range deletedColumns {
added, ok := addedColumns[name]
if !ok {
if name == "_cq_source_name" || name == "_cq_sync_time" {
// Ignore removal of these columns for SDK v4 migration; they are now
// owned by the CLI as an optional transformation.
continue
}
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s removed from table", backtickStrings(table, name)...),
Breaking: true,
})
continue
}
if deleted.dataType != added.dataType {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column type changed from %s to %s for %s", backtickStrings(table, deleted.dataType, added.dataType, name)...),
Breaking: true,
})
continue
}
if deleted.columnType == added.columnType {
// we ignore ordering changes
continue
}
if added.pk() && !deleted.pk() && !(len(addedPK) == 1 && addedPK[0] == "_cq_id" && len(deletedPK) > 0) {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: primary key constraint added to column %s", backtickStrings(table, name)...),
Breaking: true,
})
}
if !added.pk() && deleted.pk() && !(len(addedPK) == 1 && addedPK[0] == "_cq_id" && len(deletedPK) > 0) {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: primary key constraint removed from column %s", backtickStrings(table, name)...),
Breaking: true,
})
}
if added.incremental() && !deleted.incremental() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s added to cursor for incremental syncs", backtickStrings(table, name)...),
Breaking: true,
})
}
if !added.incremental() && deleted.incremental() {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column %s removed from cursor for incremental syncs", backtickStrings(table, name)...),
Breaking: true,
})
}
}
for name, added := range addedColumns {
if _, ok := deletedColumns[name]; ok {
continue
}
if added.pk() {
name += " (PK)"
}
if added.incremental() {
name += " (Incremental Key)"
}
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: column added with name %s and type %s", backtickStrings(table, name, added.dataType)...),
Breaking: added.pk(),
})
}
// check PK:
// Only if all 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
}
})
if len(addedPK) == 1 && addedPK[0] == "_cq_id" && len(deletedPK) > 0 {
changes = append(changes, change{
Text: fmt.Sprintf("Table %s: all existing primary key constraints have been removed and a primary key new constraint has been added to `_cq_id`", backtickStrings(table)...),
Breaking: true,
})
}
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
}
//nolint:revive
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
}