From bbf12ff5ec5221cb22e3cfcdcb84025d2c766d2e Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 30 Mar 2019 14:56:55 +0100 Subject: [PATCH] Ensure the database is open before executing transactions --- moor/lib/src/runtime/database.dart | 14 ++++++++------ moor/test/transactions_test.dart | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/moor/lib/src/runtime/database.dart b/moor/lib/src/runtime/database.dart index f97d2339..3f25ad5d 100644 --- a/moor/lib/src/runtime/database.dart +++ b/moor/lib/src/runtime/database.dart @@ -157,13 +157,15 @@ mixin QueryEngine on DatabaseConnectionUser { /// queries and updates must be sent to the [QueryEngine] passed to the /// [action] function. Future transaction(Future Function(QueryEngine transaction) action) async { - final transaction = Transaction(this, executor.beginTransaction()); + await executor.doWhenOpened((executor) async { + final transaction = Transaction(this, executor.beginTransaction()); - try { - await action(transaction); - } finally { - await transaction.complete(); - } + try { + await action(transaction); + } finally { + await transaction.complete(); + } + }); } } diff --git a/moor/test/transactions_test.dart b/moor/test/transactions_test.dart index b9f221fd..b62a2796 100644 --- a/moor/test/transactions_test.dart +++ b/moor/test/transactions_test.dart @@ -50,4 +50,10 @@ void main() { verify(streamQueries.handleTableUpdates({db.users})).called(1); verify(executor.transactions.send()); }); + + test('the database is opened before starting a transaction', () async { + await db.transaction((t) async { + verify(executor.doWhenOpened(any)); + }); + }); }