fix: delete a library's ebooks when the library is deleted #57

Merged
jfro merged 2 commits from jtk/library-delete-ebooks into main 2026-10-18 22:39:50 +00:00
Owner

Deleting a library nilified ebooks.library_id (the FK is on_delete: :nilify_all). That left ebook records with no library, which no scan would ever clean up and which kept showing as downloads on their books.

Change (Libraries.delete_library/1)

  • Deletes the library's ebooks and the library together in one Repo.transact. I left the FK alone, so no migration is needed. The old nilify behavior can't bite here because the ebooks are already gone by the time the library row is deleted.
  • After the commit, deletes the ebooks' cover thumbnails, except ones still in use as a books.cover_image_key (the ProcessWorker gives new books the ebook's cover, so this is common) or by another ebook. Covers are deleted after the commit, so a rollback can't leave broken images.
  • Books stay in the catalog, unchanged.
  • Logs the library id and path, the number of ebook records deleted, and how many covers were deleted or kept. Each cover that fails to delete gets a warning with its key.

Left as is, open to change

  • The books keep their ebook collection item, even when the deleted library had their only ebook. Removing it could undo something the user added by hand. I can drop it when no ebook is left, if you'd like.
  • Already-queued ProcessWorker jobs for deleted ebooks raise on Repo.get! and get discarded. JobFailureHandler skips them because the ebook is gone. The admin UI also blocks deleting a library while it's scanning.

Tests

  • The library's ebooks are deleted, ebooks in other libraries stay, and the book stays.
  • Covers: unused thumbnail deleted; one used by a book and one used by another library's ebook are kept; the book's cover_image_key is intact; summary logs checked.
  • The edge-case test for library deletion now checks the ebook is gone.
  • Both new tests fail on main.

mix precommit passes.

Fixes #24

Deleting a library nilified `ebooks.library_id` (the FK is `on_delete: :nilify_all`). That left ebook records with no library, which no scan would ever clean up and which kept showing as downloads on their books. **Change** (`Libraries.delete_library/1`) - Deletes the library's ebooks and the library together in one `Repo.transact`. I left the FK alone, so no migration is needed. The old nilify behavior can't bite here because the ebooks are already gone by the time the library row is deleted. - After the commit, deletes the ebooks' cover thumbnails, **except** ones still in use as a `books.cover_image_key` (the ProcessWorker gives new books the ebook's cover, so this is common) or by another ebook. Covers are deleted after the commit, so a rollback can't leave broken images. - Books stay in the catalog, unchanged. - Logs the library id and path, the number of ebook records deleted, and how many covers were deleted or kept. Each cover that fails to delete gets a warning with its key. **Left as is, open to change** - The books keep their `ebook` collection item, even when the deleted library had their only ebook. Removing it could undo something the user added by hand. I can drop it when no ebook is left, if you'd like. - Already-queued ProcessWorker jobs for deleted ebooks raise on `Repo.get!` and get discarded. `JobFailureHandler` skips them because the ebook is gone. The admin UI also blocks deleting a library while it's scanning. **Tests** - The library's ebooks are deleted, ebooks in other libraries stay, and the book stays. - Covers: unused thumbnail deleted; one used by a book and one used by another library's ebook are kept; the book's `cover_image_key` is intact; summary logs checked. - The edge-case test for library deletion now checks the ebook is gone. - Both new tests fail on main. `mix precommit` passes. Fixes #24
jfro added this to the 0.6.2 milestone 2026-10-18 21:51:04 +00:00
fix: delete a library's ebooks when the library is deleted
All checks were successful
CI / test (pull_request) Successful in 1m45s
de5656b3df
Deleting a library nilified ebooks.library_id, leaving ebook records no
scan would ever clean up, still listed as downloads on their books.
delete_library/1 now deletes the library's ebooks in the same
transaction, then removes their cover thumbnails unless a book or
another ebook still uses them. Books stay in the catalog.

Fixes #24
Author
Owner

Review from Nye (approved)

Checked: no other table references ebooks; delete_all with select returns {count, keys} on Postgres; covers deleted after commit and only when no book/other ebook uses the key; Storage delete returns :ok | {:error, _}; logs have library id/path/counts and per-key warnings.

Nits (non-blocking):

  1. Queued ProcessWorker jobs for deleted ebooks raise Ecto.NoResultsError and burn 3 attempts with error logs; returning {:cancel, "ebook deleted"} when the ebook is gone would be quieter (could ride with #55).
  2. Keeping the ebook collection item: agree, leave it.
Review from Nye (approved) Checked: no other table references ebooks; delete_all with select returns {count, keys} on Postgres; covers deleted after commit and only when no book/other ebook uses the key; Storage delete returns :ok | {:error, _}; logs have library id/path/counts and per-key warnings. Nits (non-blocking): 1. Queued ProcessWorker jobs for deleted ebooks raise Ecto.NoResultsError and burn 3 attempts with error logs; returning {:cancel, "ebook deleted"} when the ebook is gone would be quieter (could ride with #55). 2. Keeping the ebook collection item: agree, leave it.
fix: cancel processing jobs for ebooks that were deleted
All checks were successful
CI / test (pull_request) Successful in 1m46s
cead3f0faa
jfro merged commit 1cdb33971e into main 2026-10-18 22:39:50 +00:00
Sign in to join this conversation.
No reviewers
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
jfro/fuzzy_catalog!57
No description provided.