Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Sources/SQLiteData/CloudKit/Internal/Metadatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
"""
)
.execute(db)
try #sql(
"""
CREATE INDEX "\(raw: .sqliteDataCloudKitSchemaName)_metadata_zoneID"
ON "\(raw: .sqliteDataCloudKitSchemaName)_metadata"("ownerName", "zoneName")
"""
)
try #sql(
"""
CREATE INDEX "\(raw: .sqliteDataCloudKitSchemaName)_metadata_parentRecordName"
Expand Down
26 changes: 15 additions & 11 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1138,12 +1138,12 @@
try await userDatabase.write { db in
var defaultZoneDeleted = false
for (zoneID, reason) in deletions {
guard zoneID == self.defaultZone.zoneID
else { continue }
switch reason {
case .deleted, .purged:
try deleteRecords(in: zoneID, db: db)
defaultZoneDeleted = true
if zoneID == self.defaultZone.zoneID {
defaultZoneDeleted = true
}
case .encryptedDataReset:
try uploadRecords(in: zoneID, db: db)
@unknown default:
Expand All @@ -1159,19 +1159,23 @@
}
@Sendable
func deleteRecords(in zoneID: CKRecordZone.ID, db: Database) throws {
let recordTypes = Set(
try SyncMetadata
.where(\.hasLastKnownServerRecord)
.select(\.lastKnownServerRecord)
.fetchAll(db)
.compactMap { $0?.recordID.zoneID == zoneID ? $0?.recordType : nil }
let recordTypes = Dictionary(
grouping:
try SyncMetadata
.where { $0.zoneName.eq(zoneID.zoneName) && $0.ownerName.eq(zoneID.ownerName) }
.select { ($0.recordType, $0.recordPrimaryKey) }
.fetchAll(db),
by: \.0
)
for recordType in recordTypes {
.mapValues {
$0.map(\.1)
}
for (recordType, primaryKeys) in recordTypes {
guard let table = tablesByName[recordType]
else { continue }
func open<T: PrimaryKeyedTable>(_: T.Type) {
withErrorReporting(.sqliteDataCloudKitFailure) {
try T.delete().execute(db)
try T.where { #sql("\($0.primaryKey)").in(primaryKeys) }.delete().execute(db)
}
}
open(table)
Expand Down
68 changes: 68 additions & 0 deletions Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,74 @@
}
}

/// Syncing deletion of a root shared record that is not owned by current user should delete
/// entire zone.
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func syncDeletedRootSharedRecord_CurrentUserNotOwner() async throws {
let externalZone = CKRecordZone(
zoneID: CKRecordZone.ID(
zoneName: "external.zone",
ownerName: "external.owner"
)
)
try await syncEngine.modifyRecordZones(scope: .shared, saving: [externalZone]).notify()

let remindersListRecord = CKRecord(
recordType: RemindersList.tableName,
recordID: RemindersList.recordID(for: 1, zoneID: externalZone.zoneID)
)
remindersListRecord.setValue(1, forKey: "id", at: now)
remindersListRecord.setValue("Personal", forKey: "title", at: now)
let share = CKShare(
rootRecord: remindersListRecord,
shareID: CKRecord.ID(
recordName: "share-\(remindersListRecord.recordID.recordName)",
zoneID: remindersListRecord.recordID.zoneID
)
)

try await syncEngine
.acceptShare(
metadata: ShareMetadata(
containerIdentifier: container.containerIdentifier!,
hierarchicalRootRecordID: remindersListRecord.recordID,
rootRecord: remindersListRecord,
share: share
)
)

try await userDatabase.userWrite { db in
try db.seed {
Reminder(id: 1, title: "Get milk", remindersListID: 1)
Reminder(id: 2, title: "Take a walk", remindersListID: 1)
}
}

try await syncEngine.processPendingRecordZoneChanges(scope: .shared)

try await syncEngine.modifyRecordZones(scope: .shared, deleting: [externalZone.zoneID])
.notify()

assertQuery(Reminder.all, database: userDatabase.database)
assertQuery(RemindersList.all, database: userDatabase.database)
assertQuery(SyncMetadata.all, database: syncEngine.metadatabase)

assertInlineSnapshot(of: container, as: .customDump) {
"""
MockCloudContainer(
privateCloudDatabase: MockCloudDatabase(
databaseScope: .private,
storage: []
),
sharedCloudDatabase: MockCloudDatabase(
databaseScope: .shared,
storage: []
)
)
"""
}
}

// NB: Come back to this when we have time to investigate.
// /// Deleting a root shared record that is not owned by current user should only delete
// /// the CKShare, not delete the actual CloudKit records, but delete all the local records.
Expand Down
Loading