```
docs(health-profile): 添加防编造加固修订记录到导出健康档案设计文档 补充了关于导出摘要出现虚构病例问题的详细分析和修复方案, 包括检索策略优化、空数据兜底处理和prompt重写等三层防护措施。 ```
This commit is contained in:
@@ -23,23 +23,46 @@ struct KangkangApp: App {
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [config])
|
||||
} catch {
|
||||
// Demo 阶段 schema 仍在演进:旧 store 与新 schema 不兼容且无正式迁移时,
|
||||
// 自动迁移会失败导致启动崩溃。这里重置本地 store 重建(测试数据可丢)。
|
||||
// ⚠️ 生产环境必须改为正式的 SwiftData 迁移方案,不能静默删数据。
|
||||
print("⚠️ ModelContainer 创建失败,重置本地 store 重建: \(error)")
|
||||
let fm = FileManager.default
|
||||
let storePath = config.url.path
|
||||
for path in [storePath, storePath + "-wal", storePath + "-shm"] {
|
||||
try? fm.removeItem(atPath: path)
|
||||
}
|
||||
// Demo 阶段 schema 仍在演进:某次改动若超出 SwiftData 自动轻量迁移能力
|
||||
// (最常见:给已存在的 @Model 新增「非可选且无内联默认值」的属性),自动迁移会抛错。
|
||||
// 这里不再静默删库,而是把旧 store 连同 -wal/-shm 整体挪到带时间戳的备份目录后重建——
|
||||
// 既保证 App 能启动,又让旧数据可手动恢复(挪不动才降级为删除)。
|
||||
// ⚠️ 正式发布前仍应改为 VersionedSchema + SchemaMigrationPlan 的正式迁移。
|
||||
// 注:新增 @Model 属性请一律给「可选」或「内联默认值」,即可走轻量迁移、不触发本兜底。
|
||||
print("⚠️ ModelContainer 创建失败,备份旧 store 后重建: \(error)")
|
||||
KangkangApp.backupIncompatibleStore(at: config.url)
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [config])
|
||||
} catch {
|
||||
fatalError("Could not create ModelContainer even after reset: \(error)")
|
||||
fatalError("Could not create ModelContainer even after store reset: \(error)")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
/// 把与新 schema 不兼容的旧 store(含 `-wal` / `-shm`)挪到
|
||||
/// `Application Support/StoreBackups/<时间戳>/`,而不是直接删除。
|
||||
/// 既清出路径让新库能建起来,又把旧数据留作可手动恢复的备份;挪不动时才降级为删除。
|
||||
private static func backupIncompatibleStore(at storeURL: URL) {
|
||||
let fm = FileManager.default
|
||||
let fmt = DateFormatter()
|
||||
fmt.locale = Locale(identifier: "en_US_POSIX")
|
||||
fmt.dateFormat = "yyyyMMdd-HHmmss"
|
||||
let stamp = fmt.string(from: Date())
|
||||
let backupDir = storeURL.deletingLastPathComponent()
|
||||
.appendingPathComponent("StoreBackups/\(stamp)", isDirectory: true)
|
||||
try? fm.createDirectory(at: backupDir, withIntermediateDirectories: true)
|
||||
for suffix in ["", "-wal", "-shm"] {
|
||||
let src = URL(fileURLWithPath: storeURL.path + suffix)
|
||||
guard fm.fileExists(atPath: src.path) else { continue }
|
||||
let dst = backupDir.appendingPathComponent(src.lastPathComponent)
|
||||
do {
|
||||
try fm.moveItem(at: src, to: dst)
|
||||
} catch {
|
||||
try? fm.removeItem(at: src) // 挪不动就删,至少保证能启动
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
AppLockContainer {
|
||||
|
||||
Reference in New Issue
Block a user