Store data as JSON in a {mandator}/{year} layout, with startup migration #2

Merged
clayschaad merged 3 commits from json-storage into main 2026-10-22 12:00:17 +00:00
Owner

Summary

  • Repositories read and write JSON instead of XML. The JSON is human readable: indented, umlauts written as-is, property names as in the C# models (BankTransactionText), null values left out, computed properties (Class, SubClass, IsFxAccount, ReadOnly) not stored. Shared settings live in JsonStorage.
  • New data folder layout, defined in DataFolders:
    {DataPath}/{mandator}/{year}/*.json          data files (no Data subfolder)
    {DataPath}/uploads/{mandator}/{year}/        imported bank statements (+ creditCard/)
    {DataPath}/backup/{mandator}/{year}/         backup zips and the original XML files
    
    SettingsService builds the new paths. GetYearMandatorPath / GetLastYearMandatorPath are removed because they now equal the data paths. The mandator dropdown lists mandators that have a folder for the selected year.
  • New DataMigrator runs at startup, before the app serves requests, in two phases:
    1. Restructure: moves the old {year}/{mandator}/Data, {year}/{mandator}/uploads and {year}/backup/{mandator} folders into the new layout, unchanged.
    2. Convert: turns the seven known XML data files in each {mandator}/{year} folder into JSON, checks that the JSON reads back to the same data, then moves the XML to backup/{mandator}/{year}/xml-{timestamp}/. Bank-statement XMLs are moved but never converted.
  • Safety: nothing is ever overwritten. If a file already exists at the target, the source is dropped only when both are identical; otherwise it is an error. Any error stops startup (conversion is skipped if a move failed), so the app never runs on a half-migrated folder. Old folders are removed only once empty; unexpected leftovers are logged and kept. Both phases can safely run again.
  • CLAUDE.md updated for JSON storage and the new layout.

Upgrade notes

  • Make a full copy of the data folder before the first start of this version.
  • Don't run an older (XML) version on a migrated data folder. It would find no data and start new XML files in the old layout.
  • The backup zip now contains only the data files of the current mandator/year. Uploads live in their own folder.
  • Empty folders from the old layout are not carried over; the app recreates them when needed.

Test plan

  • dotnet build: no warnings
  • dotnet test: 77/77 pass, including 15 DataMigratorTestShould tests (old-layout data, uploads with subfolders and year backups moved; XML converted in old and new layout; readable JSON; values round-trip through the repositories; unknown XMLs not converted; rerun does nothing; stray files kept; crash recovery for moves and conversions; conflicting files; corrupt XML) and 4 SettingsServiceTestShould tests for the new paths and mandator list
  • Ran the app against a copy of the real data folder: 136 files moved, 82 XML files converted, no old year folders left, Accounts / Balance report / Transactions pages render
  • Checked every one of the 137 original files against a checksum manifest: uploads and other files byte-identical at their new location, every XML backup identical to the original, and JSON vs. XML compared field by field (11,807 records, 101,564 fields, 0 differences). 8 dates with a UTC offset in the XML are written as the same instant in local time, which is what the old app also had in memory after reading them.
  • Start once on the real data folder and check the pages

🤖 Generated with Claude Code

## Summary - Repositories read and write **JSON instead of XML**. The JSON is human readable: indented, umlauts written as-is, property names as in the C# models (`BankTransactionText`), null values left out, computed properties (`Class`, `SubClass`, `IsFxAccount`, `ReadOnly`) not stored. Shared settings live in `JsonStorage`. - **New data folder layout**, defined in `DataFolders`: ``` {DataPath}/{mandator}/{year}/*.json data files (no Data subfolder) {DataPath}/uploads/{mandator}/{year}/ imported bank statements (+ creditCard/) {DataPath}/backup/{mandator}/{year}/ backup zips and the original XML files ``` `SettingsService` builds the new paths. `GetYearMandatorPath` / `GetLastYearMandatorPath` are removed because they now equal the data paths. The mandator dropdown lists mandators that have a folder for the selected year. - New **`DataMigrator`** runs at startup, before the app serves requests, in two phases: 1. **Restructure:** moves the old `{year}/{mandator}/Data`, `{year}/{mandator}/uploads` and `{year}/backup/{mandator}` folders into the new layout, unchanged. 2. **Convert:** turns the seven known XML data files in each `{mandator}/{year}` folder into JSON, checks that the JSON reads back to the same data, then moves the XML to `backup/{mandator}/{year}/xml-{timestamp}/`. Bank-statement XMLs are moved but never converted. - **Safety:** nothing is ever overwritten. If a file already exists at the target, the source is dropped only when both are identical; otherwise it is an error. Any error stops startup (conversion is skipped if a move failed), so the app never runs on a half-migrated folder. Old folders are removed only once empty; unexpected leftovers are logged and kept. Both phases can safely run again. - `CLAUDE.md` updated for JSON storage and the new layout. ## Upgrade notes - **Make a full copy of the data folder before the first start** of this version. - **Don't run an older (XML) version** on a migrated data folder. It would find no data and start new XML files in the old layout. - The backup zip now contains only the data files of the current mandator/year. Uploads live in their own folder. - Empty folders from the old layout are not carried over; the app recreates them when needed. ## Test plan - [x] `dotnet build`: no warnings - [x] `dotnet test`: 77/77 pass, including 15 `DataMigratorTestShould` tests (old-layout data, uploads with subfolders and year backups moved; XML converted in old and new layout; readable JSON; values round-trip through the repositories; unknown XMLs not converted; rerun does nothing; stray files kept; crash recovery for moves and conversions; conflicting files; corrupt XML) and 4 `SettingsServiceTestShould` tests for the new paths and mandator list - [x] Ran the app against a copy of the real data folder: 136 files moved, 82 XML files converted, no old year folders left, Accounts / Balance report / Transactions pages render - [x] Checked every one of the 137 original files against a checksum manifest: uploads and other files byte-identical at their new location, every XML backup identical to the original, and JSON vs. XML compared field by field (11,807 records, 101,564 fields, 0 differences). 8 dates with a UTC offset in the XML are written as the same instant in local time, which is what the old app also had in memory after reading them. - [ ] Start once on the real data folder and check the pages 🤖 Generated with [Claude Code](https://claude.com/claude-code)
clayschaad added 2 commits 2026-10-22 11:44:40 +00:00
Repositories now read and write indented, human-readable JSON
(System.Text.Json, shared settings in JsonStorage). File names and folders
are unchanged apart from the extension.

XmlToJsonMigrator runs at startup before the app serves requests. It converts
the known Data/*.xml files under every year/mandator, verifies the JSON reads
back to the same data, and moves the XML to
{year}/backup/{mandator}/xml-{timestamp}/. Bank-statement XMLs in uploads/ are
never touched. Any failure stops startup and leaves that file's XML in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Property names now match the C# models (BankTransactionText instead of
bankTransactionText). Reading stays case-insensitive. The migrator test now
checks casing case-sensitively; Shouldly's ShouldContain ignores case by
default, so it never actually checked it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clayschaad added 1 commit 2026-10-22 11:58:17 +00:00
New layout below DataPath (defined in DataFolders):
  {mandator}/{year}/*.json
  uploads/{mandator}/{year}/
  backup/{mandator}/{year}/

XmlToJsonMigrator becomes DataMigrator with two phases: first move the old
{year}/{mandator}/Data, uploads and {year}/backup folders into the new layout
unchanged, then convert the XML data files to JSON. Nothing is overwritten;
an identical file at the target is dropped, a different one stops startup.
Old folders are removed only once empty.

SettingsService builds the new paths; GetYearMandatorPath and
GetLastYearMandatorPath are removed as they now equal the data paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clayschaad changed title from Store data as JSON instead of XML, with startup migration to Store data as JSON in a {mandator}/{year} layout, with startup migration 2026-10-22 11:58:30 +00:00
clayschaad merged commit 6ab5ac133e into main 2026-10-22 12:00:17 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: clayschaad/AccountingNext#2
No description provided.