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
6 changes: 3 additions & 3 deletions Package.resolved

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

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
.package(
url: "https://github.com/pointfreeco/swift-structured-queries",
from: "0.19.0",
from: "0.19.1",
traits: [
.trait(name: "StructuredQueriesTagged", condition: .when(traits: ["SQLiteDataTagged"]))
]
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.9.0"),
.package(url: "https://github.com/pointfreeco/swift-sharing", from: "2.3.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.19.0"),
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.19.1"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.0"),
],
targets: [
Expand Down
3 changes: 2 additions & 1 deletion Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

@DatabaseFunction(
"sqlitedata_icloud_hasPermission",
as: ((CKShare?.SystemFieldsRepresentation) -> Bool).self
as: ((CKShare?.SystemFieldsRepresentation) -> Bool).self,
isDeterministic: true
)
func hasPermission(_ share: CKShare?) -> Bool {
guard let share else { return true }
Expand Down
33 changes: 29 additions & 4 deletions Sources/SQLiteData/CloudKit/Internal/Triggers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@
Values(
syncEngine.$didUpdate(
recordName: new.recordName,
record: new.lastKnownServerRecord
?? rootServerRecord(recordName: new.recordName)
lastKnownServerRecord: new.lastKnownServerRecord
?? rootServerRecord(recordName: new.recordName),
newParentLastKnownServerRecord: parentLastKnownServerRecordIfShared(
parentRecordPrimaryKey: new.parentRecordPrimaryKey,
parentRecordType: new.parentRecordType
),
parentRecordPrimaryKey: new.parentRecordPrimaryKey,
parentRecordType: new.parentRecordType
)
)
} when: { _ in
Expand All @@ -165,8 +171,14 @@
Values(
syncEngine.$didUpdate(
recordName: new.recordName,
record: new.lastKnownServerRecord
?? rootServerRecord(recordName: new.recordName)
lastKnownServerRecord: new.lastKnownServerRecord
?? rootServerRecord(recordName: new.recordName),
newParentLastKnownServerRecord: parentLastKnownServerRecordIfShared(
parentRecordPrimaryKey: new.parentRecordPrimaryKey,
parentRecordType: new.parentRecordType
),
parentRecordPrimaryKey: new.parentRecordPrimaryKey,
parentRecordType: new.parentRecordType
)
)
} when: { old, new in
Expand Down Expand Up @@ -281,6 +293,19 @@
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
private func parentLastKnownServerRecordIfShared(
parentRecordPrimaryKey: some QueryExpression<String?>,
parentRecordType: some QueryExpression<String?>
) -> some QueryExpression<CKRecord?.SystemFieldsRepresentation> {
SyncMetadata
.select(\.lastKnownServerRecord)
.where {
$0.recordPrimaryKey.is(parentRecordPrimaryKey)
&& $0.recordType.is(parentRecordType)
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension AncestorMetadata.Columns {
init(_ metadata: SyncMetadata.TableColumns) {
Expand Down
37 changes: 33 additions & 4 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,37 @@

@DatabaseFunction(
"sqlitedata_icloud_didUpdate",
as: ((String, CKRecord?.SystemFieldsRepresentation) -> Void).self
as: (
(String, CKRecord?.SystemFieldsRepresentation, CKRecord?.SystemFieldsRepresentation, String?, String?) -> Void
).self
)
func didUpdate(recordName: String, record: CKRecord?) {
let zoneID = record?.recordID.zoneID ?? defaultZone.zoneID
func didUpdate(
recordName: String,
lastKnownServerRecord: CKRecord?,
newParentLastKnownServerRecord: CKRecord?,
parentRecordPrimaryKey: String? = nil,
parentRecordType: String? = nil
) throws {
let zoneID = lastKnownServerRecord?.recordID.zoneID ?? defaultZone.zoneID
let newZoneID = newParentLastKnownServerRecord?.recordID.zoneID
if let newZoneID, zoneID != newZoneID {
struct ZoneChangingError: Error, LocalizedError {
let recordName: String
let zoneID: CKRecordZone.ID
let newZoneID: CKRecordZone.ID
var errorDescription: String? {
"""
The record '\(recordName)' was moved from zone \
'\(zoneID.zoneName)/\(zoneID.ownerName)' to \
'\(newZoneID.zoneName)/\(newZoneID.ownerName)'. This is currently not supported in \
SQLiteData. To work around, delete the record and then create a new record with its \
new parent association.
"""
}
}
throw ZoneChangingError(recordName: recordName, zoneID: zoneID, newZoneID: newZoneID)
}

let change = CKSyncEngine.PendingRecordZoneChange.saveRecord(
CKRecord.ID(
recordName: recordName,
Expand Down Expand Up @@ -1345,7 +1372,9 @@
let table = tablesByName[failedRecord.recordType],
foreignKeysByTableName[table.tableName]?.count == 1,
let foreignKey = foreignKeysByTableName[table.tableName]?.first
else { continue }
else {
continue
}
func open<T: PrimaryKeyedTable>(_: T.Type) async throws {
try await userDatabase.write { db in
try $_isSynchronizingChanges.withValue(false) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SQLiteData/CloudKit/SyncMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

@Column(generated: .virtual)
public let hasLastKnownServerRecord: Bool

/// Determines if the record associated with this metadata is currently shared in CloudKit.
///
/// This can only return `true` for root records. For example, the metadata associated with a
Expand Down
Loading