destructiveFallback property Null safety

MigrationStrategy destructiveFallback

Provides a destructive MigrationStrategy that will delete and then re-create all tables, triggers and indices.

To use this behavior, override the migration getter in your database:

@DriftDatabase(...)
class MyDatabase extends _$MyDatabase {
  @override
  MigrationStrategy get migration => destructiveFallback;
}

Implementation

MigrationStrategy get destructiveFallback {
  return MigrationStrategy(
    onCreate: _defaultOnCreate,
    onUpgrade: (m, from, to) async {
      // allSchemaEntities are sorted topologically references between them.
      // Reverse order for deletion in order to not break anything.
      final reversedEntities = m._db.allSchemaEntities.toList().reversed;

      for (final entity in reversedEntities) {
        await m.drop(entity);
      }

      // Re-create them now
      await m.createAll();
    },
  );
}