-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
314 lines (253 loc) · 7.06 KB
/
sync.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package sync
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"reflect"
"sync"
sq "github.com/Masterminds/squirrel"
)
// SyncResult contains the results of syncing a single target table
type SyncResult struct {
Target TableConfig
TargetChecksum string
Synced bool
Error error
}
func (job JobConfig) syncTargets() (string, []SyncResult, error) {
primaryKeyIndices := job.getPrimaryKeyIndices()
source := table{
config: job.Source,
primaryKeys: job.PrimaryKeys,
primaryKeyIndices: primaryKeyIndices,
columns: job.Columns,
}
// Connect to the source
if err := source.connect(); err != nil {
return "", nil, err
}
targets := make([]table, len(job.Targets))
for i, target := range job.Targets {
targets[i] = table{
config: target,
primaryKeys: job.PrimaryKeys,
primaryKeyIndices: primaryKeyIndices,
columns: job.Columns,
}
}
// Get all rows from the source table and put them in a map by their primary key
sourceEntries, sourceMap, err := source.getEntries()
if err != nil {
return "", nil, err
}
// Close the source connection pool
source.Close()
sourceChecksum, err := checksumData(sourceEntries)
if err != nil {
return "", nil, err
}
var wg sync.WaitGroup
resultChan := make(chan SyncResult, len(targets))
for _, target := range targets {
wg.Add(1)
go func(target table) {
defer wg.Done()
// Connect to each target
if err := target.connect(); err != nil {
resultChan <- SyncResult{
Target: target.config,
Error: err,
}
return
}
checksum, synced, err := target.syncTarget(sourceChecksum, sourceMap)
target.Close() // Close the target's connection pool
resultChan <- SyncResult{
Target: target.config,
TargetChecksum: checksum,
Synced: synced,
Error: err,
}
}(target)
}
wg.Wait() // Wait for all goroutines to finish
close(resultChan) // Close the channel to signal that all results have been sent
// Collect the results from the channel
results := make([]SyncResult, 0, len(targets))
for result := range resultChan {
results = append(results, result)
}
return sourceChecksum, results, nil
}
func (t table) syncTarget(
sourceChecksum string,
sourceMap map[primaryKeyTuple][]any,
) (string, bool, error) {
targetEntries, targetMap, err := t.getEntries()
if err != nil {
return "", false, err
}
targetChecksum, err := checksumData(targetEntries)
if err != nil {
return "", false, err
}
// If the checksums match, then the data is already in sync
if sourceChecksum == targetChecksum {
return targetChecksum, false, nil
}
tableName := t.config.Table
var inserts []sq.InsertBuilder
var updates []sq.UpdateBuilder
var deletes []sq.DeleteBuilder
// Iterate over source rows and perform INSERTs or UPDATEs as needed
for key, val := range sourceMap {
// If the key doesn't exist in targetMap, then we need to INSERT
if _, ok := targetMap[key]; !ok {
insert := sq.Insert(tableName).Columns(t.columns...).Values(val...)
inserts = append(inserts, insert)
} else {
// If the key exists in targetMap, then we need to check if there is a diff
// Remove the key from the targetMap (to keep track of which rows we need to delete)
delete(targetMap, key)
if reflect.DeepEqual(val, targetMap[key]) {
continue // No diff, so we skip this row
}
// There is a diff, perform an UPDATE
update := sq.
Update(tableName).
Where(key.whereClause(t.primaryKeys, t.primaryKeyIndices))
pkSet := map[string]struct{}{}
for _, pk := range t.primaryKeys {
pkSet[pk] = struct{}{}
}
var hasUpdate bool
for i, col := range t.columns {
if _, ok := pkSet[col]; ok {
continue // Skip updating primary key columns
}
update = update.Set(col, val[i])
hasUpdate = true
}
if hasUpdate {
updates = append(updates, update)
}
}
}
// Iterate over target rows and DELETE any that weren't in the source
for key := range targetMap {
delete := sq.
Delete(tableName).
Where(key.whereClause(t.primaryKeys, t.primaryKeyIndices))
deletes = append(deletes, delete)
}
// Actually execute the statements (DELETEs -> UPDATEs -> INSERTs)
for _, delete := range deletes {
if _, err := delete.RunWith(t.DB).Exec(); err != nil {
return "", false, err
}
}
for _, update := range updates {
if _, err := update.RunWith(t.DB).Exec(); err != nil {
return "", false, err
}
}
for _, insert := range inserts {
if _, err := insert.RunWith(t.DB).Exec(); err != nil {
return "", false, err
}
}
return targetChecksum, true, nil
}
func (t table) getEntries() ([][]any, map[primaryKeyTuple][]any, error) {
fetchAll := sq.
Select(t.columns...).
From(t.config.Table).
OrderBy(t.primaryKeys...)
sql, args, err := fetchAll.ToSql()
if err != nil {
return nil, nil, err
}
rows, err := t.Queryx(sql, args...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
entryList := [][]any{}
entryMap := map[primaryKeyTuple][]any{}
for rows.Next() {
cols, err := rows.SliceScan()
if err != nil {
return nil, nil, err
}
entryList = append(entryList, cols)
pkTuple := primaryKeyTuple{}
for i, idx := range t.primaryKeyIndices {
val := cols[idx]
// Convert []byte to string (because []byte is unhashable and can't be in a map key)
if _, ok := val.([]byte); ok {
val = string(val.([]byte))
}
switch i {
case 0:
pkTuple.First = val
case 1:
pkTuple.Second = val
case 2:
pkTuple.Third = val
}
}
entryMap[pkTuple] = cols
}
if err = rows.Err(); err != nil {
return nil, nil, err
}
return entryList, entryMap, nil
}
func checksumData(data [][]any) (string, error) {
// Serialize the data to JSON
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
// Compute the MD5 checksum of the JSON data
hash := md5.New()
if _, err := hash.Write(jsonData); err != nil {
return "", err
}
// Convert the checksum to a hexadecimal string
checksum := hex.EncodeToString(hash.Sum(nil))
return checksum, nil
}
func (job JobConfig) getPrimaryKeyIndices() []int {
// Create a map of column names to their index in the columns slice
columnIndices := map[string]int{}
for i, col := range job.Columns {
columnIndices[col] = i
}
// Determine the indices of the primary keys in the columns slice
var primaryKeyIndices []int
for _, pk := range job.PrimaryKeys {
if _, ok := columnIndices[pk]; ok {
primaryKeyIndices = append(primaryKeyIndices, columnIndices[pk])
}
}
return primaryKeyIndices
}
// We are not allowed to have a slice as a map key, so we use a struct instead
// For now, we limit to a maximum of 3 primary key columns
type primaryKeyTuple struct{ First, Second, Third any }
func (key primaryKeyTuple) whereClause(primaryKeys []string, primaryKeyIndices []int) sq.Eq {
where := sq.Eq{}
for i, idx := range primaryKeyIndices {
columnName := primaryKeys[idx]
switch i {
case 0:
where[columnName] = key.First
case 1:
where[columnName] = key.Second
case 2:
where[columnName] = key.Third
}
}
return where
}