diff --git a/PortfolioJournal/Models/CoreDataStack.swift b/PortfolioJournal/Models/CoreDataStack.swift index cc77ffe..a9749c9 100644 --- a/PortfolioJournal/Models/CoreDataStack.swift +++ b/PortfolioJournal/Models/CoreDataStack.swift @@ -192,6 +192,24 @@ class CoreDataStack: ObservableObject { print("Core Data failed to load: \(error), \(error.userInfo)") } else { print("Core Data loaded successfully at: \(description.url?.path ?? "unknown")") + #if DEBUG + // Dev-only, opt-in: push the FULL model schema to the Development CloudKit + // environment so optional fields that were never exported by a Debug run + // (e.g. monthlyContribution on InvestmentSource, allocationTarget on Category) + // get materialised — otherwise they can't be deployed to Production and every + // Production export rejects those records (bare CKErrorPartialFailure). + // Run ONCE from Xcode with launch arg -InitializeCloudKitSchema, watch for the + // ✅ log, then deploy to Production in the CloudKit Console and remove the arg. + if ProcessInfo.processInfo.arguments.contains("-InitializeCloudKitSchema"), + let ckContainer = container as? NSPersistentCloudKitContainer { + do { + try ckContainer.initializeCloudKitSchema(options: []) + print("✅ CloudKit schema initialized in Development. Now open the CloudKit Console → Deploy Schema Changes → Production.") + } catch { + print("❌ initializeCloudKitSchema failed: \(error)") + } + } + #endif } DispatchQueue.main.async { self?.isLoaded = true