Skip to content
Open
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
10 changes: 6 additions & 4 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
private let notificationsObserver = LockIsolated<(any NSObjectProtocol)?>(nil)
private let activityCounts = LockIsolated(ActivityCounts())
private let startTask = LockIsolated<Task<Void, Never>?>(nil)
#if canImport(DeveloperToolsSupport)
#if DEBUG && canImport(DeveloperToolsSupport)
private let previewTimerTask = LockIsolated<Task<Void, Never>?>(nil)
#endif

Expand Down Expand Up @@ -423,7 +423,7 @@
/// You must start the sync engine again using ``start()`` to synchronize the changes.
public func stop() {
guard isRunning else { return }
#if canImport(DeveloperToolsSupport)
#if DEBUG && canImport(DeveloperToolsSupport)
previewTimerTask.withValue {
$0?.cancel()
$0 = nil
Expand Down Expand Up @@ -503,7 +503,7 @@
}
)

#if canImport(DeveloperToolsSupport)
#if DEBUG && canImport(DeveloperToolsSupport)
@Dependency(\.context) var context
@Dependency(\.continuousClock) var clock
if context == .preview {
Expand Down Expand Up @@ -1893,7 +1893,9 @@
) {
withErrorReporting(.sqliteDataCloudKitFailure) {
guard let recordPrimaryKey = serverRecord.recordID.recordPrimaryKey
else { return }
else {
return
}

try SyncMetadata.insert {
SyncMetadata(
Expand Down
2 changes: 1 addition & 1 deletion Tests/SQLiteDataTests/CloudKitTests/PreviewTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if canImport(CloudKit)
#if DEBUG && canImport(DeveloperToolsSupport) && canImport(CloudKit)
import DependenciesTestSupport
import InlineSnapshotTesting
import SnapshotTestingCustomDump
Expand Down
74 changes: 67 additions & 7 deletions Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@
saving: [imageRecord]
)
.notify()
syncEngine.stop()

inMemoryDataManager.storage.withValue { $0.removeAll() }

Expand All @@ -656,16 +657,75 @@
)
defer { _ = relaunchedSyncEngine }

let images = try await userDatabase.read { db in
try Image.order(by: \.id).fetchAll(db)
try await userDatabase.read { db in
expectNoDifference(
try Image.order(by: \.id).fetchAll(db),
[
Image(id: 1, image: Data("image".utf8), caption: "A good image")
]
)
}

expectNoDifference(
images,
[
Image(id: 1, image: Data("image".utf8), caption: "A good image")
]
assertInlineSnapshot(of: relaunchedSyncEngine.container, as: .customDump) {
"""
MockCloudContainer(
privateCloudDatabase: MockCloudDatabase(
databaseScope: .private,
storage: [
[0]: CKRecord(
recordID: CKRecord.ID(1:images/zone/__defaultOwner__),
recordType: "images",
parent: nil,
share: nil,
caption: "A good image",
id: "1",
image: Data(5 bytes)
)
]
),
sharedCloudDatabase: MockCloudDatabase(
databaseScope: .shared,
storage: []
)
)
"""
}
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func outsideRecord() async throws {
let customRecord = CKRecord(
recordType: "customRecord",
recordID: CKRecord.ID(
recordName: "customRecord",
zoneID: SyncEngine.defaultTestZone.zoneID
)
)
try await syncEngine.modifyRecords(scope: .private, saving: [customRecord]).notify()
assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) {
"""
(No results)
"""
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func outsideRecordWithColon() async throws {
let customRecord = CKRecord(
recordType: "customRecord",
recordID: CKRecord.ID(
recordName: "1:customRecord",
zoneID: SyncEngine.defaultTestZone.zoneID
)
)
try await syncEngine.modifyRecords(scope: .private, saving: [customRecord]).notify()
withKnownIssue("We should have a way to not sync this record's metadata") {
assertQuery(SyncMetadata.all, database: syncEngine.metadatabase) {
"""
(No results)
"""
}
}
}
}
Expand Down