onDbUpgrade method

FutureOr<void> onDbUpgrade(
  1. Database db,
  2. int oldVersion,
  3. int newVersion
)
inherited

Implementation

FutureOr<void> onDbUpgrade(
  Database db,
  int oldVersion,
  int newVersion,
) async {
  // Run the CREATE TABLE statement on the database.
  await db.transaction((txn) async {
    final batch = txn.batch();
    var upgradeQueriesFound = false;
    final allQueries = <String>[];
    for (final element in tables) {
      final queries = _recreateOn(element, oldVersion, newVersion)
          ? element.recreateTable(newVersion)
          : _createOn(element, oldVersion, newVersion)
              ? [element.createTable(newVersion)]
              : _upgradeQueries(element, oldVersion, newVersion);
      if (queries.isNotEmpty == true) {
        allQueries.addAll(queries);
        upgradeQueriesFound = true;
        for (final query in queries) {
          batch.execute(query);
        }
      }
    }
    if (!upgradeQueriesFound && kDebugMode) {
      throw ArgumentError(
        'No Upgrade queries found. If you added new entities, '
        'make sure they are also added to OrmContext.tables',
      );
    }
    await batch.commit(noResult: true);
    _logBatchResult(
      'onDbUpgrade',
      allQueries,
      'Database upgraded from $oldVersion to $newVersion',
    );
  });
  await db.transaction((txn) async {
    final batch = txn.batch();
    final allQueries = <String>[];
    for (final element in tables) {
      final queries = element.onUpgradeComplete(oldVersion, newVersion);
      if (queries.isNotEmpty == true) {
        allQueries.addAll(queries);
        for (final query in queries) {
          batch.execute(query);
        }
      } else {}
    }
    await batch.commit(noResult: true);
    _logBatchResult('After onDbUpgrade', allQueries, null);
  });
}