Showing 1 changed files with 13 additions and 3 deletions
+13 -3
HealthProbe/HealthProbeApp.swift
@@ -22,14 +22,16 @@ struct HealthProbeApp: App {
22 22
         .modelContainer(sharedModelContainer)
23 23
     }
24 24
 
25
-    // Removes legacy stores to avoid schema incompatibility.
26
-    // Clears all previous database files that may have incompatible models.
25
+    // Removes all SwiftData stores to ensure clean schema.
26
+    // Called on every app launch to prevent schema corruption from old models.
27 27
     private static func destroyLegacyStoreIfNeeded() {
28 28
         let appSupportURL = URL.applicationSupportDirectory
29 29
         let legacyFiles = [
30 30
             "HealthProbe.store",
31 31
             "HealthProbeCloud.store",
32 32
             "HealthProbeLocal.store",
33
+            "HealthProbe.store-journal", // SQLite journal
34
+            "default.store",
33 35
         ]
34 36
         for fileName in legacyFiles {
35 37
             let storeURL = appSupportURL.appending(path: fileName)
@@ -37,8 +39,16 @@ struct HealthProbeApp: App {
37 39
                 storeURL,
38 40
                 storeURL.appendingPathExtension("shm"),
39 41
                 storeURL.appendingPathExtension("wal"),
42
+                storeURL.appendingPathExtension("journal"),
40 43
             ]
41
-            for url in candidates { try? FileManager.default.removeItem(at: url) }
44
+            for url in candidates {
45
+                try? FileManager.default.removeItem(at: url)
46
+            }
47
+        }
48
+        // Also try to remove the entire Application Support directory metadata
49
+        if let metadataURL = try? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
50
+            let storeDescriptionURL = metadataURL.appending(path: ".com.apple.mobile_container_manager.metadata.plist")
51
+            try? FileManager.default.removeItem(at: storeDescriptionURL)
42 52
         }
43 53
     }
44 54