-
Notifications
You must be signed in to change notification settings - Fork 77
Fixes to date synchronization. #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mbrandonw
wants to merge
1
commit into
main
Choose a base branch
from
fix-date-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,16 @@ import Foundation | |
| extension Date { | ||
| @usableFromInline | ||
| var iso8601String: String { | ||
| let nextUpDate = Date(timeIntervalSinceReferenceDate: timeIntervalSinceReferenceDate.nextUp) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual fix for the date issue. |
||
| if #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) { | ||
| return formatted(.iso8601.currentTimestamp(includingFractionalSeconds: true)) | ||
| return | ||
| nextUpDate | ||
| .formatted( | ||
| .iso8601.currentTimestamp(includingFractionalSeconds: true) | ||
| ) | ||
| } else { | ||
| return DateFormatter.iso8601(includingFractionalSeconds: true).string(from: self) | ||
| return DateFormatter.iso8601(includingFractionalSeconds: true) | ||
| .string(from: nextUpDate) | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import DependenciesTestSupport | ||
| import Foundation | ||
| import SQLiteData | ||
| import SQLiteDataTestSupport | ||
| import Testing | ||
|
|
||
| @Suite(.dependency(\.defaultDatabase, try .database())) | ||
| struct DateTests { | ||
| @Dependency(\.defaultDatabase) var database | ||
|
|
||
| @Test func roundtrip() throws { | ||
| let date = Date(timeIntervalSinceReferenceDate: 793109282.061) | ||
| let insertedRecord = try database.write { db in | ||
| try Record.insert { Record.Draft(date: date) } | ||
| .returning(\.self) | ||
| .fetchOne(db)! | ||
| } | ||
| let updatedRecord = try database.write { db in | ||
| try Record | ||
| .update(insertedRecord) | ||
| .returning(\.self) | ||
| .fetchOne(db)! | ||
| } | ||
| #expect(insertedRecord.date == date) | ||
| #expect(insertedRecord.date == updatedRecord.date) | ||
| } | ||
| } | ||
|
|
||
| @Table | ||
| private struct Record: Equatable { | ||
| let id: Int | ||
| var date: Date | ||
| } | ||
|
|
||
| extension DatabaseWriter where Self == DatabaseQueue { | ||
| fileprivate static func database() throws -> DatabaseQueue { | ||
| let database = try DatabaseQueue() | ||
| try database.write { db in | ||
| try #sql( | ||
| """ | ||
| CREATE TABLE "records" ( | ||
| "id" INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| "date" TEXT NOT NULL | ||
| ) STRICT | ||
| """ | ||
| ) | ||
| .execute(db) | ||
| } | ||
| return database | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a somewhat unrelated fixed, but it was exacerbated by the date problem. We should only skip updating a local field of a record if there are pending iCloud changes for that record. That's because in that situation we interpret that to mean the user made local changes that will eventually be sent out to iCloud.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, how is it possible for a row to have a modification without having pending changes enqueued in the sync engine? Aren’t row changes automatically picked up by a trigger and enqueued?
Or is this a guardrail against false positives from the equality check? If so, would it really work, as there might be unrelated changes to other fields of the same record?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lukaskubanek It should not be possible, but 1) the user of the library could do something bad like make changes to the database with a different SQLite connection than what the
SyncEnginehas, and 2) some types may not roundtrip cleanly to and from SQLite (such as dates). Whenever either of those happen we don't want that field to be forever stuck and never update. Adding this condition allows us to kick things in the butt if we detect a change to a field that has no corresponding pending change.Yeah, I believe this is not 100% foolproof, but it works one particular use case now, and we have good test coverage on it, so in the future we could explore more robust solutions.