diff --git a/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift b/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift index 0d9731eb..f26c5fb6 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift @@ -136,8 +136,10 @@ package struct State: Sendable { package var changeTag = 0 package var hasPendingUntrackedChanges = false - package var pendingDatabaseChanges: OrderedSet = [] - package var pendingRecordZoneChanges: OrderedSet = [] + package var pendingDatabaseChanges: + OrderedDictionary = [:] + package var pendingRecordZoneChanges: + OrderedDictionary = [:] } private let state = LockIsolated(State()) @@ -165,11 +167,11 @@ } package var pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange] { - withState { Array($0.pendingRecordZoneChanges) } + withState { Array($0.pendingRecordZoneChanges.values) } } package var pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange] { - withState { Array($0.pendingDatabaseChanges) } + withState { Array($0.pendingDatabaseChanges.values) } } package var hasPendingUntrackedChanges: Bool { @@ -186,26 +188,46 @@ } package func add(pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange]) { - withState { - $0.pendingRecordZoneChanges.append(contentsOf: pendingRecordZoneChanges) + withState { state in + for change in pendingRecordZoneChanges { + guard let id = change.id + else { fatalError("Unsupported pendingRecordZoneChange: \(change)") } + state.pendingRecordZoneChanges[id] = change + } } } package func remove(pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange]) { - withState { - $0.pendingRecordZoneChanges.subtract(pendingRecordZoneChanges) + withState { state in + for change in pendingRecordZoneChanges { + guard let id = change.id + else { fatalError("Unsupported pendingRecordZoneChange: \(change)") } + if state.pendingRecordZoneChanges[id] == change { + state.pendingRecordZoneChanges.removeValue(forKey: id) + } + } } } package func add(pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange]) { - withState { - $0.pendingDatabaseChanges.append(contentsOf: pendingDatabaseChanges) + withState { state in + for change in pendingDatabaseChanges { + guard let zoneID = change.zoneID + else { fatalError("Unsupported pendingDatabaseChange: \(change)") } + state.pendingDatabaseChanges[zoneID] = change + } } } package func remove(pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange]) { - withState { - $0.pendingDatabaseChanges.subtract(pendingDatabaseChanges) + withState { state in + for change in pendingDatabaseChanges { + guard let zoneID = change.zoneID + else { fatalError("Unsupported pendingDatabaseChange: \(change)") } + if state.pendingDatabaseChanges[zoneID] == change { + state.pendingDatabaseChanges.removeValue(forKey: zoneID) + } + } } } } @@ -409,7 +431,7 @@ return changes } - for pendingDatabaseChange in syncEngineState.pendingDatabaseChanges { + for (_, pendingDatabaseChange) in syncEngineState.pendingDatabaseChanges { switch pendingDatabaseChange { case .saveZone(let zone): changes.zonesToSave.append(zone) @@ -434,7 +456,9 @@ case .success(let zone): changes.savedZones.append(zone) case .failure(let error as CKError): - changes.failedZoneSaves.append((changes.zonesToSave.first(where: { $0.zoneID == zoneID })!, error)) + changes.failedZoneSaves.append( + (changes.zonesToSave.first(where: { $0.zoneID == zoneID })!, error) + ) case .failure(let error): reportIssue("Error thrown not CKError: \(error)") } @@ -454,7 +478,9 @@ } syncEngine.state.remove(pendingDatabaseChanges: changes.savedZones.map { .saveZone($0) }) - syncEngine.state.remove(pendingDatabaseChanges: changes.deletedZoneIDs.map { .deleteZone($0) }) + syncEngine.state.remove( + pendingDatabaseChanges: changes.deletedZoneIDs.map { .deleteZone($0) } + ) await syncEngine.parentSyncEngine .handleEvent( .sentDatabaseChanges( diff --git a/Sources/SQLiteData/CloudKit/Internal/Triggers.swift b/Sources/SQLiteData/CloudKit/Internal/Triggers.swift index eebcafc2..b9a49760 100644 --- a/Sources/SQLiteData/CloudKit/Internal/Triggers.swift +++ b/Sources/SQLiteData/CloudKit/Internal/Triggers.swift @@ -206,7 +206,15 @@ parentRecordPrimaryKey, parentRecordType ) - } onConflictDoUpdate: { _ in + } onConflictDoUpdate: { + $0._isDeleted = false + $0.userModificationTime = $currentTime() + $0.zoneName = zoneName ?? $0.zoneName + $0.ownerName = ownerName ?? $0.ownerName + $0.parentRecordPrimaryKey = parentRecordPrimaryKey + $0.parentRecordType = parentRecordType + } where: { + $0._isDeleted } } @@ -234,10 +242,7 @@ $0.userModificationTime = $currentTime() } } - } - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - extension SyncMetadata { static func callbackTriggers(for syncEngine: SyncEngine) -> [TemporaryTrigger] { [ afterInsertTrigger(for: syncEngine), @@ -325,7 +330,7 @@ ) ) } when: { old, new in - old._isDeleted.eq(new._isDeleted) && !SyncEngine.$isSynchronizing + !new._isDeleted && !SyncEngine.$isSynchronizing } ) } diff --git a/Sources/SQLiteData/CloudKit/Internal/UserDatabase.swift b/Sources/SQLiteData/CloudKit/Internal/UserDatabase.swift index 98d601fc..b93d8428 100644 --- a/Sources/SQLiteData/CloudKit/Internal/UserDatabase.swift +++ b/Sources/SQLiteData/CloudKit/Internal/UserDatabase.swift @@ -16,6 +16,14 @@ database.configuration } + package func unsafeReentrantWrite(_ updates: (Database) throws -> T) rethrows -> T { + try database.unsafeReentrantWrite { db in + try $_isSynchronizingChanges.withValue(true) { + try updates(db) + } + } + } + package func write( _ updates: @Sendable (Database) throws -> T ) async throws -> T { diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 509b9dbc..95cded1f 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -43,6 +43,9 @@ let uuid = Dependency(\.uuid) private let observationRegistrar = ObservationRegistrar() private let notificationsObserver = LockIsolated<(any NSObjectProtocol)?>(nil) + private let pendingRecordZoneChangesObserver = + LockIsolated(nil) + private let hasUncommittedPendingRecordZoneChanges = LockIsolated(false) private let activityCounts = LockIsolated(ActivityCounts()) private let startTask = LockIsolated?>(nil) private let quotaExceeded = LockIsolated([CKDatabase.Scope.private: false, .shared: false]) @@ -368,6 +371,10 @@ db.add(function: $currentZoneName) db.add(function: $currentOwnerName) + let pendingRecordZoneChangesObserver = PendingRecordZoneChangesObserver(syncEngine: self) + db.add(transactionObserver: pendingRecordZoneChangesObserver, extent: .observerLifetime) + self.pendingRecordZoneChangesObserver.withValue { $0 = pendingRecordZoneChangesObserver } + for trigger in SyncMetadata.callbackTriggers(for: self) { try trigger.execute(db) } @@ -651,15 +658,33 @@ private func enqueueLocallyPendingChanges(scope: CKDatabase.Scope? = nil) async throws { let pendingRecordZoneChanges = try await userDatabase.write { db in - try PendingRecordZoneChange - .where { - $0.databaseScope.is(#bind(scope)) - .or(#bind(scope, as: CKDatabase.Scope?.RawRepresentation.self).is(nil)) - } - .delete() - .returning(\.pendingRecordZoneChange) - .fetchAll(db) + try takePendingRecordZoneChanges(scope: scope, db: db) } + addToSyncEngines(pendingRecordZoneChanges: pendingRecordZoneChanges) + } + + private func takePendingRecordZoneChanges( + scope: CKDatabase.Scope?, + db: Database + ) throws -> [CKSyncEngine.PendingRecordZoneChange] { + let scopedPendingRecordZoneChanges = PendingRecordZoneChange.where { + $0.databaseScope.is(#bind(scope)) + .or(#bind(scope, as: CKDatabase.Scope?.RawRepresentation.self).is(nil)) + } + let changes = + try scopedPendingRecordZoneChanges + .order { _ in #sql("rowid") } + .select(\.pendingRecordZoneChange) + .fetchAll(db) + try scopedPendingRecordZoneChanges.delete().execute(db) + return changes + } + + private func addToSyncEngines( + pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange] + ) { + guard !pendingRecordZoneChanges.isEmpty + else { return } let changesByScope = [CKDatabase.Scope?: _](grouping: pendingRecordZoneChanges) { switch $0 { case .deleteRecord(let recordID), .saveRecord(let recordID): @@ -676,6 +701,48 @@ } } + private func savePendingRecordZoneChanges( + _ changes: [CKSyncEngine.PendingRecordZoneChange] + ) { + guard !changes.isEmpty + else { return } + withErrorReporting(.sqliteDataCloudKitFailure) { + try userDatabase.unsafeReentrantWrite { db in + try PendingRecordZoneChange + .insert { + for change in changes { + PendingRecordZoneChange( + change, + databaseScope: change.id?.zoneID.databaseScope + ) + } + } + .execute(db) + } + hasUncommittedPendingRecordZoneChanges.withValue { $0 = true } + } + } + + fileprivate func databaseDidCommit(db: Database) { + let hasPendingRecordZoneChanges = hasUncommittedPendingRecordZoneChanges.withValue { + hasUncommittedPendingRecordZoneChanges in + defer { hasUncommittedPendingRecordZoneChanges = false } + return hasUncommittedPendingRecordZoneChanges + } + guard hasPendingRecordZoneChanges, isRunning + else { return } + withErrorReporting(.sqliteDataCloudKitFailure) { + let pendingRecordZoneChanges = try $_isSynchronizingChanges.withValue(true) { + try takePendingRecordZoneChanges(scope: nil, db: db) + } + addToSyncEngines(pendingRecordZoneChanges: pendingRecordZoneChanges) + } + } + + fileprivate func databaseDidRollback() { + hasUncommittedPendingRecordZoneChanges.withValue { $0 = false } + } + private func enqueueUnknownRecordsForCloudKit() async throws { try await userDatabase.write { db in try $_isSynchronizingChanges.withValue(false) { @@ -779,6 +846,14 @@ for trigger in SyncMetadata.callbackTriggers(for: self).reversed() { try trigger.drop().execute(db) } + let pendingRecordZoneChangesObserver = pendingRecordZoneChangesObserver.withValue { + pendingRecordZoneChangesObserver in + defer { pendingRecordZoneChangesObserver = nil } + return pendingRecordZoneChangesObserver + } + if let pendingRecordZoneChangesObserver { + db.remove(transactionObserver: pendingRecordZoneChangesObserver) + } } try metadatabase.erase() try migrate(metadatabase: metadatabase) @@ -856,34 +931,7 @@ ) } - guard isRunning else { - // TODO: Perform this work in a trigger instead of a task. - Task { [changes = oldChanges + newChanges] in - await withErrorReporting(.sqliteDataCloudKitFailure) { - try await userDatabase.write { db in - try PendingRecordZoneChange - .insert { - for change in changes { - PendingRecordZoneChange( - change, - databaseScope: change.id?.zoneID.databaseScope - ) - } - } - .execute(db) - } - } - } - return - } - let oldSyncEngine = self.syncEngines.withValue { - oldZoneID.ownerName == CKCurrentUserDefaultName ? $0.private : $0.shared - } - let syncEngine = self.syncEngines.withValue { - zoneID.ownerName == CKCurrentUserDefaultName ? $0.private : $0.shared - } - oldSyncEngine?.state.add(pendingRecordZoneChanges: oldChanges) - syncEngine?.state.add(pendingRecordZoneChanges: newChanges) + savePendingRecordZoneChanges(oldChanges + newChanges) } @DatabaseFunction( @@ -904,30 +952,7 @@ if let share { changes.append(.deleteRecord(share.recordID)) } - guard isRunning else { - Task { [changes] in - await withErrorReporting(.sqliteDataCloudKitFailure) { - try await userDatabase.write { db in - try PendingRecordZoneChange - .insert { - changes.map { change in - PendingRecordZoneChange( - change, - databaseScope: change.id?.zoneID.databaseScope - ) - } - } - .execute(db) - } - } - } - return - } - - let syncEngine = self.syncEngines.withValue { - zoneID.ownerName == CKCurrentUserDefaultName ? $0.private : $0.shared - } - syncEngine?.state.add(pendingRecordZoneChanges: changes) + savePendingRecordZoneChanges(changes) } package func acceptShare(metadata: ShareMetadata) async throws { @@ -2237,6 +2262,20 @@ } } + @available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) + extension CKSyncEngine.PendingDatabaseChange { + var zoneID: CKRecordZone.ID? { + switch self { + case .saveZone(let zone): + return zone.zoneID + case .deleteZone(let id): + return id + @unknown default: + return nil + } + } + } + extension CKRecord.ID { var tableName: String? { guard @@ -2705,4 +2744,24 @@ } } #endif + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + private final class PendingRecordZoneChangesObserver: TransactionObserver, @unchecked Sendable { + weak var syncEngine: SyncEngine? + + init(syncEngine: SyncEngine) { + self.syncEngine = syncEngine + } + + func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { false } + func databaseDidChange(with event: DatabaseEvent) {} + + func databaseDidCommit(_ db: Database) { + syncEngine?.databaseDidCommit(db: db) + } + + func databaseDidRollback(_ db: Database) { + syncEngine?.databaseDidRollback() + } + } #endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/AssetsTests.swift b/Tests/SQLiteDataTests/CloudKitTests/AssetsTests.swift index 4200157b..82b8edff 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/AssetsTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/AssetsTests.swift @@ -412,6 +412,93 @@ """ } } + + // * Delete asset row, then insert a new asset row with the same primary key before sending + // => New asset is saved to CloudKit + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func deleteThenReinsertAsset() async throws { + try await seedRemindersListWithAsset() + + try await userDatabase.userWrite { db in + try RemindersListAsset.find(1).delete().execute(db) + } + try await userDatabase.userWrite { db in + try RemindersListAsset.insert { + RemindersListAsset(remindersListID: 1, coverImage: Data("new-image".utf8)) + } + .execute(db) + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + #expect(try cloudKitCoverImage(remindersListID: 1) == Data("new-image".utf8)) + } + + // * Delete and reinsert asset row in a single transaction before sending + // => New asset is saved to CloudKit + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func deleteThenReinsertAsset_SameTransaction() async throws { + try await seedRemindersListWithAsset() + + try await userDatabase.userWrite { db in + try RemindersListAsset.find(1).delete().execute(db) + try RemindersListAsset.insert { + RemindersListAsset(remindersListID: 1, coverImage: Data("new-image".utf8)) + } + .execute(db) + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + #expect(try cloudKitCoverImage(remindersListID: 1) == Data("new-image".utf8)) + } + + // * Delete reminders list (cascading to its asset), then reinsert both before sending + // => List and new asset are saved to CloudKit + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func deleteThenReinsertParentAndAsset() async throws { + try await seedRemindersListWithAsset() + + try await userDatabase.userWrite { db in + try RemindersList.find(1).delete().execute(db) + } + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + RemindersListAsset(remindersListID: 1, coverImage: Data("new-image".utf8)) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + #expect( + try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + .encryptedValues["title"] as? String == "Personal" + ) + #expect(try cloudKitCoverImage(remindersListID: 1) == Data("new-image".utf8)) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + private func seedRemindersListWithAsset() async throws { + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + RemindersListAsset(remindersListID: 1, coverImage: Data("image".utf8)) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + #expect(try cloudKitCoverImage(remindersListID: 1) == Data("image".utf8)) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + private func cloudKitCoverImage(remindersListID: Int) throws -> Data? { + let record = try container.privateCloudDatabase.record( + for: RemindersListAsset.recordID(for: remindersListID) + ) + guard let asset = record["coverImage"] as? CKAsset, let fileURL = asset.fileURL + else { return nil } + return inMemoryDataManager.storage.withValue { $0[fileURL] } + } } } #endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift index 219af38b..b788024d 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift @@ -580,6 +580,195 @@ } } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `writes outside of a transaction are sent to CloudKit`() async throws { + try await userDatabase.database.writeWithoutTransaction { db in + #expect(!db.isInsideTransaction) + try RemindersList.insert { RemindersList(id: 1, title: "Personal") }.execute(db) + } + #expect( + syncEngine.private.state.pendingRecordZoneChanges == [ + .saveRecord(RemindersList.recordID(for: 1)) + ] + ) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + let record = try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + #expect(record.encryptedValues["title"] as? String == "Personal") + + try await userDatabase.database.writeWithoutTransaction { db in + #expect(!db.isInsideTransaction) + try RemindersList.find(1).delete().execute(db) + } + #expect( + syncEngine.private.state.pendingRecordZoneChanges == [ + .deleteRecord(RemindersList.recordID(for: 1)) + ] + ) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + #expect(throws: CKError.self) { + try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + } + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `delete rolled back to a savepoint is not sent to CloudKit`() async throws { + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + RemindersList(id: 2, title: "Work") + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + try await userDatabase.userWrite { db in + try db.inSavepoint { + try RemindersList.find(1).delete().execute(db) + return .rollback + } + try RemindersList.find(2).update { $0.title = "Business" }.execute(db) + } + #expect( + syncEngine.private.state.pendingRecordZoneChanges == [ + .saveRecord(RemindersList.recordID(for: 2)) + ] + ) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let record = try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + #expect(record.encryptedValues["title"] as? String == "Personal") + let metadata = try await syncEngine.metadatabase.read { db in + try SyncMetadata.find(RemindersList.recordID(for: 1)).fetchOne(db) + } + #expect(metadata != nil) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `rolled back delete is not sent to CloudKit`() async throws { + struct RollBack: Error {} + + try await userDatabase.userWrite { db in + try db.seed { RemindersList(id: 1, title: "Personal") } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + await #expect(throws: RollBack.self) { + try await self.userDatabase.userWrite { db in + try RemindersList.find(1).delete().execute(db) + throw RollBack() + } + } + #expect(syncEngine.private.state.pendingRecordZoneChanges.isEmpty) + + let remindersList = try await userDatabase.read { db in + try RemindersList.find(1).fetchOne(db) + } + #expect(remindersList?.title == "Personal") + + let metadata = try await syncEngine.metadatabase.read { db in + try SyncMetadata.find(RemindersList.recordID(for: 1)).fetchOne(db) + } + #expect(metadata != nil) + + let record = try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + #expect(record.encryptedValues["title"] as? String == "Personal") + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `rolled back reinsert does not cancel a committed delete`() async throws { + struct RollBack: Error {} + + try await userDatabase.userWrite { db in + try db.seed { RemindersList(id: 1, title: "Personal") } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + try await userDatabase.userWrite { db in + try RemindersList.find(1).delete().execute(db) + } + await #expect(throws: RollBack.self) { + try await self.userDatabase.userWrite { db in + try RemindersList.insert { RemindersList(id: 1, title: "Reinserted") }.execute(db) + throw RollBack() + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let remindersList = try await userDatabase.read { db in + try RemindersList.find(1).fetchOne(db) + } + #expect(remindersList == nil) + + let metadata = try await syncEngine.metadatabase.read { db in + try SyncMetadata.find(RemindersList.recordID(for: 1)).fetchOne(db) + } + #expect(metadata == nil) + + #expect(throws: (any Error).self) { + try container.privateCloudDatabase.record(for: RemindersList.recordID(for: 1)) + } + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func `reinserting a deleted record before sending saves it to CloudKit`() async throws { + try await userDatabase.userWrite { db in + try db.seed { RemindersList(id: 1, title: "Original") } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + try await userDatabase.userWrite { db in + try RemindersList.find(1).delete().execute(db) + } + try await userDatabase.userWrite { db in + try RemindersList.insert { RemindersList(id: 1, title: "Reinserted") }.execute(db) + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let remindersList = try await userDatabase.read { db in + try RemindersList.find(1).fetchOne(db) + } + #expect(remindersList?.title == "Reinserted") + + let record = try container.privateCloudDatabase.record( + for: RemindersList.recordID(for: 1) + ) + #expect(record.encryptedValues["title"] as? String == "Reinserted") + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func `reinserting a deleted record with a new parent before sending updates its parent`() + async throws + { + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + RemindersList(id: 2, title: "Work") + Reminder(id: 1, title: "Groceries", remindersListID: 1) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + try await userDatabase.userWrite { db in + try Reminder.find(1).delete().execute(db) + } + try await userDatabase.userWrite { db in + try Reminder.insert { + Reminder(id: 1, title: "Groceries", remindersListID: 2) + } + .execute(db) + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let metadata = try await syncEngine.metadatabase.read { db in + try SyncMetadata.find(Reminder.recordID(for: 1)).fetchOne(db) + } + #expect(metadata?.parentRecordName == RemindersList.recordName(for: 2)) + + let record = try container.privateCloudDatabase.record(for: Reminder.recordID(for: 1)) + #expect(record.parent?.recordID == RemindersList.recordID(for: 2)) + } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func remoteServerRecordUpdate() async throws { try await userDatabase.userWrite { db in diff --git a/Tests/SQLiteDataTests/CloudKitTests/ForeignKeyConstraintTests.swift b/Tests/SQLiteDataTests/CloudKitTests/ForeignKeyConstraintTests.swift index b2a0cea3..03b1d4c8 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/ForeignKeyConstraintTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/ForeignKeyConstraintTests.swift @@ -861,7 +861,6 @@ } } } - try await syncEngine.processPendingRecordZoneChanges(scope: .private) assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) { """ (No results) diff --git a/Tests/SQLiteDataTests/CloudKitTests/MockSyncEngineStateTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MockSyncEngineStateTests.swift new file mode 100644 index 00000000..131dd4b0 --- /dev/null +++ b/Tests/SQLiteDataTests/CloudKitTests/MockSyncEngineStateTests.swift @@ -0,0 +1,117 @@ +#if canImport(CloudKit) + import CloudKit + import SQLiteData + import Testing + + @Suite struct MockSyncEngineStateTests { + @Suite struct PendingRecordZoneChanges { + let idA = CKRecord.ID(recordName: "A") + let idB = CKRecord.ID(recordName: "B") + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `same change is deduplicated`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + #expect(state.pendingRecordZoneChanges == [.saveRecord(idA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `delete supersedes save`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + #expect(state.pendingRecordZoneChanges == [.deleteRecord(idA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `save supersedes delete`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + #expect(state.pendingRecordZoneChanges == [.saveRecord(idA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `last change wins`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + #expect(state.pendingRecordZoneChanges == [.deleteRecord(idA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `superseding a change does not affect other records`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.saveRecord(idA), .saveRecord(idB)]) + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + #expect(Set(state.pendingRecordZoneChanges) == [.deleteRecord(idA), .saveRecord(idB)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `removing a superseded change keeps the current change`() { + let state = MockSyncEngineState() + state.add(pendingRecordZoneChanges: [.saveRecord(idA)]) + state.add(pendingRecordZoneChanges: [.deleteRecord(idA)]) + state.remove(pendingRecordZoneChanges: [.saveRecord(idA)]) + #expect(state.pendingRecordZoneChanges == [.deleteRecord(idA)]) + } + } + + @Suite struct PendingDatabaseChanges { + let zoneA = CKRecordZone(zoneName: "A") + let zoneB = CKRecordZone(zoneName: "B") + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `same change is deduplicated`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + #expect(state.pendingDatabaseChanges == [.saveZone(zoneA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `delete supersedes save`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + #expect(state.pendingDatabaseChanges == [.deleteZone(zoneA.zoneID)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `save supersedes delete`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + #expect(state.pendingDatabaseChanges == [.saveZone(zoneA)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `last change wins`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + #expect(state.pendingDatabaseChanges == [.deleteZone(zoneA.zoneID)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `superseding a change does not affect other zones`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.saveZone(zoneA), .saveZone(zoneB)]) + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + #expect(Set(state.pendingDatabaseChanges) == [.deleteZone(zoneA.zoneID), .saveZone(zoneB)]) + } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `removing a superseded change keeps the current change`() { + let state = MockSyncEngineState() + state.add(pendingDatabaseChanges: [.saveZone(zoneA)]) + state.add(pendingDatabaseChanges: [.deleteZone(zoneA.zoneID)]) + state.remove(pendingDatabaseChanges: [.saveZone(zoneA)]) + #expect(state.pendingDatabaseChanges == [.deleteZone(zoneA.zoneID)]) + } + } + } +#endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift b/Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift index 59c680b0..8b2e5f0e 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift @@ -1843,6 +1843,87 @@ // assertInlineSnapshot(of: container, as: .customDump) // } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func `move child record from private to shared parent, then rollback`() async throws { + struct RollBack: Error {} + + try await userDatabase.userWrite { db in + try db.seed { + ModelA.Draft(id: 1, count: 42) + ModelB.Draft(id: 1, isOn: true, modelAID: 1) + ModelC.Draft(id: 1, title: "Blob", modelBID: 1) + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + let externalZone = CKRecordZone( + zoneID: CKRecordZone.ID( + zoneName: "external.zone", + ownerName: "external.owner" + ) + ) + try await syncEngine.modifyRecordZones(scope: .shared, saving: [externalZone]).notify() + + let modelARecord = CKRecord( + recordType: ModelA.tableName, + recordID: ModelA.recordID(for: 2, zoneID: externalZone.zoneID) + ) + modelARecord.setValue(2, forKey: "id", at: now) + modelARecord.setValue(1729, forKey: "count", at: now) + let share = CKShare( + rootRecord: modelARecord, + shareID: CKRecord.ID( + recordName: "share-\(modelARecord.recordID.recordName)", + zoneID: modelARecord.recordID.zoneID + ) + ) + _ = try syncEngine.modifyRecords(scope: .shared, saving: [share, modelARecord]) + let freshShare = try syncEngine.shared.database.record(for: share.recordID) as! CKShare + let freshModelARecord = try syncEngine.shared.database.record(for: modelARecord.recordID) + + try await syncEngine + .acceptShare( + metadata: ShareMetadata( + containerIdentifier: container.containerIdentifier!, + hierarchicalRootRecordID: freshModelARecord.recordID, + rootRecord: freshModelARecord, + share: freshShare + ) + ) + + try await withDependencies { + $0.currentTime.now += 1 + } operation: { + await #expect(throws: RollBack.self) { + try await self.userDatabase.userWrite { db in + try ModelB.find(1).update { $0.modelAID = 2 }.execute(db) + throw RollBack() + } + } + } + #expect(syncEngine.private.state.pendingRecordZoneChanges.isEmpty) + #expect(syncEngine.shared.state.pendingRecordZoneChanges.isEmpty) + + let modelB = try await userDatabase.read { db in try ModelB.find(1).fetchOne(db) } + #expect(modelB?.modelAID == 1) + + let metadata = try await syncEngine.metadatabase.read { db in + try SyncMetadata + .where { $0.recordType.in([ModelB.tableName, ModelC.tableName]) } + .order(by: \.recordType) + .fetchAll(db) + } + #expect(metadata.map(\.recordName) == ["1:modelBs", "1:modelCs"]) + #expect(metadata.allSatisfy { $0.zoneName == syncEngine.defaultZone.zoneID.zoneName }) + + #expect(throws: Never.self) { + try container.privateCloudDatabase.record(for: ModelB.recordID(for: 1)) + } + #expect(throws: Never.self) { + try container.privateCloudDatabase.record(for: ModelC.recordID(for: 1)) + } + } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func movesChildRecordFromPrivateParentToSharedParent() async throws { try await userDatabase.userWrite { db in diff --git a/Tests/SQLiteDataTests/CloudKitTests/SyncEngineLifecycleTests.swift b/Tests/SQLiteDataTests/CloudKitTests/SyncEngineLifecycleTests.swift index 8f3e73f2..f2599c3f 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/SyncEngineLifecycleTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/SyncEngineLifecycleTests.swift @@ -31,8 +31,6 @@ } } - try await Task.sleep(for: .seconds(1)) - assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) { """ ┌──────────────────────────────────────────┐ @@ -153,8 +151,6 @@ try RemindersList.find(1).delete().execute(db) } - try await Task.sleep(for: .seconds(1)) - try await syncEngine.start() try await syncEngine.processPendingDatabaseChanges(scope: .private) try await syncEngine.processPendingRecordZoneChanges(scope: .private) @@ -197,7 +193,6 @@ try RemindersList.find(1).update { $0.title += "!" }.execute(db) } } - try await Task.sleep(for: .seconds(0.5)) assertQuery(PendingRecordZoneChange.all, database: syncEngine.metadatabase) { """ @@ -254,6 +249,83 @@ } } + // * Stop sync engine + // * Delete list + // * Reinsert list with same ID + // * Start sync engine + // => Pending changes are recorded in order, and list is saved to CloudKit + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/418")) + func addRemindersList_StopSyncEngine_DeleteAndReinsert_StartSyncEngine() async throws { + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + syncEngine.stop() + + try await userDatabase.userWrite { db in + try RemindersList.find(1).delete().execute(db) + } + try await userDatabase.userWrite { db in + try RemindersList.insert { RemindersList(id: 1, title: "My Stuff") }.execute(db) + } + + try await syncEngine.start() + try await syncEngine.processPendingDatabaseChanges(scope: .private) + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + assertInlineSnapshot(of: container, as: .customDump) { + """ + MockCloudContainer( + privateCloudDatabase: MockCloudDatabase( + databaseScope: .private, + storage: [ + [0]: CKRecord( + recordID: CKRecord.ID(1:remindersLists/zone/__defaultOwner__), + recordType: "remindersLists", + parent: nil, + share: nil, + id: 1, + title: "My Stuff" + ) + ] + ), + sharedCloudDatabase: MockCloudDatabase( + databaseScope: .shared, + storage: [] + ) + ) + """ + } + } + + // * Stop sync engine + // * Edit list in a transaction that is rolled back + // => No pending changes are recorded + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func addRemindersList_StopSyncEngine_EditTitleAndRollBack() async throws { + struct RollBack: Error {} + + try await userDatabase.userWrite { db in + try db.seed { + RemindersList(id: 1, title: "Personal") + } + } + try await syncEngine.processPendingRecordZoneChanges(scope: .private) + + syncEngine.stop() + + await #expect(throws: RollBack.self) { + try await self.userDatabase.userWrite { db in + try RemindersList.find(1).update { $0.title = "Work" }.execute(db) + throw RollBack() + } + } + } + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) @Test func getSharedRecord_StopSyncEngine_WriteToSharedRecord_StartSyncing() async throws { let externalZoneID = CKRecordZone.ID( @@ -295,7 +367,6 @@ } } - try await Task.sleep(for: .seconds(1)) assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) { """ ┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐ @@ -362,7 +433,6 @@ """ } - try await Task.sleep(for: .seconds(0.5)) try await syncEngine.start() try await syncEngine.processPendingDatabaseChanges(scope: .private) try await syncEngine.processPendingRecordZoneChanges(scope: .shared) @@ -481,7 +551,6 @@ try await userDatabase.userWrite { db in try RemindersList.find(1).delete().execute(db) } - try await Task.sleep(for: .seconds(1)) try await syncEngine.start() try await syncEngine.processPendingDatabaseChanges(scope: .private) @@ -559,7 +628,6 @@ try RemindersList.find(1).delete().execute(db) } - try await Task.sleep(for: .seconds(0.5)) try await syncEngine.start() try await syncEngine.processPendingDatabaseChanges(scope: .private) try await syncEngine.processPendingRecordZoneChanges(scope: .private) @@ -595,7 +663,6 @@ Reminder(id: 1, title: "Get milk", remindersListID: 1) } } - try await Task.sleep(for: .seconds(1)) assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) { """ diff --git a/Tests/SQLiteDataTests/CloudKitTests/TriggerTests.swift b/Tests/SQLiteDataTests/CloudKitTests/TriggerTests.swift index be922209..16a5aefc 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/TriggerTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/TriggerTests.swift @@ -438,7 +438,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), '__defaultOwner__'), "new"."parentID", 'parents' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."parentID", "parentRecordType" = 'parents' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [28]: """ @@ -464,7 +469,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), '__defaultOwner__'), "new"."parentID", 'parents' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."parentID", "parentRecordType" = 'parents' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [29]: """ @@ -486,7 +496,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'modelAs', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [30]: """ @@ -512,7 +523,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), '__defaultOwner__'), "new"."modelAID", 'modelAs' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."modelAID", "parentRecordType" = 'modelAs' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [31]: """ @@ -538,7 +554,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), '__defaultOwner__'), "new"."modelBID", 'modelBs' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."modelBID", "parentRecordType" = 'modelBs' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [32]: """ @@ -560,7 +581,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'parents', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [33]: """ @@ -582,7 +604,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'reminderTags', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [34]: """ @@ -608,7 +631,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [35]: """ @@ -634,7 +662,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [36]: """ @@ -660,7 +693,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce('__defaultOwner__', "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce('zone', "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce('__defaultOwner__', "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [37]: """ @@ -682,7 +720,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'remindersLists', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [38]: """ @@ -704,7 +743,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'scopedModels', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [39]: """ @@ -735,7 +775,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."title", 'tags', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; END """, [41]: """ @@ -1034,7 +1075,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), '__defaultOwner__'), "new"."parentID", 'parents' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."parentID", "parentRecordType" = 'parents' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1067,7 +1113,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), '__defaultOwner__'), "new"."parentID", 'parents' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."parentID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."parentID", "parentRecordType" = 'parents' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1096,7 +1147,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'modelAs', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."id")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))); @@ -1125,7 +1177,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), '__defaultOwner__'), "new"."modelAID", 'modelAs' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelAID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelAs'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."modelAID", "parentRecordType" = 'modelAs' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1158,7 +1215,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), '__defaultOwner__'), "new"."modelBID", 'modelBs' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."modelBID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('modelBs'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."modelBID", "parentRecordType" = 'modelBs' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1187,7 +1249,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'parents', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."id")) AND (("sqlitedata_icloud_metadata"."recordType") = ('parents'))); @@ -1212,7 +1275,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'reminderTags', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."id")) AND (("sqlitedata_icloud_metadata"."recordType") = ('reminderTags'))); @@ -1241,7 +1305,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1274,7 +1343,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce(NULL, "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1307,7 +1381,12 @@ WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), 'zone'), coalesce(coalesce('__defaultOwner__', "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" FROM "sqlitedata_icloud_metadata" WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), '__defaultOwner__'), "new"."remindersListID", 'remindersLists' - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce(coalesce('zone', "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce(coalesce('__defaultOwner__', "sqlitedata_icloud_currentOwnerName"(), (SELECT "sqlitedata_icloud_metadata"."ownerName" + FROM "sqlitedata_icloud_metadata" + WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."remindersListID")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))))), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = "new"."remindersListID", "parentRecordType" = 'remindersLists' + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce(coalesce('zone', "sqlitedata_icloud_currentZoneName"(), (SELECT "sqlitedata_icloud_metadata"."zoneName" FROM "sqlitedata_icloud_metadata" @@ -1336,7 +1415,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'remindersLists', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."id")) AND (("sqlitedata_icloud_metadata"."recordType") = ('remindersLists'))); @@ -1361,7 +1441,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."id", 'scopedModels', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."id")) AND (("sqlitedata_icloud_metadata"."recordType") = ('scopedModels'))); @@ -1370,7 +1451,7 @@ [66]: """ CREATE TRIGGER "sqlitedata_icloud_after_update_on_sqlitedata_icloud_metadata" AFTER UPDATE ON "sqlitedata_icloud_metadata" - FOR EACH ROW WHEN (("old"."_isDeleted") = ("new"."_isDeleted")) AND (NOT ("sqlitedata_icloud_syncEngineIsSynchronizingChanges"())) BEGIN + FOR EACH ROW WHEN (NOT ("new"."_isDeleted")) AND (NOT ("sqlitedata_icloud_syncEngineIsSynchronizingChanges"())) BEGIN SELECT RAISE(ABORT, 'co.pointfree.SQLiteData.CloudKit.invalid-record-name-error') WHERE NOT (((substr("new"."recordName", 1, 1)) <> ('_')) AND ((octet_length("new"."recordName")) <= (255))) AND ((octet_length("new"."recordName")) = (length("new"."recordName"))); SELECT "sqlitedata_icloud_didUpdate"("new"."recordName", "new"."zoneName", "new"."ownerName", "old"."zoneName", "old"."ownerName", CASE WHEN (("new"."zoneName") <> ("old"."zoneName")) OR (("new"."ownerName") <> ("old"."ownerName")) THEN ( @@ -1408,7 +1489,8 @@ INSERT INTO "sqlitedata_icloud_metadata" ("recordPrimaryKey", "recordType", "zoneName", "ownerName", "parentRecordPrimaryKey", "parentRecordType") SELECT "new"."title", 'tags', coalesce("sqlitedata_icloud_currentZoneName"(), 'zone'), coalesce("sqlitedata_icloud_currentOwnerName"(), '__defaultOwner__'), NULL, NULL - ON CONFLICT DO NOTHING; + ON CONFLICT DO UPDATE SET "_isDeleted" = 0, "userModificationTime" = "sqlitedata_icloud_currentTime"(), "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL + WHERE "sqlitedata_icloud_metadata"."_isDeleted"; UPDATE "sqlitedata_icloud_metadata" SET "zoneName" = coalesce("sqlitedata_icloud_currentZoneName"(), "sqlitedata_icloud_metadata"."zoneName"), "ownerName" = coalesce("sqlitedata_icloud_currentOwnerName"(), "sqlitedata_icloud_metadata"."ownerName"), "parentRecordPrimaryKey" = NULL, "parentRecordType" = NULL, "userModificationTime" = "sqlitedata_icloud_currentTime"() WHERE ((("sqlitedata_icloud_metadata"."recordPrimaryKey") = ("new"."title")) AND (("sqlitedata_icloud_metadata"."recordType") = ('tags'))); diff --git a/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift b/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift index 586d2e95..a8959e9e 100644 --- a/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift +++ b/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift @@ -1,6 +1,7 @@ #if canImport(CloudKit) import CustomDump import CloudKit + import OrderedCollections import SQLiteData extension CKDatabase.Scope: @retroactive CustomDumpStringConvertible { @@ -179,13 +180,13 @@ [ ( "pendingRecordZoneChanges", - $0.pendingRecordZoneChanges + $0.pendingRecordZoneChanges.values .sorted(by: comparePendingRecordZoneChange) as any Sendable ), ( "pendingDatabaseChanges", - $0.pendingDatabaseChanges + $0.pendingDatabaseChanges.values .sorted(by: comparePendingDatabaseChange) as any Sendable ), diff --git a/Tests/SQLiteDataTests/Internal/CloudKitTestHelpers.swift b/Tests/SQLiteDataTests/Internal/CloudKitTestHelpers.swift index 0939831d..b706e9aa 100644 --- a/Tests/SQLiteDataTests/Internal/CloudKitTestHelpers.swift +++ b/Tests/SQLiteDataTests/Internal/CloudKitTestHelpers.swift @@ -168,7 +168,7 @@ extension MockSyncEngineState { withState { expectNoDifference( Set(changes), - Set($0.pendingRecordZoneChanges), + Set($0.pendingRecordZoneChanges.values), fileID: fileID, filePath: filePath, line: line, @@ -188,7 +188,7 @@ extension MockSyncEngineState { withState { expectNoDifference( Set(changes), - Set($0.pendingDatabaseChanges), + Set($0.pendingDatabaseChanges.values), fileID: fileID, filePath: filePath, line: line,