@@ -3,6 +3,7 @@ import GRDB
33
44public protocol GRDBManagerProtocol {
55 var databaseConnection : GRDBDatabaseConnection { get }
6+ func reset( ) throws
67}
78
89public protocol GRDBDatabaseConnection : DatabaseReader & DatabaseWriter { }
@@ -27,6 +28,31 @@ public final class GRDBManager: GRDBManagerProtocol {
2728 self . databaseConnection = try DatabaseQueue ( )
2829 try migrateIfNeeded ( )
2930 }
31+
32+ /// Resets the database by deleting all data from all tables
33+ /// Used when user logs out to ensure no data leaks between sessions
34+ public func reset( ) throws {
35+ try databaseConnection. write { db in
36+ // Disable foreign key constraints temporarily to avoid dependency issues
37+ try db. execute ( sql: " PRAGMA foreign_keys = OFF " )
38+
39+ // Get all user tables (excluding sqlite internal tables)
40+ let tableNames = try String . fetchAll ( db, sql: """
41+ SELECT name FROM sqlite_master
42+ WHERE type = 'table'
43+ AND name NOT LIKE 'sqlite_%'
44+ AND name NOT LIKE 'grdb_%'
45+ """ )
46+
47+ // Delete all data from each table
48+ for tableName in tableNames {
49+ try db. execute ( sql: " DELETE FROM \( tableName) " )
50+ }
51+
52+ // Re-enable foreign key constraints
53+ try db. execute ( sql: " PRAGMA foreign_keys = ON " )
54+ }
55+ }
3056}
3157
3258private extension GRDBManager {
0 commit comments