Skip to content

Commit

Permalink
Call doWhenOpened for customStatement (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Oct 20, 2019
1 parent 77fcf7a commit bf94057
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions moor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion moor/lib/src/runtime/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions moor/test/custom_queries_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
4 changes: 4 additions & 0 deletions moor/test/data/utils/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bf94057

Please sign in to comment.