clearAllTables method

Future<void> clearAllTables()

Deletes all rows of all entity tables while keeping the database schema, like Room's clearAllTables.

The counters of AUTOINCREMENT primary keys are reset as well.

Implementation

Future<void> clearAllTables() async {
  Future<void> clear(final sqflite.DatabaseExecutor executor) async {
    // foreign keys are only checked once the transaction gets committed
    await executor.execute('PRAGMA defer_foreign_keys = TRUE');
    for (final tableName in entityTableNames) {
      await executor.execute('DELETE FROM `$tableName`');
    }

    final sequenceTable = await executor.rawQuery(
      'SELECT 1 FROM sqlite_master WHERE type = ? AND name = ?',
      ['table', 'sqlite_sequence'],
    );
    if (sequenceTable.isNotEmpty) {
      await executor.execute('DELETE FROM sqlite_sequence');
    }
  }

  final database = this.database;
  if (database is sqflite.Database) {
    await database.transaction(clear);
  } else {
    await clear(database);
  }

  entityTableNames.forEach(changeListener.notifyChange);
}