Skip to content

Commit 59f198b

Browse files
refactor(merge_insert): adapt insertIntoPksTableAndGetPk for Rust/C pks schema (TASK-149)
- Unpack pk_blob into individual PK column values using codec.unpack - Build dynamic INSERT: INSERT INTO pks (__crsql_key, col1, col2) VALUES (NULL, ?, ?) - Bind unpacked values in PK order - Return auto-generated __crsql_key via last_insert_rowid() - Remove base_rowid parameter from function signature - Update call sites in changes_vtab.zig to use new signature - Fix line 1744 to call insertOrUpdateColumn (was missing insertIntoBaseTable) - Mark TableMergeStmts.sql_insert_pks as DEPRECATED (old schema) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 033b2af commit 59f198b

File tree

3 files changed

+421
-600
lines changed

3 files changed

+421
-600
lines changed

.tasks/active/TASK-149-refactor-insertIntoPksTableAndGetPk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Triggering issue:
3131
- `insertIntoPksTable()` (line ~658)
3232
- `insertIntoPksTableAndGetPk()` (line ~671)
3333
- `TableMergeStmts.sql_insert_pks` buffer and statement (line ~135)
34+
- `zig/src/changes_vtab.zig`:
35+
- Remove base_rowid parameter from insertIntoPksTableAndGetPk calls (lines 1713, 1751)
3436

3537
## Acceptance Criteria
3638
1. `insertIntoPksTableAndGetPk()` must:
@@ -64,6 +66,9 @@ Triggering issue:
6466
## Progress Log
6567
- 2025-12-21: Created from TASK-147 refactoring work. Compound PK bug fixed, findPkFromBlob refactored, this is next critical blocker.
6668
- 2025-12-21 14:30: Starting implementation. Reading existing code to understand the pattern from findPkFromBlob refactor.
69+
- 2025-12-21 14:35: Analyzed findPkFromBlob pattern. Now implementing new schema support with codec.unpack + dynamic INSERT.
70+
- 2025-12-21 14:45: Refactored insertIntoPksTableAndGetPk in merge_insert.zig - unpacks blob, builds dynamic INSERT, returns last_insert_rowid. Now fixing call sites in changes_vtab.zig.
71+
- 2025-12-21 15:00: Fixed call sites in changes_vtab.zig - removed base_rowid parameters from lines 1713, 1751. Fixed line 1744 to call insertOrUpdateColumn instead of missing insertIntoBaseTable function. Ready to build and test.
6772

6873
## Completion Notes
6974
(Empty until done.)

zig/src/changes_vtab.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ fn changesUpdate(
17101710
};
17111711

17121712
// Insert into __crsql_pks table and get the pk (auto-increment key)
1713-
const pks_pk = merge_insert.insertIntoPksTableAndGetPk(api_db, table_slice, base_rowid, pk_ptr, @intCast(pk_len)) catch {
1713+
const pks_pk = merge_insert.insertIntoPksTableAndGetPk(api_db, table_slice, pk_ptr, @intCast(pk_len)) catch {
17141714
log.debug("changesUpdate: insertIntoPksTableAndGetPk for PK-only failed", .{});
17151715
return vtab.SQLITE_ERROR;
17161716
};
@@ -1741,14 +1741,14 @@ fn changesUpdate(
17411741
const insert_value = toApiValue(argv[5]);
17421742

17431743
// Step 1a: Insert into base table (returns the base table rowid)
1744-
const base_rowid = merge_insert.insertIntoBaseTable(api_db, table_slice, cid_slice, insert_value, pk_ptr, @intCast(pk_len)) catch {
1745-
log.debug("changesUpdate: insertIntoBaseTable failed", .{});
1744+
merge_insert.insertOrUpdateColumn(api_db, table_slice, cid_slice, insert_value, pk_ptr, @intCast(pk_len)) catch {
1745+
log.debug("changesUpdate: insertOrUpdateColumn failed", .{});
17461746
return vtab.SQLITE_ERROR;
17471747
};
17481748

17491749
// Step 1b: Insert into __crsql_pks table and get the pk (auto-increment key)
17501750
// The pk is what we use for clock table entries, NOT the base table rowid
1751-
const pks_pk = merge_insert.insertIntoPksTableAndGetPk(api_db, table_slice, base_rowid, pk_ptr, @intCast(pk_len)) catch {
1751+
const pks_pk = merge_insert.insertIntoPksTableAndGetPk(api_db, table_slice, pk_ptr, @intCast(pk_len)) catch {
17521752
log.debug("changesUpdate: insertIntoPksTableAndGetPk failed", .{});
17531753
return vtab.SQLITE_ERROR;
17541754
};

0 commit comments

Comments
 (0)