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 {
    var batch = txn.batch();
    var upgradeQueriesFound = false;
    List<String> allQueries = [];
    for (var 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 (var query in queries) {
          batch.execute(query);
        }
      }
    }
    if (!upgradeQueriesFound && kDebugMode) {
      throw ArgumentError(
          'No Upgrade queries found. If you added new entities, make sure they are are added in EntitiesDbConfig.tables');
    }
    await batch.commit(noResult: true);
    _logBatchResult('onDbUpgrade', allQueries,
        'Database upgraded from $oldVersion to $newVersion');
  });
  await db.transaction((txn) async {
    var batch = txn.batch();
    List<String> allQueries = [];
    for (var element in tables) {
      final queries = element.onUpgradeComplete(oldVersion, newVersion);
      if (queries.isNotEmpty == true) {
        allQueries.addAll(queries);
        for (var query in queries) {
          batch.execute(query);
        }
      } else {}
    }
    await batch.commit(noResult: true);
    _logBatchResult('After onDbUpgrade', allQueries, null);
  });
}