beforeOpen method

  1. @override
  2. @nonVirtual
Future<void> beforeOpen(
  1. QueryExecutor executor,
  2. OpeningDetails details
)
override

A callbacks that runs after the database connection has been established, but before any other query is sent.

The query executor will wait for this future to complete before running any other query. Queries running on the executor are an exception to this, they can be used to run migrations. No matter how often QueryExecutor.ensureOpen is called, this method will not be called more than once.

Implementation

@override
@nonVirtual
Future<void> beforeOpen(QueryExecutor executor, OpeningDetails details) {
  return _runConnectionZoned(BeforeOpenRunner(this, executor), () async {
    if (schemaVersion <= 0) {
      throw StateError(
        'The schemaVersion of your database must be positive. \n'
        "A value of zero can't be distinguished from an uninitialized "
        'database, which causes issues in the migrator',
      );
    }

    if (details.wasCreated) {
      final migrator = createMigrator();
      await _resolvedMigration.onCreate(migrator);
    } else if (details.hadUpgrade) {
      final migrator = createMigrator();
      await _resolvedMigration.onUpgrade(
          migrator, details.versionBefore!, details.versionNow);
    }

    await _resolvedMigration.beforeOpen?.call(details);
  });
}