From 3b7dd937aa785c14f12879cfd18f1e49a438c1a5 Mon Sep 17 00:00:00 2001 From: mario-luis-dev Date: Wed, 4 Nov 2026 12:29:36 +0100 Subject: [PATCH] Don't delete a child whose parent was never uploaded When a save fails with a reference violation, the sync engine applies the child's 'ON DELETE' action, as if the parent had been deleted on another device. A parent whose own save failed, for example with 'quotaExceeded', is not on the server until it is retried, so a child saved under it in the meantime was deleted. Apply the 'ON DELETE' action only when the server has confirmed the parent before (its last known server record has a change tag). Otherwise queue the parent and the child again. Co-Authored-By: Claude Opus 5.5 --- .../CloudKit/CloudKit+StructuredQueries.swift | 6 ++ Sources/SQLiteData/CloudKit/SyncEngine.swift | 30 +++++++ .../ReferenceViolationTests.swift | 80 +++++++++++++++++++ 3 files changed, 116 insertions(+) diff --git a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift index d2f0726e0..2d53b4f66 100644 --- a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift +++ b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift @@ -379,6 +379,12 @@ get { self[#function] } set { self[#function] = newValue } } + + /// Whether this record came back from the server: only saved records carry a change tag. + /// (The mock database stores its change tag in `_recordChangeTag`.) + package var hasServerChangeTag: Bool { + recordChangeTag != nil || _recordChangeTag != nil + } } extension DataProtocol { diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index e1dc5d1aa..a5916aefd 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -1701,6 +1701,19 @@ await clearServerRecord() case .referenceViolation: + // NB: The server does not have this record's parent. That only means the parent was + // deleted if the server ever confirmed it. A parent the server never confirmed was + // never uploaded (for example, its save failed with an error that is not retried), + // so applying the 'ON DELETE' action here would delete the child and every record + // that cascades from it. Queue both again instead: parents sort before their + // children, so the parent is sent first. + if let parentRecordID = failedRecord.parent?.recordID, + await isAwaitingFirstUpload(parentRecordID) + { + newPendingRecordZoneChanges.append(.saveRecord(parentRecordID)) + newPendingRecordZoneChanges.append(.saveRecord(failedRecord.recordID)) + continue + } guard let recordPrimaryKey = failedRecord.recordID.recordPrimaryKey, let table = tablesByName[failedRecord.recordType], @@ -2048,6 +2061,23 @@ } } + /// Whether a record exists locally but the server has never confirmed a save of it. + /// + /// Only records returned by the server carry a change tag. A last known server record without + /// one was built locally for a send that never succeeded. + private func isAwaitingFirstUpload(_ recordID: CKRecord.ID) async -> Bool { + await withErrorReporting(.sqliteDataCloudKitFailure) { + try await metadatabase.read { db in + guard + let metadata = try SyncMetadata.find(recordID).fetchOne(db), + !metadata._isDeleted + else { return false } + return !(metadata.lastKnownServerRecord?.hasServerChangeTag ?? false) + } + } + ?? false + } + private func refreshLastKnownServerRecord(_ record: CKRecord) async { await withErrorReporting(.sqliteDataCloudKitFailure) { try await userDatabase.write { db in diff --git a/Tests/SQLiteDataTests/CloudKitTests/ReferenceViolationTests.swift b/Tests/SQLiteDataTests/CloudKitTests/ReferenceViolationTests.swift index eaabf060a..39eda7c1e 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/ReferenceViolationTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/ReferenceViolationTests.swift @@ -7,10 +7,90 @@ import SQLiteDataTestSupport import SnapshotTestingCustomDump import Testing + import TestLocals extension BaseCloudKitTests { @MainActor final class ReferenceViolationTests: BaseCloudKitTests, @unchecked Sendable { + // * The user's iCloud storage is full. + // * Local client creates a list and a reminder in it. Both saves fail with + // 'quotaExceeded' and wait until a later save succeeds. + // * The user frees up space, then adds another reminder to the list. + // => The new reminder is kept, and the list and both reminders are uploaded. + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.taskLocal($quota, 0)) + func addReminderAfterListFailedToSaveWithQuotaExceeded() async throws { + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + Reminder(id: 1, title: "Get milk", remindersListID: 1) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + syncEngine.private.database.state.withValue { $0.quota = .max } + try await userDatabase.userWrite { db in + try Reminder.insert { Reminder(id: 2, title: "Get bread", remindersListID: 1) } + .execute(db) + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let reminders = try await userDatabase.read { db in + try Reminder.order(by: \.id).fetchAll(db) + } + #expect( + reminders == [ + Reminder(id: 1, title: "Get milk", remindersListID: 1), + Reminder(id: 2, title: "Get bread", remindersListID: 1), + ] + ) + assertInlineSnapshot(of: container, as: .customDump) { + """ + MockCloudContainer( + privateCloudDatabase: MockCloudDatabase( + databaseScope: .private, + storage: [ + [0]: CKRecord( + recordID: CKRecord.ID(1:reminders/zone/__defaultOwner__), + recordType: "reminders", + parent: CKReference(recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__)), + share: nil, + id: 1, + isCompleted: 0, + remindersListID: 1, + title: "Get milk" + ), + [1]: CKRecord( + recordID: CKRecord.ID(2:reminders/zone/__defaultOwner__), + recordType: "reminders", + parent: CKReference(recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__)), + share: nil, + id: 2, + isCompleted: 0, + remindersListID: 1, + title: "Get bread" + ), + [2]: CKRecord( + recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__), + recordType: "remindersLists", + parent: nil, + share: nil, + id: 1, + title: "Personal" + ) + ] + ), + sharedCloudDatabase: MockCloudDatabase( + databaseScope: .shared, + storage: [] + ) + ) + """ + } + } + // * Local client moves a reminder to a list. // * At same time, remote deletes that list. // => When data is synchronized the reminder and list are deleted.