Skip to content

Drift: Support nested transactions #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2025
Merged
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
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2025-07-29

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`sqlite_async` - `v0.11.8`](#sqlite_async---v0118)
- [`drift_sqlite_async` - `v0.2.3`](#drift_sqlite_async---v023)

---

#### `sqlite_async` - `v0.11.8`

- Support nested transactions (emulated with `SAVEPOINT` statements).
- Fix web compilation issues with version `2.8.0` of `package:sqlite3`.

#### `drift_sqlite_async` - `v0.2.3`

- Support nested transactions.

## 2025-06-03

---
Expand Down
4 changes: 4 additions & 0 deletions packages/drift_sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3

- Support nested transactions.

## 0.2.2

- Fix write detection when using UPDATE/INSERT/DELETE with RETURNING in raw queries.
Expand Down
17 changes: 16 additions & 1 deletion packages/drift_sqlite_async/lib/src/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,28 @@ class _SqliteAsyncTransactionDelegate extends SupportedTransactionDelegate {

_SqliteAsyncTransactionDelegate(this._db);

@override
FutureOr<void> Function(QueryDelegate, Future<void> Function(QueryDelegate))?
get startNested => _startNested;

@override
Future<void> startTransaction(Future Function(QueryDelegate p1) run) async {
await _db.writeTransaction((context) async {
await _startTransaction(_db, run);
}

Future<void> _startTransaction(
SqliteWriteContext context, Future Function(QueryDelegate p1) run) async {
await context.writeTransaction((context) async {
final delegate = _SqliteAsyncQueryDelegate(context, null);
return run(delegate);
});
}

Future<void> _startNested(
QueryDelegate outer, Future<void> Function(QueryDelegate) block) async {
await _startTransaction(
(outer as _SqliteAsyncQueryDelegate)._context, block);
}
}

class _SqliteAsyncVersionDelegate extends DynamicVersionDelegate {
Expand Down
8 changes: 4 additions & 4 deletions packages/drift_sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: drift_sqlite_async
version: 0.2.2
version: 0.2.3
homepage: https://github.com/powersync-ja/sqlite_async.dart
repository: https://github.com/powersync-ja/sqlite_async.dart
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.
Expand All @@ -14,12 +14,12 @@ topics:
environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
drift: ">=2.19.0 <3.0.0"
sqlite_async: ^0.11.0
drift: ">=2.28.0 <3.0.0"
sqlite_async: ^0.11.8

dev_dependencies:
build_runner: ^2.4.8
drift_dev: ">=2.19.0 <3.0.0"
drift_dev: ">=2.28.0 <3.0.0"
glob: ^2.1.2
lints: ^5.0.0
sqlite3: ^2.4.0
Expand Down
30 changes: 30 additions & 0 deletions packages/drift_sqlite_async/test/db_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,35 @@ void main() {
final deleted = await dbu.delete(dbu.todoItems).go();
expect(deleted, 10);
});

test('nested transactions', () async {
await dbu
.into(dbu.todoItems)
.insert(TodoItemsCompanion.insert(description: 'root'));

await dbu.transaction(() async {
await dbu
.into(dbu.todoItems)
.insert(TodoItemsCompanion.insert(description: 'tx0'));

await dbu.transaction(() async {
await dbu
.into(dbu.todoItems)
.insert(TodoItemsCompanion.insert(description: 'tx1'));

await expectLater(() {
return dbu.transaction(() async {
await dbu
.into(dbu.todoItems)
.insert(TodoItemsCompanion.insert(description: 'tx2'));
throw 'rollback';
});
}, throwsA(anything));
});
});

final items = await dbu.todoItems.all().get();
expect(items.map((e) => e.description).toSet(), {'root', 'tx0', 'tx1'});
});
});
}
5 changes: 5 additions & 0 deletions packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.11.8

- Support nested transactions (emulated with `SAVEPOINT` statements).
- Fix web compilation issues with version `2.8.0` of `package:sqlite3`.

## 0.11.7

- Shared worker: Release locks owned by connected client tab when it closes.
Expand Down
2 changes: 1 addition & 1 deletion packages/sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sqlite_async
description: High-performance asynchronous interface for SQLite on Dart and Flutter.
version: 0.11.7
version: 0.11.8
repository: https://github.com/powersync-ja/sqlite_async.dart
environment:
sdk: ">=3.5.0 <4.0.0"
Expand Down