Skip to content

Commit 5e15029

Browse files
committed
Require transactions
1 parent a444b0f commit 5e15029

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

crates/core/src/schema/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ fn raw_table_migration(
9292
args: &[*mut sqlite::value],
9393
) -> Result<(), PowerSyncError> {
9494
let db = context.db_handle();
95+
if db.get_autocommit() {
96+
return Err(PowerSyncError::argument_error(
97+
"powersync_raw_table_migrate must be called within a transaction",
98+
));
99+
}
95100
let context = unsafe { DatabaseState::from_context(&context) };
96101
let action = RawTableMigration::from_args(args)?;
97102

dart/test/schema_test.dart

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ END''',
335335

336336
setUp(() => db.execute('SELECT powersync_init();'));
337337

338+
test('requires a transaction', () {
339+
expect(
340+
() => db.execute(
341+
'SELECT powersync_raw_table_migrate(?, ?)', ['create', syncName]),
342+
throwsA(isA<SqliteException>()),
343+
);
344+
});
345+
338346
group('create', () {
339347
void createUntypedItem(String id, Object? json) {
340348
db.execute(
@@ -354,8 +362,10 @@ END''',
354362
db.execute(
355363
'CREATE TABLE greetings (id TEXT PRIMARY KEY, hello TEXT) STRICT;');
356364

365+
db.execute('BEGIN');
357366
db.execute(
358367
'SELECT powersync_raw_table_migrate(?, ?)', ['create', syncName]);
368+
db.execute('COMMIT');
359369
expect(db.select('select * from ps_untyped'), isEmpty);
360370
expect(db.select('select * from greetings'), [
361371
{'id': 'foo', 'hello': 'world'},
@@ -383,8 +393,10 @@ END''',
383393
db.execute(
384394
'CREATE TABLE greetings (id TEXT PRIMARY KEY, hello TEXT) STRICT;');
385395

396+
db.execute('BEGIN');
386397
db.execute(
387398
'SELECT powersync_raw_table_migrate(?, ?)', ['create', syncName]);
399+
db.execute('COMMIT');
388400
expect(db.select('select * from ps_untyped'), isEmpty);
389401
expect(db.select('select * from greetings'), [
390402
{'id': 'foo', 'hello': 'world'},
@@ -405,14 +417,16 @@ END''',
405417
'INSERT INTO greetings (id, hello, local) VALUES (?, ?, uuid())',
406418
['id_1', 'second'],
407419
)
420+
..execute('BEGIN')
408421
..execute('SELECT powersync_raw_table_migrate(?, ?)', [
409422
'drop',
410423
json.encode({
411424
'name': syncName,
412425
'table_name': 'greetings',
413426
'synced_columns': ['hello']
414427
})
415-
]);
428+
])
429+
..execute('COMMIT');
416430

417431
expect(db.select('select * from ps_untyped'), [
418432
{'type': 'synced_table', 'id': 'id_0', 'data': '{"hello":"first"}'},
@@ -425,8 +439,8 @@ END''',
425439
json.encode({'name': syncName, 'table_name': 'greetings'});
426440

427441
db.execute('SELECT powersync_replace_schema(?)', [
428-
json.encode(
429-
singleRawTableSchema({'name': syncName, 'table_name': 'greetings'})),
442+
json.encode(singleRawTableSchema(
443+
{'name': syncName, 'table_name': 'greetings'})),
430444
]);
431445
db.execute(
432446
'CREATE TABLE greetings (id TEXT PRIMARY KEY, hello TEXT) STRICT;');
@@ -439,12 +453,21 @@ SELECT
439453
''', [tableJson]);
440454

441455
db
442-
..execute('INSERT INTO ps_untyped (type, id, data) VALUES (?, ?, ?)',
443-
[syncName, 'id_1', json.encode({'hello': 'world'})])
444-
..execute('INSERT INTO ps_untyped (type, id, data) VALUES (?, ?, ?)',
445-
[syncName, 'id_2', json.encode({'hello': 'again'})]);
456+
..execute(
457+
'INSERT INTO ps_untyped (type, id, data) VALUES (?, ?, ?)', [
458+
syncName,
459+
'id_1',
460+
json.encode({'hello': 'world'})
461+
])
462+
..execute(
463+
'INSERT INTO ps_untyped (type, id, data) VALUES (?, ?, ?)', [
464+
syncName,
465+
'id_2',
466+
json.encode({'hello': 'again'})
467+
]);
446468

447469
// create migration: ps_untyped -> raw table; triggers must not fire
470+
db.execute('BEGIN');
448471
db.execute(
449472
'SELECT powersync_raw_table_migrate(?, ?)', ['create', syncName]);
450473
expect(db.select('SELECT * FROM ps_crud'), isEmpty);
@@ -454,6 +477,7 @@ SELECT
454477
'drop',
455478
json.encode({'name': syncName, 'table_name': 'greetings'}),
456479
]);
480+
db.execute('COMMIT');
457481
expect(db.select('SELECT * FROM ps_crud'), isEmpty);
458482
});
459483
});

0 commit comments

Comments
 (0)