Skip to content

Commit 709a0fe

Browse files
committed
Drift: Support nested transactions
1 parent 8d65840 commit 709a0fe

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

packages/drift_sqlite_async/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.3
2+
3+
- Support nested transactions.
4+
15
## 0.2.2
26

37
- Fix write detection when using UPDATE/INSERT/DELETE with RETURNING in raw queries.

packages/drift_sqlite_async/lib/src/executor.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,28 @@ class _SqliteAsyncTransactionDelegate extends SupportedTransactionDelegate {
129129

130130
_SqliteAsyncTransactionDelegate(this._db);
131131

132+
@override
133+
FutureOr<void> Function(QueryDelegate, Future<void> Function(QueryDelegate))?
134+
get startNested => _startNested;
135+
132136
@override
133137
Future<void> startTransaction(Future Function(QueryDelegate p1) run) async {
134-
await _db.writeTransaction((context) async {
138+
await _startTransaction(_db, run);
139+
}
140+
141+
Future<void> _startTransaction(
142+
SqliteWriteContext context, Future Function(QueryDelegate p1) run) async {
143+
await context.writeTransaction((context) async {
135144
final delegate = _SqliteAsyncQueryDelegate(context, null);
136145
return run(delegate);
137146
});
138147
}
148+
149+
Future<void> _startNested(
150+
QueryDelegate outer, Future<void> Function(QueryDelegate) block) async {
151+
await _startTransaction(
152+
(outer as _SqliteAsyncQueryDelegate)._context, block);
153+
}
139154
}
140155

141156
class _SqliteAsyncVersionDelegate extends DynamicVersionDelegate {

packages/drift_sqlite_async/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: drift_sqlite_async
2-
version: 0.2.2
2+
version: 0.2.3
33
homepage: https://github.com/powersync-ja/sqlite_async.dart
44
repository: https://github.com/powersync-ja/sqlite_async.dart
55
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.
@@ -14,12 +14,12 @@ topics:
1414
environment:
1515
sdk: ">=3.0.0 <4.0.0"
1616
dependencies:
17-
drift: ">=2.19.0 <3.0.0"
17+
drift: ">=2.28.0 <3.0.0"
1818
sqlite_async: ^0.11.0
1919

2020
dev_dependencies:
2121
build_runner: ^2.4.8
22-
drift_dev: ">=2.19.0 <3.0.0"
22+
drift_dev: ">=2.28.0 <3.0.0"
2323
glob: ^2.1.2
2424
lints: ^5.0.0
2525
sqlite3: ^2.4.0

packages/drift_sqlite_async/test/db_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,35 @@ void main() {
117117
final deleted = await dbu.delete(dbu.todoItems).go();
118118
expect(deleted, 10);
119119
});
120+
121+
test('nested transactions', () async {
122+
await dbu
123+
.into(dbu.todoItems)
124+
.insert(TodoItemsCompanion.insert(description: 'root'));
125+
126+
await dbu.transaction(() async {
127+
await dbu
128+
.into(dbu.todoItems)
129+
.insert(TodoItemsCompanion.insert(description: 'tx0'));
130+
131+
await dbu.transaction(() async {
132+
await dbu
133+
.into(dbu.todoItems)
134+
.insert(TodoItemsCompanion.insert(description: 'tx1'));
135+
136+
await expectLater(() {
137+
return dbu.transaction(() async {
138+
await dbu
139+
.into(dbu.todoItems)
140+
.insert(TodoItemsCompanion.insert(description: 'tx2'));
141+
throw 'rollback';
142+
});
143+
}, throwsA(anything));
144+
});
145+
});
146+
147+
final items = await dbu.todoItems.all().get();
148+
expect(items.map((e) => e.description).toSet(), {'root', 'tx0', 'tx1'});
149+
});
120150
});
121151
}

0 commit comments

Comments
 (0)