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
2 changes: 1 addition & 1 deletion Examples/RemindersTests/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Testing
try $0.bootstrapDatabase()
try await $0.defaultSyncEngine.sendChanges()
},
.snapshots(record: .failed)
.snapshots(record: .missing)
)
struct BaseTestSuite {}

Expand Down
2 changes: 1 addition & 1 deletion Examples/SyncUps/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct AppView: View {
switch path {
case .detail(let model):
SyncUpDetailView(model: model)
case .meeting(let meeting, attendees: let attendees):
case .meeting(let meeting, let attendees):
MeetingView(meeting: meeting, attendees: attendees)
case .record(let model):
RecordMeetingView(model: model)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SQLiteData/CloudKit/CloudKitSharing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"""
)
}
try await metadatabase.write { db in
try await userDatabase.write { db in
try SyncMetadata
.where { $0.recordName.eq(recordName) }
.update {
Expand Down
10 changes: 6 additions & 4 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,8 @@
}
var unsyncedRecords: [CKRecord] = []
for start in stride(from: 0, to: orderedUnsyncedRecordIDs.count, by: batchSize) {
let recordIDsBatch = orderedUnsyncedRecordIDs
let recordIDsBatch =
orderedUnsyncedRecordIDs
.dropFirst(start)
.prefix(batchSize)
let results = try await syncEngine.database.records(for: Array(recordIDsBatch))
Expand Down Expand Up @@ -1586,7 +1587,7 @@
return false
case (_, nil):
return true
case let (.some(lhs), .some(rhs)):
case (.some(let lhs), .some(let rhs)):
let lhsIndex = tablesByOrder[lhs] ?? (rootFirst ? .max : .min)
let rhsIndex = tablesByOrder[rhs] ?? (rootFirst ? .max : .min)
guard lhsIndex != rhsIndex
Expand Down Expand Up @@ -1937,7 +1938,7 @@

private func refreshLastKnownServerRecord(_ record: CKRecord) async {
await withErrorReporting(.sqliteDataCloudKitFailure) {
try await metadatabase.write { db in
try await userDatabase.write { db in
let metadata = try SyncMetadata.find(record.recordID).fetchOne(db)
func updateLastKnownServerRecord() throws {
try SyncMetadata
Expand Down Expand Up @@ -2328,7 +2329,8 @@
tablesByName: [String: any SynchronizableTable]
) throws -> [String: Int] {
let tableDependencies = try userDatabase.read { db in
var dependencies: OrderedDictionary<HashableSynchronizedTable, [any SynchronizableTable]> = [:]
var dependencies: OrderedDictionary<HashableSynchronizedTable, [any SynchronizableTable]> =
[:]
for table in tables {
func open<T>(_: some SynchronizableTable<T>) throws -> [String] {
try PragmaForeignKeyList<T>
Expand Down
44 changes: 35 additions & 9 deletions Sources/SQLiteData/CloudKit/SyncMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,27 @@
/// next batch of pending changes is processed.
public let _isDeleted: Bool

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

@Column("isShared", generated: .virtual)
fileprivate let _isShared: Bool

/// The time the user last modified the record.
public let userModificationTime: Int64

public var hasLastKnownServerRecord: Bool {
lastKnownServerRecord != nil
}

/// 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
/// `RemindersList` can have `isShared == true`, but a `Reminder` associated with the list
/// will have `isShared == false`.
@Column(generated: .virtual)
public let isShared: Bool

/// The time the user last modified the record.
public let userModificationTime: Int64
public var isShared: Bool {
_isShared
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
Expand All @@ -129,6 +137,24 @@
public var parentRecordType: TableColumn<SyncMetadata, String?> {
parentRecordID.parentRecordType
}

// NB: Workaround for https://github.com/groue/GRDB.swift/discussions/1844
public var hasLastKnownServerRecord: some QueryExpression<Bool> {
#sql(
"""
((\(self._hasLastKnownServerRecord) = 1) AND (\(self.lastKnownServerRecord) OR 1))
"""
)
}

// NB: Workaround for https://github.com/groue/GRDB.swift/discussions/1844
public var isShared: some QueryExpression<Bool> {
#sql(
"""
((\(self._isShared) = 1) AND (\(self.share) OR 1))
"""
)
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
Expand Down Expand Up @@ -162,8 +188,8 @@
self.lastKnownServerRecord = lastKnownServerRecord
self._lastKnownServerRecordAllFields = _lastKnownServerRecordAllFields
self.share = share
self.hasLastKnownServerRecord = lastKnownServerRecord != nil
self.isShared = share != nil
self._hasLastKnownServerRecord = lastKnownServerRecord != nil
self._isShared = share != nil
self.userModificationTime = userModificationTime
self._isDeleted = false
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/SQLiteDataTests/AssertQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Testing
@MainActor
@Suite(
.dependency(\.defaultDatabase, try .database()),
.snapshots(record: .failed),
.snapshots(record: .missing),
)
struct AssertQueryTests {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
Expand Down
32 changes: 16 additions & 16 deletions Tests/SQLiteDataTests/CloudKitTests/AccountLifecycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
├────────────────────────────────────────────────────────────────────┤
Expand All @@ -193,8 +193,8 @@
│ _lastKnownServerRecordAllFields: nil, │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: false,
isShared: false,
_hasLastKnownServerRecord: false, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└────────────────────────────────────────────────────────────────────┘
Expand Down Expand Up @@ -257,8 +257,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
├─────────────────────────────────────────────────────────────────────────────────────────┤
Expand Down Expand Up @@ -293,8 +293,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└─────────────────────────────────────────────────────────────────────────────────────────┘
Expand Down Expand Up @@ -421,8 +421,8 @@
│ share: nil │
│ ), │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: true,
_hasLastKnownServerRecord: true, │
_isShared: true, │
│ userModificationTime: 0 │
│ ) │
├─────────────────────────────────────────────────────────────────────────────────────────────────────┤
Expand All @@ -443,8 +443,8 @@
│ _lastKnownServerRecordAllFields: nil, │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: false,
isShared: false,
_hasLastKnownServerRecord: false, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└─────────────────────────────────────────────────────────────────────────────────────────────────────┘
Expand Down Expand Up @@ -518,8 +518,8 @@
│ share: nil │
│ ), │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: true,
_hasLastKnownServerRecord: true, │
_isShared: true, │
│ userModificationTime: 0 │
│ ) │
├─────────────────────────────────────────────────────────────────────────────────────────────────────┤
Expand Down Expand Up @@ -554,8 +554,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└─────────────────────────────────────────────────────────────────────────────────────────────────────┘
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
│ │ ), │
│ │ share: nil, │
│ │ _isDeleted: false, │
│ │ hasLastKnownServerRecord: true,
│ │ isShared: false,
│ │ _hasLastKnownServerRecord: true, │
│ │ _isShared: false, │
│ │ userModificationTime: 0 │
│ │ ) │
└─────────────────────┴────────────────────────────────────────────────────────────────────┘
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└────────────────────────────────────────────────────────────┘
Expand Down Expand Up @@ -629,8 +629,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└────────────────────────────────────────────────────────────┘
Expand Down Expand Up @@ -699,8 +699,8 @@
│ ), │
│ share: nil, │
│ _isDeleted: false, │
hasLastKnownServerRecord: true,
isShared: false,
_hasLastKnownServerRecord: true, │
_isShared: false, │
│ userModificationTime: 0 │
│ ) │
└────────────────────────────────────────────────────────────────┘
Expand Down
Loading