fix: correctly migrate AccountingLedgerEntry dates for UTC+ timezones#1521
Draft
the-narwhal wants to merge 1 commit intofrappe:masterfrom
Draft
fix: correctly migrate AccountingLedgerEntry dates for UTC+ timezones#1521the-narwhal wants to merge 1 commit intofrappe:masterfrom
the-narwhal wants to merge 1 commit intofrappe:masterfrom
Conversation
The fixLedgerDateTime patch (v0.21.2) used forEach with async callbacks, which fires promises without awaiting them. The outer await resolved immediately after the SELECT, so no UPDATE statements ever executed. The patch was silently recorded as succeeded in PatchRun, preventing any retry. - Rewrite fixLedgerDateTime using for...of loops with proper await - Add fixLedgerDateTimeRerun patch (v0.37.0) under a new name so users who already had the broken patch recorded as executed get the fix applied on their next launch - Remove orphaned '// end ugly timezone fix code' comment in LedgerPosting
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is a draft PR — feedback, discussion, and testing are very welcome.
Fixes #819 — P&L report shows entries in the wrong year for UTC+ timezone users
Root cause
AccountingLedgerEntry.dateis aDatetimefield serialised by callingDate.prototype.toISOString(). For a UTC+ user (e.g. Malaysia, UTC+8), local midnight on2023-01-01is2022-12-31T16:00:00.000Zin UTC. SQLite stores that literal string, and the P&L filteris a plain string comparison:
"2022-12-31T16:00:00.000Z" < "2023-01-01"→ the entry is silently placed in 2022 instead of 2023.What was already partially in place
timezoneDateTimeAdjuster(added Feb 2024 inLedgerPosting) correctly forces every new ledger entry to UTC midnight ("2023-01-01T00:00:00.000Z"), whose UTC date string always matches the user's intended date. ThefixLedgerDateTimemigration patch was meant to backfill existing entries with the same fix — but it contained a critical async bug.The broken patch
No exception was thrown, so the patch was recorded as
failed: falsein thePatchRuntable and will never retry.Changes
backend/patches/v0_21_0/fixLedgerDateTime.tsfor...of+await— fixes new installs upgrading from old versionsbackend/patches/v0_37_0/fixLedgerDateTimeRerun.tsfixLedgerDateTimeRerun) atv0.37.0— runs for users who already had the broken patch silently executebackend/patches/index.tsmodels/Transactional/LedgerPosting.ts// end ugly timezone fix codecommentWhy a new patch name?
The patch runner only re-executes a patch if it previously recorded
failed: true. Since the broken patch recordedfailed: false, it will never retry under the same name. A new name (fixLedgerDateTimeRerun) atv0.37.0ensures every user whose DB version is ≤0.37.0— i.e. everyone currently running the app — gets the migration applied on their next launch.Who is affected
Only UTC+ timezone users (UTC+8, UTC+5:30, etc.) with entries created before the
timezoneDateTimeAdjusterwas introduced. UTC and UTC− users were never affected because their local midnight falls on the same or a later UTC day, so the stored string already compared correctly.