Skip to content

Commit 9a3eaac

Browse files
committed
Delete data from all grdb tables on logout
1 parent 581525c commit 9a3eaac

File tree

3 files changed

+225
-54
lines changed

3 files changed

+225
-54
lines changed

Modules/Sources/Storage/GRDB/GRDBManager.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import GRDB
33

44
public protocol GRDBManagerProtocol {
55
var databaseConnection: GRDBDatabaseConnection { get }
6+
func reset() throws
67
}
78

89
public 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

3258
private extension GRDBManager {

0 commit comments

Comments
 (0)