Skip to content

Commit f2d3788

Browse files
committed
Test insert-only tables
1 parent c2e454c commit f2d3788

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

crates/core/src/schema/table_info.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ impl TableInfoFlags {
162162
}
163163

164164
pub const fn insert_only(self) -> bool {
165+
// Note: insert_only is incompatible with local_only. For backwards compatibility, we want
166+
// to silently ignore insert_only if local_only is set.
167+
if self.local_only() {
168+
return false;
169+
}
170+
165171
self.0 & Self::INSERT_ONLY != 0
166172
}
167173

dart/test/crud_test.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,5 +710,69 @@ void main() {
710710
containsPair('data', {'col': 'not an integer'}),
711711
]);
712712
});
713+
714+
group('insert only', () {
715+
test('smoke test', () {
716+
db
717+
..execute('select powersync_replace_schema(?)', [
718+
json.encode({
719+
'tables': [
720+
{
721+
'name': 'items',
722+
'insert_only': true,
723+
'columns': [
724+
{'name': 'col', 'type': 'int'}
725+
],
726+
}
727+
]
728+
})
729+
])
730+
..execute(
731+
'INSERT INTO items (id, col) VALUES (uuid(), 1)',
732+
);
733+
734+
expect(db.select('SELECT * FROM ps_crud'), hasLength(1));
735+
// Insert-only tables don't update the $local bucket
736+
expect(db.select('SELECT * FROM ps_buckets'), isEmpty);
737+
738+
// Can't update or delete insert-only tables.
739+
expect(() => db.execute('UPDATE items SET col = col + 1'),
740+
throwsA(anything));
741+
expect(() => db.execute('DELETE FROM items WHERE col = 1'),
742+
throwsA(anything));
743+
});
744+
745+
test('has no effect on local-only tables', () {
746+
db
747+
..execute('select powersync_replace_schema(?)', [
748+
json.encode({
749+
'tables': [
750+
{
751+
'name': 'items',
752+
'insert_only': true,
753+
'local_only': true,
754+
'columns': [
755+
{'name': 'col', 'type': 'int'}
756+
],
757+
}
758+
]
759+
})
760+
]);
761+
762+
db.execute(
763+
'INSERT INTO items (id, col) VALUES (uuid(), 1)',
764+
);
765+
expect(db.select('SELECT * FROM items'), hasLength(1));
766+
767+
db
768+
..execute('UPDATE items SET col = col + 1')
769+
..execute('DELETE FROM items WHERE col = 2');
770+
expect(db.select('SELECT * FROM items'), isEmpty);
771+
772+
// because this is a local-only table, no crud items should have been
773+
// created.
774+
expect(db.select('SELECT * FROM ps_crud'), isEmpty);
775+
});
776+
});
713777
});
714778
}

0 commit comments

Comments
 (0)