fix: restore a profile holding a Bilan transfer, a missing category or a category under a newer one (#521) #532

Merged
maximus merged 4 commits from issue-521-restore-fk-failures into main 2026-11-07 18:58:33 +00:00

4 commits

Author SHA1 Message Date
le king fu
eafd67de51 docs(state): name the manual checks of the restore fixes (#521)
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 3m5s
The line of the stack at the end of "Vérifications dues à la release
suivante" now names #521 and PR #532 next to #518 and PR #531: the
procedure lives in the body of PR #532, section "Vérifications
manuelles dues (prochaine release)". Link 3 of the stack extends this
line.

Resolves #521

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 22:48:41 -05:00
le king fu
df8da7881b docs: the Bilan transfers, the missing categories and the two passes of a restore (#521)
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 3m8s
- CHANGELOG.md / CHANGELOG.fr.md, Fixed / Corrigé: a profile with a
  Bilan transfer, a transaction whose category is gone, or a category
  under a newer one restores again; the transfers whose transaction
  comes back unchanged are kept, the others unlinked and counted before
  and after; a file whose references cannot be satisfied is refused;
  "Rétablir la sauvegarde" reads the file before a second confirmation
  and reloads on "Fermer".
- ADR 0010 amendment: RESTRICT unchanged; the restore reads, removes and
  puts back the transfers whose transaction returns with the same id,
  date, amount and description, and says how many it unlinks.
- docs/architecture.md: dataExportService.ts and dataRestoreService.ts
  rows, useDataImport, the two passes of restoreCategories, the SREF
  section, the node:sqlite tests of restore.test.ts.
- User guide and in-app docs (docs.settings.tips, appended last so that
  tips[6], which REVIEW.md cites, keeps its index).
- Two comments of attachTransactions name the app's real labels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 22:40:54 -05:00
le king fu
32b7c2e518 test: pin the transfers' creation dates, renumber rows no transfer holds (#521)
The creation date of a restored transfer is now compared against a value
set apart from the clock, so a restore that wrote its own would show.
The file whose rows do not keep their ids now gives a duplicate id to
two rows no transfer holds: the linked rows come back unchanged in the
file, and only the renumbering by SQLite unlinks them, which the preview
must announce too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 22:34:12 -05:00
le king fu
23079da609 fix: restore a profile holding a Bilan transfer, a missing category or a category under a newer one (#521)
Three foreign keys stopped a restore on "FOREIGN KEY constraint failed",
the whole restore rolled back:

- (a) balance_account_transfers.transaction_id is ON DELETE RESTRICT
  (ADR 0010): the DELETE FROM transactions of both transaction modes,
  and so "Rétablir la sauvegarde", failed as soon as one transfer
  existed. Inside the restore's transaction, each transfer is now read
  with the date, amount and description of its transaction, then the
  table is emptied; once the transactions are back, the transfers whose
  transaction returns with the same id, date, amount and description
  are put back as they were (id, account, direction, notes, creation
  date), and the others are unlinked.
- (b) a transaction whose category the profile no longer holds (after
  "Réinitialiser", another restore, or another profile's file) failed
  its insert: it now comes back without a category, manual flag at 0,
  and one Journal line gives their number, nothing else.
- (c) restoreCategories inserted by id against parent_id, so a category
  under a newer one, or a loop, failed. Every category now goes in with
  parent_id NULL, and a second pass sets the parents.

The three restores return a RestoreOutcome { unlinkedTransfers,
uncategorizedTransactions } through runRestore<T> (categories only:
zeros). previewUnlinkedTransfers counts, outside any transaction and by
the same rule, the transfers a restore would unlink.
validateImportedCategoryReferences (pure, dataExportService.ts) refuses,
at the parse and at the head of the two restores that write categories,
a file whose parent_id, keyword or supplier names a category it does
not hold (danglingCategoryReference), before anything is deleted.

The "Gestion des données" card announces the transfers to unlink in its
confirmation and gives the real number after the restore (IMPORT_SUCCESS
carries the outcome). restoreFromBackup is split into readBackup (read,
decrypt, parse, count) and applyBackup (restore, preferences, a
RestoreResult with the outcome): "Rétablir la sauvegarde" reads the file
(after the PIN when encrypted) before a second confirmation, ends on
"Sauvegarde rétablie", and reloads the app on "Fermer" only.

Regression test in src/__integration__/restore.test.ts (real services
on node:sqlite, consolidated_schema.sql, foreign keys on): red on the
base for the three causes, green here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 22:32:49 -05:00