|
4 | 4 | "reflect" |
5 | 5 | "testing" |
6 | 6 |
|
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
7 | 9 | "github.com/PeerDB-io/peerdb/flow/connectors/utils" |
8 | 10 | "github.com/PeerDB-io/peerdb/flow/generated/protos" |
9 | 11 | ) |
@@ -138,3 +140,213 @@ func TestGenerateMergeUpdateStatement_WithUnchangedToastColsAndSoftDelete(t *tes |
138 | 140 | t.Errorf("Unexpected result. Expected: %v, but got: %v", expected, result) |
139 | 141 | } |
140 | 142 | } |
| 143 | + |
| 144 | +// helper to build a simple PG-typed TableSchema for merge tests |
| 145 | +func buildTableSchema(columns []*protos.FieldDescription, pks []string) *protos.TableSchema { |
| 146 | + return &protos.TableSchema{ |
| 147 | + TableIdentifier: "test_table", |
| 148 | + System: protos.TypeSystem_PG, |
| 149 | + PrimaryKeyColumns: pks, |
| 150 | + Columns: columns, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func normalizeSQL(s string) string { |
| 155 | + return utils.RemoveSpacesTabsNewlines(s) |
| 156 | +} |
| 157 | + |
| 158 | +func TestGenerateMergeStatement_BasicColumns(t *testing.T) { |
| 159 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 160 | + {Name: "id", Type: "integer"}, |
| 161 | + {Name: "name", Type: "text"}, |
| 162 | + {Name: "value", Type: "numeric"}, |
| 163 | + }, []string{"id"}) |
| 164 | + |
| 165 | + gen := normalizeStmtGenerator{ |
| 166 | + rawTableName: "_peerdb_raw_test", |
| 167 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 168 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 169 | + peerdbCols: &protos.PeerDBColumns{ |
| 170 | + SyncedAtColName: "_peerdb_synced_at", |
| 171 | + SoftDeleteColName: "", |
| 172 | + }, |
| 173 | + metadataSchema: "_peerdb_internal", |
| 174 | + supportsMerge: true, |
| 175 | + } |
| 176 | + |
| 177 | + result := gen.generateMergeStatement("public.test_table", schema, []string{""}) |
| 178 | + |
| 179 | + // CTE uses jsonb_to_record with record definitions |
| 180 | + require.Contains(t, result, "jsonb_to_record(_peerdb_data)") |
| 181 | + require.Contains(t, result, `"id" integer`) |
| 182 | + require.Contains(t, result, `"name" text`) |
| 183 | + require.Contains(t, result, `"value" numeric`) |
| 184 | + // USING select just references the column names directly (no ->> casts) |
| 185 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`"id","name","value",_peerdb_record_type,_peerdb_unchanged_toast_columns`)) |
| 186 | + // MERGE ON condition |
| 187 | + require.Contains(t, result, `src."id"=dst."id"`) |
| 188 | + require.NotContains(t, result, "_peerdb_data->>") |
| 189 | +} |
| 190 | + |
| 191 | +func TestGenerateMergeStatement_JsonColumns(t *testing.T) { |
| 192 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 193 | + {Name: "id", Type: "integer"}, |
| 194 | + {Name: "metadata", Type: "json"}, |
| 195 | + {Name: "config", Type: "jsonb"}, |
| 196 | + }, []string{"id"}) |
| 197 | + |
| 198 | + gen := normalizeStmtGenerator{ |
| 199 | + rawTableName: "_peerdb_raw_test", |
| 200 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 201 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 202 | + peerdbCols: &protos.PeerDBColumns{ |
| 203 | + SyncedAtColName: "_peerdb_synced_at", |
| 204 | + SoftDeleteColName: "", |
| 205 | + }, |
| 206 | + metadataSchema: "_peerdb_internal", |
| 207 | + supportsMerge: true, |
| 208 | + } |
| 209 | + |
| 210 | + result := gen.generateMergeStatement("public.test_table", schema, []string{""}) |
| 211 | + |
| 212 | + // json/jsonb columns: record defs should declare them as jsonb |
| 213 | + require.Contains(t, result, `"metadata" jsonb`) |
| 214 | + require.Contains(t, result, `"config" jsonb`) |
| 215 | + // USING select should unwrap them via #>> |
| 216 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`("metadata" #>> '{}')::json AS "metadata"`)) |
| 217 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`("config" #>> '{}')::jsonb AS "config"`)) |
| 218 | +} |
| 219 | + |
| 220 | +func TestGenerateMergeStatement_SoftDelete(t *testing.T) { |
| 221 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 222 | + {Name: "id", Type: "integer"}, |
| 223 | + {Name: "data", Type: "text"}, |
| 224 | + }, []string{"id"}) |
| 225 | + |
| 226 | + gen := normalizeStmtGenerator{ |
| 227 | + rawTableName: "_peerdb_raw_test", |
| 228 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 229 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 230 | + peerdbCols: &protos.PeerDBColumns{ |
| 231 | + SyncedAtColName: "_peerdb_synced_at", |
| 232 | + SoftDeleteColName: "_peerdb_soft_delete", |
| 233 | + }, |
| 234 | + metadataSchema: "_peerdb_internal", |
| 235 | + supportsMerge: true, |
| 236 | + } |
| 237 | + |
| 238 | + result := gen.generateMergeStatement("public.test_table", schema, []string{""}) |
| 239 | + |
| 240 | + // soft delete: WHEN NOT MATCHED with record_type=2 inserts with soft delete TRUE |
| 241 | + require.Contains(t, normalizeSQL(result), |
| 242 | + normalizeSQL(`WHEN NOT MATCHED AND (src._peerdb_record_type=2) THEN INSERT`)) |
| 243 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`"_peerdb_soft_delete"`)) |
| 244 | + // conflict part should be UPDATE SET soft_delete=TRUE |
| 245 | + require.Contains(t, normalizeSQL(result), |
| 246 | + normalizeSQL(`UPDATE SET "_peerdb_soft_delete"=TRUE,"_peerdb_synced_at"=CURRENT_TIMESTAMP`)) |
| 247 | +} |
| 248 | + |
| 249 | +func TestGenerateMergeStatement_CompositePK(t *testing.T) { |
| 250 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 251 | + {Name: "tenant_id", Type: "integer"}, |
| 252 | + {Name: "user_id", Type: "integer"}, |
| 253 | + {Name: "email", Type: "text"}, |
| 254 | + }, []string{"tenant_id", "user_id"}) |
| 255 | + |
| 256 | + gen := normalizeStmtGenerator{ |
| 257 | + rawTableName: "_peerdb_raw_test", |
| 258 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 259 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 260 | + peerdbCols: &protos.PeerDBColumns{ |
| 261 | + SyncedAtColName: "_peerdb_synced_at", |
| 262 | + SoftDeleteColName: "", |
| 263 | + }, |
| 264 | + metadataSchema: "_peerdb_internal", |
| 265 | + supportsMerge: true, |
| 266 | + } |
| 267 | + |
| 268 | + result := gen.generateMergeStatement("public.test_table", schema, []string{""}) |
| 269 | + |
| 270 | + // composite PK: PARTITION BY should use both PK columns |
| 271 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`PARTITION BY "tenant_id","user_id"`)) |
| 272 | + // ON clause should join on both |
| 273 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`src."tenant_id"=dst."tenant_id"`)) |
| 274 | + require.Contains(t, normalizeSQL(result), normalizeSQL(`src."user_id"=dst."user_id"`)) |
| 275 | +} |
| 276 | + |
| 277 | +func TestGenerateMergeStatement_ToastColumns(t *testing.T) { |
| 278 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 279 | + {Name: "id", Type: "integer"}, |
| 280 | + {Name: "small_col", Type: "text"}, |
| 281 | + {Name: "big_col", Type: "text"}, |
| 282 | + }, []string{"id"}) |
| 283 | + |
| 284 | + gen := normalizeStmtGenerator{ |
| 285 | + rawTableName: "_peerdb_raw_test", |
| 286 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 287 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {"", "big_col"}}, |
| 288 | + peerdbCols: &protos.PeerDBColumns{ |
| 289 | + SyncedAtColName: "_peerdb_synced_at", |
| 290 | + SoftDeleteColName: "", |
| 291 | + }, |
| 292 | + metadataSchema: "_peerdb_internal", |
| 293 | + supportsMerge: true, |
| 294 | + } |
| 295 | + |
| 296 | + result := gen.generateMergeStatement("public.test_table", schema, []string{"", "big_col"}) |
| 297 | + |
| 298 | + normalized := normalizeSQL(result) |
| 299 | + // Should have an update branch for unchanged toast col = '' (all cols updated) |
| 300 | + require.Contains(t, normalized, normalizeSQL(`_peerdb_unchanged_toast_columns=''`)) |
| 301 | + // Should have an update branch for unchanged toast col = 'big_col' (only id and small_col updated) |
| 302 | + require.Contains(t, normalized, normalizeSQL(`_peerdb_unchanged_toast_columns='big_col'`)) |
| 303 | + require.Contains(t, normalized, normalizeSQL(`"id"=src."id","small_col"=src."small_col"`)) |
| 304 | +} |
| 305 | + |
| 306 | +func TestGenerateMergeStatement_UserDefinedType(t *testing.T) { |
| 307 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 308 | + {Name: "id", Type: "integer"}, |
| 309 | + {Name: "status", Type: "my_enum", TypeSchemaName: "my_schema"}, |
| 310 | + }, []string{"id"}) |
| 311 | + |
| 312 | + gen := normalizeStmtGenerator{ |
| 313 | + rawTableName: "_peerdb_raw_test", |
| 314 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 315 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 316 | + peerdbCols: &protos.PeerDBColumns{ |
| 317 | + SyncedAtColName: "_peerdb_synced_at", |
| 318 | + SoftDeleteColName: "", |
| 319 | + }, |
| 320 | + metadataSchema: "_peerdb_internal", |
| 321 | + supportsMerge: true, |
| 322 | + } |
| 323 | + |
| 324 | + result := gen.generateMergeStatement("public.test_table", schema, []string{""}) |
| 325 | + |
| 326 | + // User-defined types should be schema-qualified in the record definition |
| 327 | + require.Contains(t, result, `"status" "my_schema"."my_enum"`) |
| 328 | +} |
| 329 | + |
| 330 | +func TestGenerateNormalizeStatements_Merge(t *testing.T) { |
| 331 | + schema := buildTableSchema([]*protos.FieldDescription{ |
| 332 | + {Name: "id", Type: "integer"}, |
| 333 | + {Name: "name", Type: "text"}, |
| 334 | + }, []string{"id"}) |
| 335 | + |
| 336 | + gen := normalizeStmtGenerator{ |
| 337 | + rawTableName: "_peerdb_raw_test", |
| 338 | + tableSchemaMapping: map[string]*protos.TableSchema{"public.test_table": schema}, |
| 339 | + unchangedToastColumnsMap: map[string][]string{"public.test_table": {""}}, |
| 340 | + peerdbCols: &protos.PeerDBColumns{ |
| 341 | + SyncedAtColName: "_peerdb_synced_at", |
| 342 | + SoftDeleteColName: "", |
| 343 | + }, |
| 344 | + metadataSchema: "_peerdb_internal", |
| 345 | + supportsMerge: true, |
| 346 | + } |
| 347 | + |
| 348 | + stmts := gen.generateNormalizeStatements("public.test_table") |
| 349 | + require.Len(t, stmts, 1) |
| 350 | + require.Contains(t, stmts[0], "jsonb_to_record") |
| 351 | + require.Contains(t, stmts[0], "MERGE INTO") |
| 352 | +} |
0 commit comments