Skip to content

fix(persistence): delete force_unit_edits on save - #279

Merged
DeepDiver1975 merged 1 commit into
mainfrom
fix/review-force-unit-edits
Sep 11, 2026
Merged

DeepDiver1975 merged 1 commit into
mainfrom
fix/review-force-unit-edits

Conversation

@DeepDiver1975

Copy link
Copy Markdown
Contributor

Problem

IncidentRepository.Save rewrites the whole aggregate by deleting 15 tables and re-inserting everything, but the DELETE list omitted force_unit_edits. Every save re-inserted a unit's whole Stärke history with fresh GUID ids on top of what was already on disk. A file saved N times ended up with N copies of every Stärke correction; Load read them all back, so the in-memory history and the PDF Wert-Historie showed duplicates and the file grew with every save.

The existing Save_is_idempotent_overwrite_not_append test saves twice but only asserts on incident_meta, which is why this slipped through.

Fix

  • Add force_unit_edits to the DELETE list in IncidentRepository.Save.
  • Migration V21 heals already-affected .fwincident files by keeping one row per (unit_id, ordinal) (duplicates are content-identical, earliest rowid wins).
  • Regression test: save twice with a surviving edited unit, assert both the row count and the loaded Edits.Count.
  • Migration test: a V20 file with duplicated rows comes back deduplicated and in ordinal order after Load.
  • CHANGELOG entry under Unreleased.

Found during a code review of main; part of a series of review follow-up PRs.

Test plan

  • dotnet test LageBuch.Desktop.slnf green (1101 passed, 1 pre-existing skip)
  • New regression test shown failing before the fix

🤖 Generated with Claude Code

https://claude.ai/code/session_01PXhXSh2pae9WnaACYUUe3V

@DeepDiver1975

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Claude finished @DeepDiver1975's task in 2m 1s —— View job


Review of #279 — fix(persistence): delete force_unit_edits on save

  • Gather context (diff, changed files)
  • Understand the request (code review)
  • Review IncidentRepository.cs change
  • Review Migrations.cs V21 migration logic
  • Review regression/migration tests
  • Review CHANGELOG entry
  • Post final review feedback

Summary: This is a clean, well-scoped fix. The root cause (force_unit_edits missing from the DELETE list in IncidentRepository.Save, src/LageBuch.Persistence/IncidentRepository.cs:23) is correctly diagnosed and fixed with a single-line addition, matching the pattern used for every other child table.

Correctness checks I traced through:

  • force_unit_edits.id is a TEXT PRIMARY KEY (Migrations.cs:491), so it doesn't alias SQLite's rowid — the V21 migration's DELETE ... WHERE rowid NOT IN (SELECT MIN(rowid) ... GROUP BY unit_id, ordinal) is valid and correctly keeps the earliest-inserted row per unit/ordinal.
  • The "duplicates are content-identical" claim holds: ordinal is the edit's index within the unit's Edits list at save time (IncidentRepository.cs:148, j reset per unit), and edit records are immutable history entries that are never mutated after creation — so re-inserting under the bug always reproduces byte-identical rows for existing ordinals, only genuinely new edits get new ordinals.
  • Load's ORDER BY ordinal on force_unit_edits (IncidentRepository.cs:478) is a global sort with no unit_id partition, but since ordinal is monotonically increasing per unit, the subsequent GroupBy(UnitId) still preserves correct intra-unit order regardless of interleaving with other units — not a bug, just a slightly non-obvious detail (already there pre-PR, not introduced by this change).
  • The fix and the DELETE loop both run inside the same transaction as the reinsert, so the migration/fix is atomic with the rest of Save.
  • Both new tests are meaningful, not just smoke tests: Save_does_not_duplicate_force_unit_edits_on_repeated_saves asserts both raw row count and Load(...).Edits.Count, and V20_duplicated_force_unit_edits_migrate_to_v21_deduplicated checks post-migration row count plus that Load returns edits in correct ordinal order (edits[0]/edits[1] values), not just deduplicated count.

Minor, non-blocking observations:

  • If a .fwincident file previously had a force unit removed and then was resaved under the pre-fix bug, force_unit_edits rows for that now-gone unit_id would be orphaned (no matching force_units row) and V21 won't clean those up — it only dedupes by (unit_id, ordinal), not by orphan status. This looks harmless in practice (orphaned rows are simply never looked up via strengthEditsByUnit.TryGetValue), just a small amount of permanent dead data left in already-affected files. Not worth blocking on, but could be a one-line addendum to V21 if the author wants files fully clean (e.g., DELETE FROM force_unit_edits WHERE unit_id NOT IN (SELECT id FROM force_units)).
  • The migration comment on ApplyV21 (Migrations.cs:637-641) is a good example of explaining why, consistent with repo conventions.
  • CHANGELOG entry is clear and correctly placed under ### Fixed in Unreleased.

No blocking issues found. Nice catch, and the regression test approach (asserting row count and loaded aggregate state, not just one) directly closes the gap that let the original bug slip through Save_is_idempotent_overwrite_not_append.

Save's DELETE list omitted force_unit_edits, so every save re-inserted
a unit's whole Wert-Historie on top of what was already on disk. A
file saved N times ended up with N copies of every Stärke correction,
and Load read them all back, so the in-memory history and the PDF
Wert-Historie showed duplicates.

Add force_unit_edits to the DELETE list, and add migration V21 to
heal existing files by keeping one row per (unit_id, ordinal).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PXhXSh2pae9WnaACYUUe3V
Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com>
@DeepDiver1975
DeepDiver1975 force-pushed the fix/review-force-unit-edits branch from 4520111 to 59e1f2a Compare September 11, 2026 08:46
@DeepDiver1975
DeepDiver1975 merged commit 0682a30 into main Sep 11, 2026
3 checks passed
@DeepDiver1975
DeepDiver1975 deleted the fix/review-force-unit-edits branch September 11, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant