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
3 changes: 1 addition & 2 deletions Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
|| share.currentUserParticipant?.permission == .readWrite
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@DatabaseFunction("sqlitedata_icloud_syncEngineIsSynchronizingChanges")
func syncEngineIsSynchronizingChanges() -> Bool {
SyncEngine._isSynchronizingChanges
_isSynchronizingChanges
}
#endif
8 changes: 4 additions & 4 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@
try validateSchema()
}

@TaskLocal package static var _isSynchronizingChanges = false
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this out of the SyncEngine so that it didn't have the available restrictions. This made it annoying to use userDatabase.write from inside a test traits.


nonisolated package func setUpSyncEngine() throws {
let migrator = metadatabaseMigrator()
#if DEBUG
Expand Down Expand Up @@ -464,7 +462,7 @@
previousRecordTypeByTableName[tableName] == nil
}

try Self.$_isSynchronizingChanges.withValue(false) {
try $_isSynchronizingChanges.withValue(false) {
for tableName in newTableNames {
try self.uploadRecordsToCloudKit(tableName: tableName, db: db)
}
Expand Down Expand Up @@ -1304,7 +1302,7 @@
else { continue }
func open<T: PrimaryKeyedTable>(_: T.Type) async throws {
try await userDatabase.write { db in
try Self.$_isSynchronizingChanges.withValue(false) {
try $_isSynchronizingChanges.withValue(false) {
switch foreignKey.onDelete {
case .cascade:
try T
Expand Down Expand Up @@ -2006,4 +2004,6 @@
}
return query
}

@TaskLocal package var _isSynchronizingChanges = false
#endif
8 changes: 2 additions & 6 deletions Sources/SQLiteData/Internal/UserDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ package struct UserDatabase {
database.configuration
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
package func write<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
) async throws -> T {
try await database.write { db in
try SyncEngine.$_isSynchronizingChanges.withValue(true) {
try $_isSynchronizingChanges.withValue(true) {
try updates(db)
}
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
package func read<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
) async throws -> T {
Expand All @@ -36,19 +34,17 @@ package struct UserDatabase {
}

@_disfavoredOverload
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
package func write<T>(
_ updates: (Database) throws -> T
) throws -> T {
try database.write { db in
try SyncEngine.$_isSynchronizingChanges.withValue(true) {
try $_isSynchronizingChanges.withValue(true) {
try updates(db)
}
}
}

@_disfavoredOverload
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
package func read<T>(
_ updates: (Database) throws -> T
) throws -> T {
Expand Down
11 changes: 11 additions & 0 deletions Tests/SQLiteDataTests/AssertQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Testing
.snapshots(record: .failed),
)
struct AssertQueryTests {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasic() throws {
assertQuery(
Record.all.select(\.id)
Expand All @@ -23,6 +24,8 @@ struct AssertQueryTests {
"""
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecord() throws {
assertQuery(
Record.where { $0.id == 1 }
Expand All @@ -37,6 +40,8 @@ struct AssertQueryTests {
"""
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasicUpdate() throws {
assertQuery(
Record.all
Expand All @@ -52,6 +57,8 @@ struct AssertQueryTests {
"""
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecordUpdate() throws {
assertQuery(
Record
Expand All @@ -69,7 +76,9 @@ struct AssertQueryTests {
"""
}
}

#if DEBUG
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasicIncludeSQL() throws {
assertQuery(
includeSQL: true,
Expand All @@ -90,7 +99,9 @@ struct AssertQueryTests {
}
}
#endif

#if DEBUG
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecordIncludeSQL() throws {
assertQuery(
includeSQL: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

await signOut()

try await userDatabase.userRead { db in
try await userDatabase.read { db in
try #expect(RemindersList.count().fetchOne(db) == 0)
try #expect(Reminder.count().fetchOne(db) == 0)
try #expect(RemindersListPrivate.count().fetchOne(db) == 0)
Expand Down
4 changes: 2 additions & 2 deletions Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
as: String.self
)
assertInlineSnapshot(
of: try { try userDatabase.userRead { try query.fetchAll($0) } }(),
of: try { try userDatabase.write { try query.fetchAll($0) } }(),
as: .customDump
) {
"""
Expand All @@ -458,7 +458,7 @@
try syncEngine.tearDownSyncEngine()

assertInlineSnapshot(
of: try { try userDatabase.userRead { try query.fetchAll($0) } }(),
of: try { try userDatabase.read { try query.fetchAll($0) } }(),
as: .customDump
) {
"""
Expand Down
10 changes: 5 additions & 5 deletions Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
)
defer { _ = relaunchedSyncEngine }

let remindersLists = try await userDatabase.userRead { db in
let remindersLists = try await userDatabase.read { db in
try RemindersListWithPosition.order(by: \.id).fetchAll(db)
}
let reminders = try await userDatabase.userRead { db in
let reminders = try await userDatabase.read { db in
try ReminderWithPosition.order(by: \.id).fetchAll(db)
}

Expand Down Expand Up @@ -154,7 +154,7 @@
)
defer { _ = relaunchedSyncEngine }

let remindersLists = try await userDatabase.userRead { db in
let remindersLists = try await userDatabase.read { db in
try RemindersListWithData.order(by: \.id).fetchAll(db)
}

Expand Down Expand Up @@ -223,7 +223,7 @@
)
defer { _ = relaunchedSyncEngine }

let remindersLists = try await userDatabase.userRead { db in
let remindersLists = try await userDatabase.read { db in
try RemindersListWithData.order(by: \.id).fetchAll(db)
}

Expand Down Expand Up @@ -280,7 +280,7 @@
)
defer { _ = relaunchedSyncEngine }

let images = try await userDatabase.userRead { db in
let images = try await userDatabase.read { db in
try Image.order(by: \.id).fetchAll(db)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
}
try await syncEngine.processPendingRecordZoneChanges(scope: .shared)

try await self.userDatabase.userRead { db in
try await self.userDatabase.read { db in
try #expect(Reminder.all.fetchCount(db) == 0)
}
assertInlineSnapshot(of: container, as: .customDump) {
Expand Down Expand Up @@ -432,7 +432,7 @@
}
try await syncEngine.processPendingRecordZoneChanges(scope: .shared)

try await self.userDatabase.userRead { db in
try await self.userDatabase.read { db in
try #expect(RemindersList.find(1).fetchOne(db) == RemindersList(id: 1, title: "Personal"))
}
assertInlineSnapshot(of: container, as: .customDump) {
Expand Down
1 change: 1 addition & 0 deletions Tests/SQLiteDataTests/CustomFunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Testing
Date(timeIntervalSinceReferenceDate: 0)
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func basics() throws {
var configuration = Configuration()
configuration.prepareDatabase { db in
Expand Down
29 changes: 2 additions & 27 deletions Tests/SQLiteDataTests/Internal/UserDatabaseHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,22 @@ import GRDB
import SQLiteData

extension UserDatabase {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func userWrite<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
) async throws -> T {
try await write { db in
try SyncEngine.$_isSynchronizingChanges.withValue(false) {
try updates(db)
}
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func userRead<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
) async throws -> T {
try await read { db in
try SyncEngine.$_isSynchronizingChanges.withValue(false) {
try $_isSynchronizingChanges.withValue(false) {
try updates(db)
}
}
}

@_disfavoredOverload
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func userWrite<T>(
_ updates: (Database) throws -> T
) throws -> T {
try write { db in
try SyncEngine.$_isSynchronizingChanges.withValue(false) {
try updates(db)
}
}
}

@_disfavoredOverload
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func userRead<T>(
_ updates: (Database) throws -> T
) throws -> T {
try write { db in
try SyncEngine.$_isSynchronizingChanges.withValue(false) {
try $_isSynchronizingChanges.withValue(false) {
try updates(db)
}
}
Expand Down