Skip to content

Commit 05704b5

Browse files
authored
Detect SyncEngine.isSynchronizing misuse in triggers (#381)
* Detect `SyncEngine.isSynchronizing` misuse in triggers A warning is raised to suggest `$isSynchronizing`, instead. * re-record snaps * wip * wip * wip
1 parent 32ca8df commit 05704b5

8 files changed

Lines changed: 269 additions & 212 deletions

File tree

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 31 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let package = Package(
3737
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
3838
.package(
3939
url: "https://github.com/pointfreeco/swift-structured-queries",
40-
from: "0.27.0",
40+
from: "0.29.0",
4141
traits: [
4242
.trait(name: "StructuredQueriesTagged", condition: .when(traits: ["SQLiteDataTagged"]))
4343
]

Package@swift-6.0.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let package = Package(
2828
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.9.0"),
2929
.package(url: "https://github.com/pointfreeco/swift-sharing", from: "2.3.0"),
3030
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
31-
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.27.0"),
31+
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.29.0"),
3232
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.0"),
3333
],
3434
targets: [

Sources/SQLiteData/CloudKit/SyncEngine.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,15 @@
899899
/// See <doc:CloudKit#Updating-triggers-to-be-compatible-with-synchronization> for more info.
900900
@DatabaseFunction("sqlitedata_icloud_syncEngineIsSynchronizingChanges")
901901
public static var isSynchronizing: Bool {
902-
_isSynchronizingChanges
902+
if _isCreatingTemporaryTrigger {
903+
reportIssue(
904+
"""
905+
Invoked 'SyncEngine.isSynchronizing' at trigger creation, which is unexpected. Use \
906+
'SyncEngine.$isSynchronizing' to invoke at trigger execution, instead.
907+
"""
908+
)
909+
}
910+
return _isSynchronizingChanges
903911
}
904912

905913
@available(*, deprecated, message: "Use 'SyncEngine.$isSynchronizing', instead.")

Tests/SQLiteDataTests/AssertQueryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct AssertQueryTests {
136136
"""
137137
SELECT "records"."id", "records"."date"
138138
FROM "records"
139-
WHERE ("records"."id") = (1)
139+
WHERE (("records"."id") = (1))
140140
"""
141141
} results: {
142142
"""

Tests/SQLiteDataTests/CloudKitTests/SyncEngineTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@
9393
"""
9494
)
9595
}
96+
97+
@Test func isSynchronizingTriggerWarning() {
98+
withKnownIssue {
99+
_ = Reminder.createTemporaryTrigger(
100+
after: .insert { new in
101+
Values(SyncEngine.isSynchronizing)
102+
}
103+
)
104+
} matching: { issue in
105+
issue.description.hasSuffix(
106+
"""
107+
Invoked 'SyncEngine.isSynchronizing' at trigger creation, which is unexpected. Use \
108+
'SyncEngine.$isSynchronizing' to invoke at trigger execution, instead.
109+
"""
110+
)
111+
}
112+
_ = Reminder.createTemporaryTrigger(
113+
after: .insert { new in
114+
Values(SyncEngine.$isSynchronizing)
115+
}
116+
)
117+
}
96118
}
97119
}
98120

0 commit comments

Comments
 (0)