Skip to content

Commit fa43586

Browse files
authored
Clean up sync metadata when unrecognizable table is deleted. (#236)
* Clean up sync metadata when unrecognizable table is deleted. * clean up tests
1 parent 163c287 commit fa43586

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Sources/SQLiteData/CloudKit/SyncEngine.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,14 @@
12431243
}
12441244
} else {
12451245
// NB: Deleting a record from a table we do not currently recognize.
1246+
await withErrorReporting(.sqliteDataCloudKitFailure) {
1247+
try await userDatabase.write { db in
1248+
try SyncMetadata
1249+
.findAll(recordIDs)
1250+
.delete()
1251+
.execute(db)
1252+
}
1253+
}
12461254
}
12471255
}
12481256

Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Foundation
55
import InlineSnapshotTesting
66
import SQLiteData
7+
import SQLiteDataTestSupport
78
import SnapshotTestingCustomDump
89
import Testing
910

@@ -65,6 +66,49 @@
6566
}
6667
}
6768

69+
// * CloudKit sends record for table we do not recognize.
70+
// * CloudKit deletes that record
71+
// => Local sync metadata should be deleted for that record.
72+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
73+
@Test func cloudKitSendsNonExistentTable() async throws {
74+
let record = CKRecord(
75+
recordType: UnrecognizedTable.tableName,
76+
recordID: UnrecognizedTable.recordID(for: 1)
77+
)
78+
record.setValue(1, forKey: "id", at: now)
79+
try await syncEngine.modifyRecords(scope: .private, saving: [record]).notify()
80+
81+
assertQuery(SyncMetadata.select(\.recordName), database: syncEngine.metadatabase) {
82+
"""
83+
┌────────────────────────┐
84+
"1:unrecognizedTables"
85+
└────────────────────────┘
86+
"""
87+
}
88+
89+
try await syncEngine.modifyRecords(scope: .private, deleting: [record.recordID]).notify()
90+
91+
assertQuery(SyncMetadata.select(\.recordName), database: syncEngine.metadatabase) {
92+
"""
93+
(No results)
94+
"""
95+
}
96+
assertInlineSnapshot(of: container, as: .customDump) {
97+
"""
98+
MockCloudContainer(
99+
privateCloudDatabase: MockCloudDatabase(
100+
databaseScope: .private,
101+
storage: []
102+
),
103+
sharedCloudDatabase: MockCloudDatabase(
104+
databaseScope: .shared,
105+
storage: []
106+
)
107+
)
108+
"""
109+
}
110+
}
111+
68112
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
69113
@Test func metadataRowWithNoCorrespondingRecordRow() async throws {
70114
try await userDatabase.userWrite { db in
@@ -223,6 +267,6 @@
223267
}
224268

225269
@Table struct UnrecognizedTable {
226-
let id: UUID
270+
let id: Int
227271
}
228272
#endif

0 commit comments

Comments
 (0)