mirror of https://github.com/AMT-Cheif/drift.git
API to close databases, integration test that saves data
This commit is contained in:
parent
7327e1f8b1
commit
4bf58cb83b
|
@ -121,6 +121,10 @@ class Database extends _$Database {
|
|||
return (select(users)..where((u) => u.id.equals(id))).watchSingle();
|
||||
}
|
||||
|
||||
Future<int> writeUser(Insertable<User> user) {
|
||||
return into(users).insert(user);
|
||||
}
|
||||
|
||||
Future<void> makeFriends(User a, User b, {bool goodFriends}) async {
|
||||
var friendsValue = const Value<bool>.absent();
|
||||
if (goodFriends != null) {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -331,4 +331,9 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes this database and releases associated resources.
|
||||
Future<void> close() async {
|
||||
await executor.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<void> close() async {
|
||||
// no-op per default for backwards compatibility
|
||||
}
|
||||
}
|
||||
|
||||
/// A statement that should be executed in a batch. Used internally by moor.
|
||||
|
|
|
@ -101,6 +101,10 @@ class SqlJsDatabase {
|
|||
Uint8List export() {
|
||||
return _obj.callMethod('export') as Uint8List;
|
||||
}
|
||||
|
||||
void close() {
|
||||
_obj.callMethod('close');
|
||||
}
|
||||
}
|
||||
|
||||
class PreparedStatement {
|
||||
|
|
|
@ -87,6 +87,13 @@ class _WebDelegate extends DatabaseDelegate {
|
|||
return _handlePotentialUpdate();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> 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<int> _handlePotentialUpdate() {
|
||||
|
|
Loading…
Reference in New Issue