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>