From 4bf58cb83bac695a62d5fb74efecb6185a18297c Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 25 Jul 2019 17:04:32 +0200 Subject: [PATCH] API to close databases, integration test that saves data --- .../tests/lib/database/database.dart | 4 ++++ .../tests/lib/suite/migrations.dart | 13 +++++++++++++ moor/lib/src/runtime/database.dart | 5 +++++ moor/lib/src/runtime/executor/executor.dart | 7 +++++++ moor/lib/src/web/sql_js.dart | 4 ++++ moor/lib/src/web/web_db.dart | 7 +++++++ 6 files changed, 40 insertions(+) diff --git a/extras/integration_tests/tests/lib/database/database.dart b/extras/integration_tests/tests/lib/database/database.dart index 2665d670..f98e32ff 100644 --- a/extras/integration_tests/tests/lib/database/database.dart +++ b/extras/integration_tests/tests/lib/database/database.dart @@ -121,6 +121,10 @@ class Database extends _$Database { return (select(users)..where((u) => u.id.equals(id))).watchSingle(); } + Future writeUser(Insertable user) { + return into(users).insert(user); + } + Future makeFriends(User a, User b, {bool goodFriends}) async { var friendsValue = const Value.absent(); if (goodFriends != null) { diff --git a/extras/integration_tests/tests/lib/suite/migrations.dart b/extras/integration_tests/tests/lib/suite/migrations.dart index a5db4529..778ce4f8 100644 --- a/extras/integration_tests/tests/lib/suite/migrations.dart +++ b/extras/integration_tests/tests/lib/suite/migrations.dart @@ -1,4 +1,5 @@ import 'package:test/test.dart'; +import 'package:tests/data/sample_data.dart'; import 'package:tests/database/database.dart'; import 'suite.dart'; @@ -11,4 +12,16 @@ void migrationTests(TestExecutor executor) { final count = await database.userCount(); expect(count.single.cOUNTid, 3); }); + + test('saves and restores database', () async { + var database = Database(executor.createExecutor(), schemaVersion: 1); + await database.writeUser(People.florian); + await database.close(); + + database = Database(executor.createExecutor(), schemaVersion: 2); + + // the 3 initial users plus People.florian + final count = await database.userCount(); + expect(count.single.cOUNTid, 4); + }); } diff --git a/moor/lib/src/runtime/database.dart b/moor/lib/src/runtime/database.dart index dc14d3d9..bfb60851 100644 --- a/moor/lib/src/runtime/database.dart +++ b/moor/lib/src/runtime/database.dart @@ -331,4 +331,9 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser }); } } + + /// Closes this database and releases associated resources. + Future close() async { + await executor.close(); + } } diff --git a/moor/lib/src/runtime/executor/executor.dart b/moor/lib/src/runtime/executor/executor.dart index 4669a521..7191fe94 100644 --- a/moor/lib/src/runtime/executor/executor.dart +++ b/moor/lib/src/runtime/executor/executor.dart @@ -48,6 +48,13 @@ abstract class QueryExecutor { /// Starts a [TransactionExecutor]. TransactionExecutor beginTransaction(); + + /// Closes this database connection and releases all resources associated with + /// it. Implementations should also handle [close] calls in a state where the + /// database isn't open. + Future close() async { + // no-op per default for backwards compatibility + } } /// A statement that should be executed in a batch. Used internally by moor. diff --git a/moor/lib/src/web/sql_js.dart b/moor/lib/src/web/sql_js.dart index 719d635c..7f398e94 100644 --- a/moor/lib/src/web/sql_js.dart +++ b/moor/lib/src/web/sql_js.dart @@ -101,6 +101,10 @@ class SqlJsDatabase { Uint8List export() { return _obj.callMethod('export') as Uint8List; } + + void close() { + _obj.callMethod('close'); + } } class PreparedStatement { diff --git a/moor/lib/src/web/web_db.dart b/moor/lib/src/web/web_db.dart index 3be9d216..6b0b6631 100644 --- a/moor/lib/src/web/web_db.dart +++ b/moor/lib/src/web/web_db.dart @@ -87,6 +87,13 @@ class _WebDelegate extends DatabaseDelegate { return _handlePotentialUpdate(); } + @override + Future close() { + _storeDb(); + _db?.close(); + return Future.value(); + } + /// Saves the database if the last statement changed rows. As a side-effect, /// saving the database resets the `last_insert_id` counter in sqlite. Future _handlePotentialUpdate() {