From 828d3d247bbb86603e05ededf9b6fcaf5122ea3a Mon Sep 17 00:00:00 2001 From: Amine Date: Thu, 12 Nov 2026 15:48:14 +0800 Subject: [PATCH] Use upsert instead of REPLACE INTO for synced rows --- crates/core/src/sync/sync_local.rs | 11 ++++-- dart/test/sync_test.dart | 57 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/core/src/sync/sync_local.rs b/crates/core/src/sync/sync_local.rs index eda2cd6..f3722d6 100644 --- a/crates/core/src/sync/sync_local.rs +++ b/crates/core/src/sync/sync_local.rs @@ -166,7 +166,7 @@ WHERE target.key = '{TARGET_CHECKPOINT_REQUEST_ID_KEY}' None => { // Prepare statement on first use untyped_insert_statement.insert(self.db.prepare_v2( - "REPLACE INTO ps_untyped(type, id, data) VALUES(?, ?, ?)", + "INSERT INTO ps_untyped(type, id, data) VALUES(?, ?, ?) ON CONFLICT (type, id) DO UPDATE SET data = excluded.data", )?) } }; @@ -420,10 +420,15 @@ impl<'a> ParsedSchemaTable<'a> { None => cache.infer_put_statement(db, schema_version, raw_table)?, }, TableDefinition::JsonView { ref local_table } => { + // Use an upsert instead of REPLACE INTO: REPLACE deletes the existing row + // without running DELETE triggers and gives the new row a different rowid, + // which breaks triggers on the internal table (such as FTS indexes). let mut statement = SqlBuffer::new(); - statement.push_str("REPLACE INTO "); + statement.push_str("INSERT INTO "); let _ = statement.identifier().write_str(local_table); - statement.push_str("(id, data) VALUES (?, ?)"); + statement.push_str( + "(id, data) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET data = excluded.data", + ); Rc::new(PendingStatement { sql: statement.sql, diff --git a/dart/test/sync_test.dart b/dart/test/sync_test.dart index 2087ffc..d2054a6 100644 --- a/dart/test/sync_test.dart +++ b/dart/test/sync_test.dart @@ -954,6 +954,63 @@ void _syncTests({ expect(db.select('SELECT * FROM items'), hasLength(1)); }); + group('triggers on internal tables', () { + void syncPut(int opId, String objectType, Object data) { + pushCheckpoint( + lastOpId: opId, buckets: [bucketDescription('a', count: opId)]); + pushSyncData('a', '$opId', 'row-0', 'PUT', data, objectType: objectType); + pushCheckpointComplete(lastOpId: '$opId'); + } + + test('synced update of existing row runs update triggers', () { + db.execute('CREATE TABLE trigger_log (op TEXT, id TEXT, data TEXT)'); + for (final (op, row) in [ + ('INSERT', 'NEW'), + ('UPDATE', 'NEW'), + ('DELETE', 'OLD'), + ]) { + db.execute(''' +CREATE TRIGGER log_$op AFTER $op ON ps_data__items BEGIN + INSERT INTO trigger_log VALUES ('$op', $row.id, $row.data); +END;'''); + } + + invokeControl('start', null); + syncPut(1, 'items', {'col': 'a'}); + final [first] = db.select('SELECT rowid FROM ps_data__items'); + + syncPut(2, 'items', {'col': 'b'}); + final [second] = db.select('SELECT rowid FROM ps_data__items'); + + expect(fetchRows(), [ + {'id': 'row-0', 'col': 'b'} + ]); + // REPLACE INTO would delete the old row without running DELETE + // triggers and insert the new row with a new rowid. + expect(second['rowid'], first['rowid']); + expect(db.select('SELECT * FROM trigger_log'), [ + {'op': 'INSERT', 'id': 'row-0', 'data': '{"col":"a"}'}, + {'op': 'UPDATE', 'id': 'row-0', 'data': '{"col":"b"}'}, + ]); + }); + + test('synced update of untyped row keeps rowid', () { + invokeControl('start', null); + syncPut(1, 'not_in_schema', {'col': 'a'}); + final [first] = db.select('SELECT rowid FROM ps_untyped'); + + syncPut(2, 'not_in_schema', {'col': 'b'}); + expect(db.select('SELECT rowid, type, id, data FROM ps_untyped'), [ + { + 'rowid': first['rowid'], + 'type': 'not_in_schema', + 'id': 'row-0', + 'data': '{"col":"b"}', + } + ]); + }); + }); + group('trigger resync', () { test('forbidden during sync', () { invokeControl('start', null);