feat: manual, reversible same-book mappings between Open Library works #8
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "feat/canonical-book-mapping"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Canonical book mapping
A reader can now say that an Open Library work is the same book as one of their saved books (a duplicate record, or a translation filed as its own work), and undo it later. Mapping is manual only. Nothing is merged automatically.
Changes
0012)book_work_mappingtable:(user_id, work_id) → canonical_work_id, plus display snapshots of both titles.library_book.merged_into_idcolumn, with a composite self-FK that hasON DELETE CASCADE.server/utils/book-mapping.ts,POST/DELETE /api/library/mappings)UiCommandlist, searchable by title).Decisions
work_idis ever rewritten. Everything resolves through the mapping table.merged_into_id). That entry keeps its own lists untouched, and its lists are merged into the chosen entry.Verification
pnpm lint-fullpasses.Demo
Search "petit prince": map the separate "Petit Prince" work onto the saved "Le petit prince" (picked from the suggestions). The card now shows the saved state with Undo. In the library, merge "El principito" (Read) into "Le petit prince", then Undo, which restores it with its lists.
Demo video
Review of the six commits in
master...feat/canonical-book-mapping.Important — required before merge
[P2] Lock both entries before reading memberships for the merge —
server/utils/book-mapping.ts:46–57(also:65–68).The identity advisory lock does not serialize with
updateMemberships(server/utils/library.ts:148–154), which only locks the book row. Neither source nor target is row-locked beforemergeEntryreads their memberships. For example, an Edit lists request can lock the source and replace its lists while a mapping request reads its old committed memberships. The mapping copies those old memberships, waits at the final source-row update, then hides the source after the edit commits. Both requests succeed, but the source's newly added lists/status are absent from the visible merged entry. Acquire compatible locks on both book rows before taking the membership snapshot, or have membership edits participate in the same identity lock.[P2] Preserve the imported work identity when reusing Goodreads matches after undo —
server/utils/library-import.ts:53–54,:80(interaction with:120–129).Reproduce: save B; map an unsaved catalog work A onto B; import Goodreads row G matched to A; undo A's mapping; upload G again. The first import resolves A to B and stores B's entry ID in
library_import_item.bookId. The repeat-import shortcut reconstructs the match from that joined B row, ignoring the original A candidate retained byfinishItem. It therefore continues importing G into B even though A and B are now separate. Preserve/reuse the originally confirmed work metadata and resolve it against the current mappings when repeating an import, rather than treating the previous destination entry as the confirmed work identity. Keep the existing reuse behavior for manually created matches.[P2] Combine memberships when undo reparents multiple hidden entries —
server/utils/book-mapping.ts:114–119.Reproduce: map unsaved A onto saved B; remove B (the catalog mapping survives); save A; save C and D with different custom lists/statuses and map both onto the saved A entry. The mappings are A→B, C→B, D→B, with C and D hidden under A. Undo A→B.
restoreSeparatedEntriesrestores the earliest of C/D and reparents the other underneath it, but only changesmergedIntoId: it never unions their lists or applies the most-advanced-status rule. C and D still resolve to the same book, yet their visible entry lacks the other hidden entry's lists/status; those lists only remain on the now-separate A entry. When rebuilding a remaining merged group, consolidate its memberships into its new visible entry while preserving the hidden rows' own memberships for later undo.Categories: important 3; nit picks 0; pre-existing 0; security 0.
Validation:
pnpm lint-fullpassed. No additional upstream Open Library requests found in the PR. Review only; no repository code or tests changed.Thanks. I accepted and fixed all three findings, one commit each.
pnpm lint-fullpasses.5d6f774):mergeEntrynow row-locks both entries, always in id order, before it reads their memberships. A concurrent Edit lists request then either commits before the snapshot is taken or waits until the mapping finishes.57b8f28): a repeated import now reuses the catalogue work that was originally confirmed (the import item's storedcandidates[0]) and resolves it through the current mappings. It no longer reuses the entry that work was merged into. Manually created books still reuse their own entry. The "only when the saved entry still exists" rule is unchanged.f70128a): when undo leaves several hidden entries of one book together, the earliest one is restored. The others now merge into it throughmergeEntry, so it gains their lists with the usual rules (custom lists combined, most advanced status wins). The hidden entries keep their own lists for a later undo. I checked your scenario against the dev server: C (Reading) came back with D's Read status and D's custom list, and A kept the combined lists.Master had not moved, so no rebase or migration regeneration was needed.