pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
965 stars 191 forks source link

Feature Request: Add clearAllTables Function #834

Open nawinkhatiwada opened 3 months ago

nawinkhatiwada commented 3 months ago

In some scenarios, it is necessary to clear all data from all tables in the database, including tables that store auto-increment values (sqlite_sequence). Currently, achieving this requires custom SQL queries and manual management.

I would like to request the addition of a clearAllTables function to the Floor library. This function would clear all data from all tables in the database, including thesqlite_sequence table. The function should be transaction-safe to ensure data integrity.

The alternative is to manually write SQL queries to delete data from all tables, but this approach can be error-prone and less convenient for developers.

Here is a sample implementation of how this might be achieved using the sqflite package in Flutter:

Future<void> clearAllTables(Database db) async {
  final tables = await db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");

  await db.transaction((txn) async {
    for (var table in tables) {
      var tableName = table['name'];
      await txn.execute('DELETE FROM $tableName');
    }
  });
}

This function retrieves all table names and executes a DELETE FROM statement for each table inside a transaction.

Having a built-in function in the Floor library would make it easier and more reliable for developers to clear their database.

Thank you for considering this feature request.