Call doWhenOpened for customStatement (#199)

This commit is contained in:
Simon Binder 2019-10-20 11:39:24 +02:00
parent 77fcf7a7ba
commit bf94057d1b
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 19 additions and 1 deletions

View File

@ -1,3 +1,7 @@
## unreleased
- Fix crash when `customStatement` is the first operation used on a database ([#199](https://github.com/simolus3/moor/issues/199))
## 2.0.1
- Introduced `isBetween` and `isBetweenValues` methods for comparable expressions (int, double, datetime)

View File

@ -270,7 +270,11 @@ mixin QueryEngine on DatabaseConnectionUser {
@protected
@visibleForTesting
Future<void> customStatement(String statement, [List<dynamic> args]) {
return _resolvedEngine.executor.runCustom(statement, args);
final engine = _resolvedEngine;
return engine.executor.doWhenOpened((executor) {
return executor.runCustom(statement, args);
});
}
/// Executes [action] in a transaction, which means that all its queries and

View File

@ -73,4 +73,10 @@ void main() {
// shouldn't call stream queries - we didn't set the updates parameter
verifyNever(streamQueries.handleTableUpdates(any));
});
test('custom statement', () async {
// regression test for https://github.com/simolus3/moor/issues/199 - the
// mock will throw when used before opening
expect(db.customStatement('UPDATE tbl SET a = b'), completes);
});
}

View File

@ -29,6 +29,10 @@ class MockExecutor extends Mock implements QueryExecutor {
assert(_opened);
return Future.value(0);
});
when(runCustom(any, any)).thenAnswer((_) {
assert(_opened);
return Future.value(0);
});
when(beginTransaction()).thenAnswer((_) {
assert(_opened);
return transactions;