-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.go
599 lines (567 loc) · 17.2 KB
/
triggers.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-FileCopyrightText: © 2024 Kevin Conway <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package cdc
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
)
const (
defaultLogTableName = "__cdc_log"
defaultMaxBatchSize = 50
)
type Option func(*triggers) error
func WithLogTableName(name string) Option {
return func(t *triggers) error {
t.logTableName = name
return nil
}
}
func WithMaxBatchSize(size int) Option {
return func(t *triggers) error {
t.maxBatchSize = size
return nil
}
}
func WithoutSubsecondTime(v bool) Option {
return func(t *triggers) error {
t.subsec = !v
return nil
}
}
func WithBlobSupport(v bool) Option {
return func(t *triggers) error {
t.blobs = v
return nil
}
}
// New returns a CDC implementation based on triggers.
//
// This implementation works with any SQLite driver and uses only SQL operations
// to implement CDC. For each specified table to monitor, the implementation
// creates triggers for AFTER INSERT, AFTER UPDATE, and AFTER DELETE. These
// triggers populate a log table, named __cdc_log by default. The log table
// entries contain effectively identical information as the Change struct.
//
// The before and after images are stored as JSON objects in the log table. The
// JSON objects are generated from the column names and values in the table.
// Currently, only non-BLOB data types are supported in the before and after
// images. Any BLOB type column is left out of the image. Note that the
// limitation is associated with BLOB type data and not specifically BLOB type
// columns. You are recommended to use strict tables to avoid non-BLOB columns
// from containing BLOB data.
func New(db *sql.DB, handler ChangesHandler, tables []string, options ...Option) (CDC, error) {
meta, err := newDBMeta(db)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("%w: failed to create fsnotify watcher", err)
}
result := &triggers{
db: db,
meta: meta,
handler: handler,
tables: tables,
fnOnce: &sync.Once{},
watcher: watcher,
closed: make(chan any),
closeOnce: &sync.Once{},
logTableName: defaultLogTableName,
maxBatchSize: defaultMaxBatchSize,
subsec: true,
blobs: false,
}
for _, opt := range options {
if err := opt(result); err != nil {
return nil, err
}
}
return result, nil
}
type triggers struct {
db *sql.DB
meta *dbMeta
handler ChangesHandler
tables []string
fnOnce *sync.Once
watcher *fsnotify.Watcher
closed chan any
closeOnce *sync.Once
logTableName string
maxBatchSize int
subsec bool
blobs bool
}
func (c *triggers) CDC(ctx context.Context) error {
var err error
c.fnOnce.Do(func() {
err = c.cdc(ctx)
})
return err
}
func (c *triggers) cdc(ctx context.Context) error {
if err := c.watcher.Add(filepath.Dir(c.meta.Filename)); err != nil {
return fmt.Errorf("%w: failed to add %q to fsnotify watcher", err, filepath.Dir(c.meta.Filename))
}
watchTargets := make(map[string]bool)
watchTargets[c.meta.Filename] = true
if c.meta.WAL {
watchTargets[c.meta.Filename+"-wal"] = true
watchTargets[c.meta.Filename+"-shm"] = true
}
for {
select {
case <-c.closed:
return nil
case <-ctx.Done():
return c.Close(ctx)
case event, ok := <-c.watcher.Events:
if !ok {
// The watcher was closed, most likely from an explicit call to
// CDC.Close.
return c.Close(ctx)
}
if event.Op == fsnotify.Chmod {
continue
}
if !watchTargets[event.Name] {
continue
}
if err := c.drainChanges(ctx); err != nil {
return fmt.Errorf("%w: failed to process changes from the log", err)
}
case err, ok := <-c.watcher.Errors:
if !ok {
// The watcher was closed, most likely from an explicit call to
// CDC.Close.
return c.Close(ctx)
}
return fmt.Errorf("%w: fsnotify watcher error", err)
}
}
}
func (c *triggers) drainChanges(ctx context.Context) error {
changes := make(Changes, 0, c.maxBatchSize)
for {
rows, err := c.db.QueryContext(ctx, `SELECT id, timestamp, tablename, operation, before, after FROM `+c.logTableName+` ORDER BY id ASC LIMIT ?`, c.maxBatchSize) //nolint:gosec
if err != nil {
return fmt.Errorf("%w: failed to select changes from the log", err)
}
defer rows.Close()
maxID := new(int64)
for rows.Next() {
timestamp := new(string)
table := new(string)
operation := new(string)
before := &sql.NullString{}
after := &sql.NullString{}
if err := rows.Scan(maxID, timestamp, table, operation, before, after); err != nil {
return fmt.Errorf("%w: failed to read change record from the log", err)
}
ts, err := time.Parse("2006-01-02 15:04:05.999999999", *timestamp)
if err != nil {
return fmt.Errorf("%w: failed to parse timestamp %s from the log", err, *timestamp)
}
ch := Change{
Timestamp: ts,
Table: *table,
Operation: strToOperation(*operation),
}
if before.Valid {
ch.Before = []byte(before.String)
}
if after.Valid {
ch.After = []byte(after.String)
}
changes = append(changes, ch)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("%w: failed to read changes from the log", err)
}
if len(changes) < 1 {
return nil
}
if err := c.handle(ctx, changes); err != nil {
return fmt.Errorf("%w: failed to handle changes", err)
}
changes = changes[:0]
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("%w: failed to create transaction to delete logs", err)
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `DELETE FROM `+c.logTableName+` WHERE id <= ?`, *maxID) //nolint:gosec
if err != nil {
return fmt.Errorf("%w: failed to delete handled logs", err)
}
if err = tx.Commit(); err != nil {
return fmt.Errorf("%w: failed to commit deletion of logs", err)
}
}
}
func (c *triggers) Bootstrap(ctx context.Context) error {
var err error
c.fnOnce.Do(func() {
err = c.bootstrap(ctx)
})
return err
}
func (c *triggers) bootstrap(ctx context.Context) error {
for _, table := range c.tables {
if err := c.bootstrapTable(ctx, table); err != nil {
return fmt.Errorf("%w: failed to bootstrap table %s", err, table)
}
}
return nil
}
func (c *triggers) bootstrapTable(ctx context.Context, table string) error {
t, ok := c.meta.Tables[table]
if !ok {
return fmt.Errorf("table %q not found in database", table)
}
q := sqlSelectFirst(t, c.blobs)
rows, err := c.db.QueryContext(ctx, q, c.maxBatchSize)
if err != nil {
return fmt.Errorf("%w: failed to select first bootstrap rows for %s", err, table)
}
defer rows.Close()
chs := make(Changes, 0, c.maxBatchSize)
selections := append(sqlKeyValuesForTable(t), new(string))
for rows.Next() {
if err := rows.Scan(selections...); err != nil {
return fmt.Errorf("%w: failed to read bootstrap row for %s", err, table)
}
body := selections[len(selections)-1].(*string)
chs = append(chs, Change{
Table: table,
Timestamp: time.Now(),
Operation: Insert,
After: []byte(*body),
})
}
if rows.Err() != nil {
return fmt.Errorf("%w: failed to read bootstrap rows for %s", rows.Err(), table)
}
_ = rows.Close()
if len(chs) < 1 {
return nil
}
if len(chs) < c.maxBatchSize {
return c.handle(ctx, chs)
}
if err := c.handle(ctx, chs); err != nil {
return fmt.Errorf("%w: failed to handle bootstrap changes for %s", err, table)
}
keys := make([]any, len(selections)-1)
copy(keys, selections[:len(selections)-1])
params := make([]any, len(keys)+1)
for {
q = sqlSelectNext(t, c.blobs)
copy(params, keys)
params[len(params)-1] = c.maxBatchSize
rows, err = c.db.QueryContext(ctx, q, params...)
if err != nil {
return fmt.Errorf("%w: failed to select bootstrap rows for %s", err, table)
}
defer rows.Close()
chs = chs[:0]
for rows.Next() {
selections = append(sqlKeyValuesForTable(t), new(string))
if err := rows.Scan(selections...); err != nil {
return fmt.Errorf("%w: failed to read bootstrap row for %s", err, table)
}
body := selections[len(selections)-1].(*string)
chs = append(chs, Change{
Table: table,
Timestamp: time.Now(),
Operation: Insert,
After: []byte(*body),
})
copy(keys, selections[:len(selections)-1])
}
if rows.Err() != nil {
return fmt.Errorf("%w: failed to read bootstrap rows for %s", rows.Err(), table)
}
_ = rows.Close()
if len(chs) < 1 {
return nil
}
if len(chs) < c.maxBatchSize {
return c.handle(ctx, chs)
}
if err := c.handle(ctx, chs); err != nil {
return fmt.Errorf("%w: failed to handle bootstrap changes for %s", err, table)
}
}
}
func (c *triggers) BootstrapAndCDC(ctx context.Context) error {
var err error
c.fnOnce.Do(func() {
err = c.bootstrap(ctx)
if err != nil {
return
}
err = c.cdc(ctx)
})
return err
}
func (c *triggers) Setup(ctx context.Context) error {
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("%w: failed to create setup transaction", err)
}
defer tx.Rollback()
logSQL := sqlCreateLogTable(c.logTableName)
if _, err = tx.Exec(logSQL); err != nil {
return fmt.Errorf("%w: failed to create log table", err)
}
for _, table := range c.tables {
t, ok := c.meta.Tables[table]
if !ok {
return fmt.Errorf("table %q not found in database", table)
}
if _, err = tx.Exec(sqlCreateTableTriggerInsert(c.logTableName, t, c.subsec, c.blobs)); err != nil {
return fmt.Errorf("%w: failed to create table trigger for inserts on %s", err, table)
}
if _, err = tx.Exec(sqlCreateTableTriggerUpdate(c.logTableName, t, c.subsec, c.blobs)); err != nil {
return fmt.Errorf("%w: failed to create table trigger for updates on %s", err, table)
}
if _, err = tx.Exec(sqlCreateTableTriggerDelete(c.logTableName, t, c.subsec, c.blobs)); err != nil {
return fmt.Errorf("%w: failed to create table trigger for deletes on %s", err, table)
}
}
if err = tx.Commit(); err != nil {
return fmt.Errorf("%w: failed to commit setup transaction", err)
}
return nil
}
func (c *triggers) Teardown(ctx context.Context) error {
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("%w: failed to create teardown transaction", err)
}
defer tx.Rollback()
for _, table := range c.tables {
t, ok := c.meta.Tables[table]
if !ok {
return fmt.Errorf("table %q not found in database", table)
}
if _, err = tx.Exec(sqlDeleteTableTriggerInsert(t)); err != nil {
return fmt.Errorf("%w: failed to delete table trigger for inserts on %s", err, table)
}
if _, err = tx.Exec(sqlDeleteTableTriggerUpdate(t)); err != nil {
return fmt.Errorf("%w: failed to delete table trigger for updates on %s", err, table)
}
if _, err = tx.Exec(sqlDeleteTableTriggerDelete(t)); err != nil {
return fmt.Errorf("%w: failed to delete table trigger for deletes on %s", err, table)
}
}
logSQL := sqlDeleteLogTable(c.logTableName)
if _, err = tx.Exec(logSQL); err != nil {
return fmt.Errorf("%w: failed to delete log table", err)
}
if err = tx.Commit(); err != nil {
return fmt.Errorf("%w: failed to commit teardown transaction", err)
}
return nil
}
func (c *triggers) Close(ctx context.Context) error {
c.closeOnce.Do(func() {
close(c.closed)
})
if err := c.watcher.Close(); err != nil {
return fmt.Errorf("%w: failed to close fsnotify watcher", err)
}
return c.db.Close()
}
func (c *triggers) handle(ctx context.Context, changes Changes) error {
return c.handler.HandleChanges(ctx, changes)
}
func sqlCreateLogTable(name string) string {
return `CREATE TABLE IF NOT EXISTS ` + name + ` (
id INTEGER PRIMARY KEY,
timestamp TEXT NOT NULL,
tablename TEXT NOT NULL,
operation TEXT NOT NULL,
before TEXT,
after TEXT
)`
}
func sqlCreateTableTriggerInsert(logTable string, table tableMeta, subsec bool, blobs bool) string {
return `CREATE TRIGGER IF NOT EXISTS ` + table.Name + `__cdc_insert AFTER INSERT ON ` + table.Name + ` BEGIN
INSERT INTO ` + logTable + ` (timestamp, tablename, operation, before, after) VALUES
(` + sqlDateTimeNow(subsec) + `, '` + table.Name + `', 'INSERT', NULL, ` + sqlJsonObject("NEW.", table.Columns, blobs) + `);
END`
}
func sqlCreateTableTriggerUpdate(logTable string, table tableMeta, subsec bool, blobs bool) string {
return `CREATE TRIGGER IF NOT EXISTS ` + table.Name + `__cdc_update AFTER UPDATE ON ` + table.Name + ` BEGIN
INSERT INTO ` + logTable + ` (timestamp, tablename, operation, before, after) VALUES
(` + sqlDateTimeNow(subsec) + `, '` + table.Name + `', 'UPDATE', ` + sqlJsonObject("OLD.", table.Columns, blobs) + `, ` + sqlJsonObject("NEW.", table.Columns, blobs) + `);
END`
}
func sqlCreateTableTriggerDelete(logTable string, table tableMeta, subsec bool, blobs bool) string {
return `CREATE TRIGGER IF NOT EXISTS ` + table.Name + `__cdc_delete AFTER DELETE ON ` + table.Name + ` BEGIN
INSERT INTO ` + logTable + ` (timestamp, tablename, operation, before, after) VALUES
(` + sqlDateTimeNow(subsec) + `, '` + table.Name + `', 'DELETE', ` + sqlJsonObject("OLD.", table.Columns, blobs) + `, NULL);
END`
}
func sqlDateTimeNow(subsec bool) string {
if subsec {
return "datetime('now', 'subsec')"
}
return "datetime('now')"
}
func sqlDeleteTableTriggerInsert(table tableMeta) string {
return `DROP TRIGGER IF EXISTS ` + table.Name + `__cdc_insert`
}
func sqlDeleteTableTriggerUpdate(table tableMeta) string {
return `DROP TRIGGER IF EXISTS ` + table.Name + `__cdc_update`
}
func sqlDeleteTableTriggerDelete(table tableMeta) string {
return `DROP TRIGGER IF EXISTS ` + table.Name + `__cdc_delete`
}
func sqlDeleteLogTable(table string) string {
return `DROP TABLE IF EXISTS ` + table
}
const colChunkSize = 63
func sqlJsonObject(prefix string, columns []columnMeta, blobs bool) string {
objects := make([]string, 0, (len(columns)/colChunkSize)+1)
var b strings.Builder
b.WriteString("json_object(")
for offset, column := range columns {
if strings.ToUpper(column.Type) == "BLOB" {
if blobs {
b.WriteString(fmt.Sprintf("'%s'", column.Name))
b.WriteString(", hex(")
b.WriteString(prefix + column.Name)
b.WriteString(")")
if (offset+1)%colChunkSize == 0 && offset < len(columns)-1 {
b.WriteString(")")
objects = append(objects, b.String())
b.Reset()
b.WriteString("json_object(")
continue
}
if offset < len(columns)-1 {
b.WriteString(", ")
}
}
continue
}
b.WriteString(fmt.Sprintf("'%s'", column.Name))
b.WriteString(", ")
b.WriteString(prefix + column.Name)
if (offset+1)%colChunkSize == 0 && offset < len(columns)-1 {
b.WriteString(")")
objects = append(objects, b.String())
b.Reset()
b.WriteString("json_object(")
continue
}
if offset < len(columns)-1 {
b.WriteString(", ")
}
}
b.WriteString(")")
objects = append(objects, b.String())
b.Reset()
if len(objects) == 1 {
return objects[0]
}
for offset, object := range objects {
if offset+1 == len(objects) {
b.WriteString(object)
for x := 1; x < len(objects); x = x + 1 {
b.WriteString(")")
}
continue
}
b.WriteString("json_patch(")
b.WriteString(object)
b.WriteString(", ")
}
return b.String()
}
func sqlSelectFirst(table tableMeta, blobs bool) string {
if !table.WithoutRowID {
return `SELECT rowid, ` + sqlJsonObject("", table.Columns, blobs) + ` AS body FROM ` + table.Name + ` ORDER BY rowid LIMIT ?`
}
var keyCount int
for _, column := range table.Columns {
if column.PK != 0 {
keyCount = keyCount + 1
}
}
keyColumns := make([]string, keyCount)
for _, column := range table.Columns {
if column.PK != 0 {
keyColumns[column.PK-1] = column.Name
}
}
return `SELECT ` + strings.Join(keyColumns, ", ") + `, ` + sqlJsonObject("", table.Columns, blobs) + ` AS body FROM ` + table.Name + ` ORDER BY ` + strings.Join(keyColumns, ", ") + ` LIMIT ?`
}
func sqlSelectNext(table tableMeta, blobs bool) string {
if !table.WithoutRowID {
return `SELECT rowid, ` + sqlJsonObject("", table.Columns, blobs) + ` AS body FROM ` + table.Name + ` WHERE rowid > ? ORDER BY rowid LIMIT ?`
}
var keyCount int
for _, column := range table.Columns {
if column.PK != 0 {
keyCount = keyCount + 1
}
}
keyColumns := make([]string, keyCount)
for _, column := range table.Columns {
if column.PK != 0 {
keyColumns[column.PK-1] = column.Name
}
}
var b strings.Builder
b.WriteString(`SELECT ` + strings.Join(keyColumns, ", ") + `, ` + sqlJsonObject("", table.Columns, blobs) + ` AS body FROM ` + table.Name)
b.WriteString(` WHERE `)
for offset, column := range keyColumns {
b.WriteString(column)
b.WriteString(" > ?")
if offset < keyCount-1 {
b.WriteString(" AND ")
}
}
b.WriteString(` ORDER BY ` + strings.Join(keyColumns, ", ") + ` LIMIT ?`)
return b.String()
}
func sqlKeyValuesForTable(table tableMeta) []any {
if !table.WithoutRowID {
return []any{new(int64)}
}
var keyCount int
for _, column := range table.Columns {
if column.PK != 0 {
keyCount = keyCount + 1
}
}
keyValues := make([]any, keyCount)
for offset, column := range table.Columns {
if column.PK != 0 {
keyValues[offset] = new(any)
}
}
return keyValues
}
func strToOperation(operation string) Operation {
switch strings.ToUpper(operation) {
case "INSERT":
return Insert
case "UPDATE":
return Update
case "DELETE":
return Delete
}
return Operation("UNKNOWN")
}