Skip to content

Commit ef27ce5

Browse files
committed
fix(persistence): disable foreign key constraints during flush
This commit modifies the `flush` method in `DriftChatDatabase` to temporarily disable foreign key constraints using `PRAGMA foreign_keys = OFF` before deleting all table data. This prevents foreign key constraint violations that could occur when deleting tables in an order that doesn't respect dependencies. The constraints are re-enabled using `PRAGMA foreign_keys = ON` in a `finally` block to ensure they are restored even if an error occurs during the deletion process.
1 parent 46d07dc commit ef27ce5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/stream_chat_persistence/lib/src/db/drift_chat_database.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ class DriftChatDatabase extends _$DriftChatDatabase {
7373
);
7474

7575
/// Deletes all the tables
76-
Future<void> flush() => batch((batch) {
77-
allTables.forEach((table) {
78-
delete(table).go();
79-
});
76+
Future<void> flush() async {
77+
await customStatement('PRAGMA foreign_keys = OFF');
78+
try {
79+
await transaction(() async {
80+
for (final table in allTables) {
81+
await delete(table).go();
82+
}
8083
});
84+
} finally {
85+
await customStatement('PRAGMA foreign_keys = ON');
86+
}
87+
}
8188

8289
/// Closes the database instance
8390
Future<void> disconnect() => close();

0 commit comments

Comments
 (0)