Skip to content

Commit 81f44fe

Browse files
committed
Skip creating ps_crud entries when clearing raw tables
1 parent ac6ca06 commit 81f44fe

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

crates/core/src/view_admin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ DELETE FROM {table};",
122122
}
123123

124124
if let Some(schema) = state.view_schema() {
125+
// Pretend to be in a sync_local step when clearing raw tables. Similar to the case above
126+
// where we delete from the underlying table to sidestep the CRUD trigger, we don't want
127+
// triggers on raw tables to record this delete in ps_crud.
128+
let _skip_crud = state.sync_local_guard();
129+
125130
for raw_table in &schema.raw_tables {
126131
if let Some(stmt) = &raw_table.clear {
127132
local_db.exec_safe(&stmt).map_err(|e| {

dart/test/crud_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,26 @@ void main() {
946946
db.execute('UPDATE users SET name = ?', ['name']);
947947
expect(db.select('SELECT * FROM ps_crud'), hasLength(1));
948948
});
949+
950+
test('clearing raw tables does not create crud entries', () {
951+
db.execute('CREATE TABLE users (id TEXT, name TEXT) STRICT;');
952+
db.execute(
953+
'INSERT INTO users (id, name) VALUES (uuid(), ?)', ['test user']);
954+
955+
createRawTableTriggers(rawTableDescription(
956+
{'table_name': 'users', 'ignore_empty_update': true}));
957+
db.execute('select powersync_replace_schema(?)', [
958+
json.encode({
959+
'tables': [],
960+
'raw_tables': [
961+
rawTableDescription({'clear': 'DELETE FROM users'})
962+
]
963+
})
964+
]);
965+
966+
db.execute('SELECT powersync_clear(2)');
967+
expect(db.select('SELECT * FROM ps_crud'), isEmpty);
968+
});
949969
});
950970
});
951971
}

0 commit comments

Comments
 (0)