Remove limitations of a beforeOpen callback (#216)

This commit is contained in:
Simon Binder 2019-10-31 18:41:15 +01:00
parent 85426a7bf4
commit ed4d69a792
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 5 additions and 67 deletions

View File

@ -1,6 +1,7 @@
## unreleased
- Fix crash when `customStatement` is the first operation used on a database ([#199](https://github.com/simolus3/moor/issues/199))
- Allow transactions inside a `beforeOpen` callback
## 2.0.1

View File

@ -2,7 +2,6 @@ import 'dart:async';
import 'package:meta/meta.dart';
import 'package:moor/moor.dart';
import 'package:moor/src/runtime/executor/before_open.dart';
import 'package:moor/src/runtime/executor/stream_queries.dart';
const _zoneRootUserKey = #DatabaseConnectionUser;
@ -475,15 +474,13 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser
/// is used internally by database implementations and should not be called by
/// users.
Future<void> beforeOpenCallback(
QueryExecutor executor, OpeningDetails details) async {
QueryExecutor executor, OpeningDetails details) {
final migration = _resolvedMigration;
if (migration.beforeOpen != null) {
final engine = BeforeOpenEngine(this, executor);
await _runEngineZoned(engine, () {
return migration.beforeOpen(details);
});
return migration.beforeOpen(details);
}
return Future.value();
}
/// Closes this database and releases associated resources.

View File

@ -1,33 +0,0 @@
import 'package:meta/meta.dart';
import 'package:moor/moor.dart';
import 'package:moor/src/runtime/executor/stream_queries.dart';
/// Used internally by moor.
class BeforeOpenEngine extends DatabaseConnectionUser with QueryEngine {
/// Used internally by moor.
BeforeOpenEngine(DatabaseConnectionUser other, QueryExecutor executor)
: super.delegate(
other,
executor: executor,
streamQueries: _IgnoreStreamQueries(),
);
@override
@alwaysThrows
Future<T> transaction<T>(Function action) {
throw UnsupportedError("Transactions can't be started inside beforeOpen");
}
}
class _IgnoreStreamQueries extends StreamQueryStore {
@override
Stream<T> registerStream<T>(QueryStreamFetcher<T> statement) {
throw StateError('Streams cannot be created inside a transaction. See the '
'documentation of GeneratedDatabase.transaction for details.');
}
@override
Future handleTableUpdates(Set<TableInfo> tables) {
return Future.value(null);
}
}

View File

@ -189,33 +189,6 @@ class _TransactionExecutor extends TransactionExecutor
}
}
class _BeforeOpeningExecutor extends QueryExecutor
with _ExecutorWithQueryDelegate {
final DelegatedDatabase db;
@override
QueryDelegate get impl => db.delegate;
@override
bool get isSequential => db.isSequential;
@override
bool get logStatements => db.logStatements;
_BeforeOpeningExecutor(this.db);
@override
TransactionExecutor beginTransaction() {
throw Exception(
"Transactions can't be started in the before open callback");
}
@override
Future<bool> ensureOpen() {
return Future.value(true);
}
}
/// A database engine (implements [QueryExecutor]) that delegated the relevant
/// work to a [DatabaseDelegate].
class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
@ -301,6 +274,6 @@ class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
}
Future<void> _runBeforeOpen(OpeningDetails d) {
return databaseInfo.beforeOpenCallback(_BeforeOpeningExecutor(this), d);
return databaseInfo.beforeOpenCallback(this, d);
}
}