From 40243bf761ecc0b5268edab6a28408f5bd44b4ff Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 15 Jul 2025 17:42:48 +0200 Subject: [PATCH 1/3] Drift: Support nested transactions --- packages/drift_sqlite_async/CHANGELOG.md | 4 +++ .../drift_sqlite_async/lib/src/executor.dart | 17 ++++++++++- packages/drift_sqlite_async/pubspec.yaml | 6 ++-- packages/drift_sqlite_async/test/db_test.dart | 30 +++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/packages/drift_sqlite_async/CHANGELOG.md b/packages/drift_sqlite_async/CHANGELOG.md index b101ac4..e70cd40 100644 --- a/packages/drift_sqlite_async/CHANGELOG.md +++ b/packages/drift_sqlite_async/CHANGELOG.md @@ -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. diff --git a/packages/drift_sqlite_async/lib/src/executor.dart b/packages/drift_sqlite_async/lib/src/executor.dart index 41886f3..127462e 100644 --- a/packages/drift_sqlite_async/lib/src/executor.dart +++ b/packages/drift_sqlite_async/lib/src/executor.dart @@ -129,13 +129,28 @@ class _SqliteAsyncTransactionDelegate extends SupportedTransactionDelegate { _SqliteAsyncTransactionDelegate(this._db); + @override + FutureOr Function(QueryDelegate, Future Function(QueryDelegate))? + get startNested => _startNested; + @override Future startTransaction(Future Function(QueryDelegate p1) run) async { - await _db.writeTransaction((context) async { + await _startTransaction(_db, run); + } + + Future _startTransaction( + SqliteWriteContext context, Future Function(QueryDelegate p1) run) async { + await context.writeTransaction((context) async { final delegate = _SqliteAsyncQueryDelegate(context, null); return run(delegate); }); } + + Future _startNested( + QueryDelegate outer, Future Function(QueryDelegate) block) async { + await _startTransaction( + (outer as _SqliteAsyncQueryDelegate)._context, block); + } } class _SqliteAsyncVersionDelegate extends DynamicVersionDelegate { diff --git a/packages/drift_sqlite_async/pubspec.yaml b/packages/drift_sqlite_async/pubspec.yaml index 62a64da..aea987f 100644 --- a/packages/drift_sqlite_async/pubspec.yaml +++ b/packages/drift_sqlite_async/pubspec.yaml @@ -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. @@ -14,12 +14,12 @@ topics: environment: sdk: ">=3.0.0 <4.0.0" dependencies: - drift: ">=2.19.0 <3.0.0" + drift: ">=2.28.0 <3.0.0" sqlite_async: ^0.11.0 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 diff --git a/packages/drift_sqlite_async/test/db_test.dart b/packages/drift_sqlite_async/test/db_test.dart index bda09df..f5b269a 100644 --- a/packages/drift_sqlite_async/test/db_test.dart +++ b/packages/drift_sqlite_async/test/db_test.dart @@ -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'}); + }); }); } From 1f720565467cd16400c4c97e871d70947fb1ea33 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 15 Jul 2025 17:47:12 +0200 Subject: [PATCH 2/3] Prepare for release --- packages/drift_sqlite_async/pubspec.yaml | 2 +- packages/sqlite_async/CHANGELOG.md | 4 ++++ packages/sqlite_async/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/drift_sqlite_async/pubspec.yaml b/packages/drift_sqlite_async/pubspec.yaml index aea987f..77f250d 100644 --- a/packages/drift_sqlite_async/pubspec.yaml +++ b/packages/drift_sqlite_async/pubspec.yaml @@ -15,7 +15,7 @@ environment: sdk: ">=3.0.0 <4.0.0" dependencies: drift: ">=2.28.0 <3.0.0" - sqlite_async: ^0.11.0 + sqlite_async: ^0.11.8 dev_dependencies: build_runner: ^2.4.8 diff --git a/packages/sqlite_async/CHANGELOG.md b/packages/sqlite_async/CHANGELOG.md index 97dbf50..eedfb93 100644 --- a/packages/sqlite_async/CHANGELOG.md +++ b/packages/sqlite_async/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.11.8 + +- Support nested transactions (emulated with `SAVEPOINT` statements). + ## 0.11.7 - Shared worker: Release locks owned by connected client tab when it closes. diff --git a/packages/sqlite_async/pubspec.yaml b/packages/sqlite_async/pubspec.yaml index e610d7f..a155f2b 100644 --- a/packages/sqlite_async/pubspec.yaml +++ b/packages/sqlite_async/pubspec.yaml @@ -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" From 17012edf88a498e0c04255dbb4e0f6a2d054c299 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 29 Jul 2025 17:29:02 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 24 ++++++++++++++++++++++++ packages/sqlite_async/CHANGELOG.md | 1 + 2 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9cda07..cafde71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 --- diff --git a/packages/sqlite_async/CHANGELOG.md b/packages/sqlite_async/CHANGELOG.md index eedfb93..387c944 100644 --- a/packages/sqlite_async/CHANGELOG.md +++ b/packages/sqlite_async/CHANGELOG.md @@ -1,6 +1,7 @@ ## 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