mirror of https://github.com/AMT-Cheif/drift.git
Remove limitations of a beforeOpen callback (#216)
This commit is contained in:
parent
85426a7bf4
commit
ed4d69a792
|
@ -1,6 +1,7 @@
|
||||||
## unreleased
|
## unreleased
|
||||||
|
|
||||||
- Fix crash when `customStatement` is the first operation used on a database ([#199](https://github.com/simolus3/moor/issues/199))
|
- 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
|
## 2.0.1
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ import 'dart:async';
|
||||||
|
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moor/moor.dart';
|
import 'package:moor/moor.dart';
|
||||||
import 'package:moor/src/runtime/executor/before_open.dart';
|
|
||||||
import 'package:moor/src/runtime/executor/stream_queries.dart';
|
import 'package:moor/src/runtime/executor/stream_queries.dart';
|
||||||
|
|
||||||
const _zoneRootUserKey = #DatabaseConnectionUser;
|
const _zoneRootUserKey = #DatabaseConnectionUser;
|
||||||
|
@ -475,15 +474,13 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser
|
||||||
/// is used internally by database implementations and should not be called by
|
/// is used internally by database implementations and should not be called by
|
||||||
/// users.
|
/// users.
|
||||||
Future<void> beforeOpenCallback(
|
Future<void> beforeOpenCallback(
|
||||||
QueryExecutor executor, OpeningDetails details) async {
|
QueryExecutor executor, OpeningDetails details) {
|
||||||
final migration = _resolvedMigration;
|
final migration = _resolvedMigration;
|
||||||
|
|
||||||
if (migration.beforeOpen != null) {
|
if (migration.beforeOpen != null) {
|
||||||
final engine = BeforeOpenEngine(this, executor);
|
return migration.beforeOpen(details);
|
||||||
await _runEngineZoned(engine, () {
|
|
||||||
return migration.beforeOpen(details);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
return Future.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Closes this database and releases associated resources.
|
/// Closes this database and releases associated resources.
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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
|
/// A database engine (implements [QueryExecutor]) that delegated the relevant
|
||||||
/// work to a [DatabaseDelegate].
|
/// work to a [DatabaseDelegate].
|
||||||
class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
|
class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
|
||||||
|
@ -301,6 +274,6 @@ class DelegatedDatabase extends QueryExecutor with _ExecutorWithQueryDelegate {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _runBeforeOpen(OpeningDetails d) {
|
Future<void> _runBeforeOpen(OpeningDetails d) {
|
||||||
return databaseInfo.beforeOpenCallback(_BeforeOpeningExecutor(this), d);
|
return databaseInfo.beforeOpenCallback(this, d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue