-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathnormalize_stmt_generator_test.go
More file actions
352 lines (304 loc) · 14 KB
/
Copy pathnormalize_stmt_generator_test.go
File metadata and controls
352 lines (304 loc) · 14 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
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
package connpostgres
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
"github.com/PeerDB-io/peerdb/flow/connectors/utils"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
)
func TestGenerateMergeUpdateStatement(t *testing.T) {
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{""}
expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP`,
}
normalizeGen := normalizeStmtGenerator{
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
}
result := normalizeGen.generateUpdateStatements(allCols, unchangedToastCols)
for i := range expected {
expected[i] = utils.RemoveSpacesTabsNewlines(expected[i])
result[i] = utils.RemoveSpacesTabsNewlines(result[i])
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result)
}
}
func TestGenerateMergeUpdateStatement_WithSoftDelete(t *testing.T) {
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{""}
expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
}
normalizeGen := normalizeStmtGenerator{
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "_peerdb_soft_delete",
},
}
result := normalizeGen.generateUpdateStatements(allCols, unchangedToastCols)
for i := range expected {
expected[i] = utils.RemoveSpacesTabsNewlines(expected[i])
result[i] = utils.RemoveSpacesTabsNewlines(result[i])
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result)
}
}
func TestGenerateMergeUpdateStatement_WithUnchangedToastCols(t *testing.T) {
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{"", "col2,col3", "col2", "col3"}
expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP`,
}
normalizeGen := normalizeStmtGenerator{
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
}
result := normalizeGen.generateUpdateStatements(allCols, unchangedToastCols)
for i := range expected {
expected[i] = utils.RemoveSpacesTabsNewlines(expected[i])
result[i] = utils.RemoveSpacesTabsNewlines(result[i])
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result)
}
}
func TestGenerateMergeUpdateStatement_WithUnchangedToastColsAndSoftDelete(t *testing.T) {
allCols := []string{`"col1"`, `"col2"`, `"col3"`}
unchangedToastCols := []string{"", "col2,col3", "col2", "col3"}
expected := []string{
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns=''
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","col3"=src."col3",
"_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col2,col3'
THEN UPDATE SET "col1"=src."col1","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col2'
THEN UPDATE SET "col1"=src."col1","col3"=src."col3","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
`WHEN MATCHED AND src._peerdb_record_type!=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=FALSE`,
`WHEN MATCHED AND src._peerdb_record_type=2 AND _peerdb_unchanged_toast_columns='col3'
THEN UPDATE SET "col1"=src."col1","col2"=src."col2","_peerdb_synced_at"=CURRENT_TIMESTAMP,"_peerdb_soft_delete"=TRUE`,
}
normalizeGen := normalizeStmtGenerator{
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "_peerdb_soft_delete",
},
}
result := normalizeGen.generateUpdateStatements(allCols, unchangedToastCols)
for i := range expected {
expected[i] = utils.RemoveSpacesTabsNewlines(expected[i])
result[i] = utils.RemoveSpacesTabsNewlines(result[i])
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result)
}
}
// helper to build a simple PG-typed TableSchema for merge tests
func buildTableSchema(columns []*protos.FieldDescription, pks []string) *protos.TableSchema {
return &protos.TableSchema{
TableIdentifier: "test_table",
System: protos.TypeSystem_PG,
PrimaryKeyColumns: pks,
Columns: columns,
}
}
func normalizeSQL(s string) string {
return utils.RemoveSpacesTabsNewlines(s)
}
func TestGenerateMergeStatement_BasicColumns(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "name", Type: "text"},
{Name: "value", Type: "numeric"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{""})
// CTE uses jsonb_to_record with record definitions
require.Contains(t, result, "jsonb_to_record(_peerdb_data)")
require.Contains(t, result, `"id" integer`)
require.Contains(t, result, `"name" text`)
require.Contains(t, result, `"value" numeric`)
// USING select just references the column names directly (no ->> casts)
require.Contains(t, normalizeSQL(result), normalizeSQL(`"id","name","value",_peerdb_record_type,_peerdb_unchanged_toast_columns`))
// MERGE ON condition
require.Contains(t, result, `src."id"=dst."id"`)
require.NotContains(t, result, "_peerdb_data->>")
}
func TestGenerateMergeStatement_JsonColumns(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "metadata", Type: "json"},
{Name: "config", Type: "jsonb"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{""})
// json/jsonb columns: record defs should declare them as jsonb
require.Contains(t, result, `"metadata" jsonb`)
require.Contains(t, result, `"config" jsonb`)
// USING select should unwrap them via #>>
require.Contains(t, normalizeSQL(result), normalizeSQL(`("metadata" #>> '{}')::json AS "metadata"`))
require.Contains(t, normalizeSQL(result), normalizeSQL(`("config" #>> '{}')::jsonb AS "config"`))
}
func TestGenerateMergeStatement_SoftDelete(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "data", Type: "text"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "_peerdb_soft_delete",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{""})
// soft delete: WHEN NOT MATCHED with record_type=2 inserts with soft delete TRUE
require.Contains(t, normalizeSQL(result),
normalizeSQL(`WHEN NOT MATCHED AND (src._peerdb_record_type=2) THEN INSERT`))
require.Contains(t, normalizeSQL(result), normalizeSQL(`"_peerdb_soft_delete"`))
// conflict part should be UPDATE SET soft_delete=TRUE
require.Contains(t, normalizeSQL(result),
normalizeSQL(`UPDATE SET "_peerdb_soft_delete"=TRUE,"_peerdb_synced_at"=CURRENT_TIMESTAMP`))
}
func TestGenerateMergeStatement_CompositePK(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "tenant_id", Type: "integer"},
{Name: "user_id", Type: "integer"},
{Name: "email", Type: "text"},
}, []string{"tenant_id", "user_id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{""})
// composite PK: PARTITION BY should use both PK columns
require.Contains(t, normalizeSQL(result), normalizeSQL(`PARTITION BY "tenant_id","user_id"`))
// ON clause should join on both
require.Contains(t, normalizeSQL(result), normalizeSQL(`src."tenant_id"=dst."tenant_id"`))
require.Contains(t, normalizeSQL(result), normalizeSQL(`src."user_id"=dst."user_id"`))
}
func TestGenerateMergeStatement_ToastColumns(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "small_col", Type: "text"},
{Name: "big_col", Type: "text"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {"", "big_col"}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{"", "big_col"})
normalized := normalizeSQL(result)
// Should have an update branch for unchanged toast col = '' (all cols updated)
require.Contains(t, normalized, normalizeSQL(`_peerdb_unchanged_toast_columns=''`))
// Should have an update branch for unchanged toast col = 'big_col' (only id and small_col updated)
require.Contains(t, normalized, normalizeSQL(`_peerdb_unchanged_toast_columns='big_col'`))
require.Contains(t, normalized, normalizeSQL(`"id"=src."id","small_col"=src."small_col"`))
}
func TestGenerateMergeStatement_UserDefinedType(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "status", Type: "my_enum", TypeSchemaName: "my_schema"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
result := gen.generateMergeStatement("public.test_table", schema, []string{""})
// User-defined types should be schema-qualified in the record definition
require.Contains(t, result, `"status" "my_schema"."my_enum"`)
}
func TestGenerateNormalizeStatements_Merge(t *testing.T) {
schema := buildTableSchema([]*protos.FieldDescription{
{Name: "id", Type: "integer"},
{Name: "name", Type: "text"},
}, []string{"id"})
gen := normalizeStmtGenerator{
rawTableName: "_peerdb_raw_test",
tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema},
unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}},
peerdbCols: &protos.PeerDBColumns{
SyncedAtColName: "_peerdb_synced_at",
SoftDeleteColName: "",
},
metadataSchema: "_peerdb_internal",
supportsMerge: true,
}
stmts := gen.generateNormalizeStatements("public.test_table")
require.Len(t, stmts, 1)
require.Contains(t, stmts[0], "jsonb_to_record")
require.Contains(t, stmts[0], "MERGE INTO")
}