Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/core/src/sync/sync_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)?)
}
};
Expand Down Expand Up @@ -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,
Expand Down
57 changes: 57 additions & 0 deletions dart/test/sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,63 @@ void _syncTests<T>({
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);
Expand Down
Loading