(Refactor) Extract file-specific information from book (#1734)

* refactor(book): extract file-specific information from book

First commit in a series aimed at refactoring the data model for books.
More specifically, the idea is to extract all file-specific information
from the `book` table and move it to the `book_file` table, previously named `book_additional_file`

The aim is to make it easier to:
* Improve support for books with multiple file formats (PDF, EPUB, etc) for all interactions (Read, Download, etc)
* Support for merging/unmerging books
* Add support for additional file types
* Specify preferred formats at the user level

Ref: #489

* refactor(book): ensure the API build and runs

Further work on the refactoring aimed at separating file-related details
from the `book` table.

With this commit all the missing changes that were prventing the API to build
or to book have been addressed.

TODO: test extensively, adjust existing unit tests and add new ones

* fix(read): add mapping for book format

This restores the read functionality which relies on the book format field
to decide which reader to use.

* fix: fix read, dowload and file upload

This commit fixes multiple issues either caused by the refactoring or pre-existing:

* Fix the Read button behaviour after the refactoring
* Unregister the watcher process when uploading additional formats
* Fix downloading of additional book formats (using the wrong ID)

* fix: adjust tests to use the new BookFileEntity class

All the tests that used to fecth file information from BookEntity
now need to get them from the relevant BookFileEntity

* fix: do not rely on AdditionalFileType

* Use the BookFileEntity bookFormat instead

* fix: use the relevant BookFileEntity class

* fix: call the right methods

* fix: Add missing mapping for the test

* fix: adapt the test to the new semantics

All book files, including the primary one, are treated as equal now.
The tests needs to take that into account when checking for additional
formats.

* fix: use mutable lists

* fix: fix syntax for droppung unique constraint

MariaDB uses indexes, not constraints

* fix: regression on book file ordering

We want to make this refactoring 100% compatible with the current behaviour (modulo a few bugs),
therefore we need to maintain the right order to ensure the "primary" book stays the same
after the migration.

* fix: allow download of supplementary files

* fix(opds): replace removed additionalFiles entity graph with bookFiles

- Update BookOpdsRepository @EntityGraph paths to use BookEntity.bookFiles after the refactor
- Add @DataJpaTest to validate BookOpdsRepository wiring and catch invalid EntityGraph attributes
- Add H2 as testRuntimeOnly dependency so the JPA slice test can run with an embedded DB

* chore(bookdrop): mount bookdrop folder from a local directory

It's consistent with the library dir, and makes debugging easier
when working on the local environment

* fix: rename migration after rebase

It's no longer 66, bumped to 73

* fix: handle BookEntity primary file NPEs after rebase

Adjust tests to always instanciate BookFileEntity when manipulating
BookEntity.

* chore: rename migration to avoid conflict

V73 is already taken on develop, V67 was left "unused"

* chore: rename again to ensure it's applied

* fix: make sure to flush the data to DB

Without the flush there is a high chance of leaving the DB in an inconsistent
state after a book move.

* fix: move all files belonging to a book

This fixes a pre-existing bug which has some nasty ramifications.
We never moved "additional files" when changing library for a book entity,
causing them to become effectively "unreacheable" from the UI.

* fix(migration): remove the unique index before importing data

* fix: fix build and test after rebase

* fix(migration): drop legacy table

* fix(upload): use the templetized name when storing on DB

* fix(rebase): Add logical fixes post rebase

* Adapt the code to properly handle the new `archive_type` field and logic
* Bump the version number for the DB migration
* Use `getPrimaryBookFile()` whenever trying to access book files

* fix(migration): Handle additional book formats

* Add support for FB2 files
* Corretly handle cb7 files as CBX

* fix(file mover): fix a regression when moving books across categories

The previous approach would trigger the JPA's `orphanRemoval` parameter on the
bookEntities, effetively triggering a delete on the DB.

This caused files to be moved but the data on the DB would get stale, also causing
additional formats to be treated as separate books upon a rescan.

* fix(rescan): do not delete alternative files on rescan

Upon library rescan, additional files such as images were removed from
book entities association when using the "Each file is a book" library structure.
This commit is contained in:
Sergio Visinoni
2026-01-15 18:15:00 -07:00
committed by GitHub
parent 99c8c131f6
commit 2be02017d4
71 changed files with 1455 additions and 573 deletions
@@ -0,0 +1,107 @@
-- Migrate file specific data from book to book_file
RENAME TABLE book_additional_file TO book_file_old;
CREATE TABLE book_file LIKE book_file_old;
ALTER TABLE book_file
ADD CONSTRAINT fk_book_file_book
FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE;
ALTER TABLE book_file ADD COLUMN is_book boolean DEFAULT false;
ALTER TABLE book_file ADD COLUMN book_type varchar(32);
ALTER TABLE book_file ADD COLUMN archive_type VARCHAR(255);
-- Drop the column before importing the data to avoid duplicate index errors
ALTER TABLE book_file DROP COLUMN alt_format_current_hash;
INSERT INTO book_file (book_id, file_name, file_sub_path, is_book, book_type, archive_type, file_size_kb, initial_hash, added_on, current_hash)
SELECT id, file_name, file_sub_path, true, CASE when book_type = 0 then 'PDF' when book_type = 1 then 'EPUB' when book_type = 2 then 'CBX' end, archive_type, file_size_kb, initial_hash, added_on, current_hash FROM book;
INSERT INTO book_file (book_id, file_name, file_sub_path, file_size_kb, initial_hash, current_hash, description, added_on, additional_file_type)
SELECT book_id, file_name, file_sub_path, file_size_kb, initial_hash, current_hash, description, added_on, additional_file_type FROM book_file_old;
UPDATE book_file SET is_book = true WHERE additional_file_type = 'ALTERNATIVE_FORMAT';
ALTER TABLE book_file DROP COLUMN additional_file_type;
-- Set book_type for existing book files
UPDATE book_file
SET book_type = CASE
WHEN LOWER(file_name) LIKE '%.epub' THEN 'EPUB'
WHEN LOWER(file_name) LIKE '%.pdf' THEN 'PDF'
WHEN LOWER(file_name) LIKE '%.cbz' THEN 'CBX'
WHEN LOWER(file_name) LIKE '%.cbr' THEN 'CBX'
WHEN LOWER(file_name) LIKE '%.cb7' THEN 'CBX'
WHEN LOWER(file_name) LIKE '%.fb2' THEN 'FB2'
ELSE book_type
END
WHERE is_book = 1
AND book_type IS NULL;
-- Prevent duplicates of book files (is_book=true) by (library_id, library_path_id, file_sub_path, file_name)
-- MariaDB/MySQL do not support UNIQUE constraints across multiple tables, so we need to enforce via triggers.
DELIMITER $$
CREATE OR REPLACE PROCEDURE assert_no_duplicate_book_file(
IN p_book_file_id BIGINT,
IN p_book_id BIGINT,
IN p_file_name VARCHAR(1000),
IN p_file_sub_path VARCHAR(512),
IN p_is_book BOOLEAN
)
BEGIN
DECLARE v_library_id BIGINT;
DECLARE v_library_path_id BIGINT;
IF p_is_book = true THEN
SELECT b.library_id, b.library_path_id
INTO v_library_id, v_library_path_id
FROM book b
WHERE b.id = p_book_id;
IF v_library_id IS NOT NULL AND EXISTS (
SELECT 1
FROM book_file bf
INNER JOIN book b2 ON b2.id = bf.book_id
WHERE bf.is_book = true
AND bf.file_name = p_file_name
AND bf.file_sub_path = p_file_sub_path
AND b2.library_id = v_library_id
AND b2.library_path_id = v_library_path_id
AND (p_book_file_id IS NULL OR bf.id <> p_book_file_id)
LIMIT 1
) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Duplicate book file detected for library/path/subpath/name';
END IF;
END IF;
END$$
CREATE OR REPLACE TRIGGER trg_book_file_prevent_duplicate_book_insert
BEFORE INSERT ON book_file
FOR EACH ROW
BEGIN
CALL assert_no_duplicate_book_file(NULL, NEW.book_id, NEW.file_name, NEW.file_sub_path, NEW.is_book);
END$$
CREATE OR REPLACE TRIGGER trg_book_file_prevent_duplicate_book_update
BEFORE UPDATE ON book_file
FOR EACH ROW
BEGIN
CALL assert_no_duplicate_book_file(OLD.id, NEW.book_id, NEW.file_name, NEW.file_sub_path, NEW.is_book);
END$$
DELIMITER ;
-- Regenerate virtual column for alternative book format files, create the index without UNIQUE constraint
ALTER TABLE book_file ADD COLUMN alt_format_current_hash VARCHAR(128) AS (CASE WHEN is_book = true THEN current_hash END) STORED;
ALTER TABLE book_file ADD INDEX idx_book_file_current_hash_alt_format (alt_format_current_hash);
-- Remove constraint from book table
ALTER TABLE book DROP INDEX IF EXISTS unique_library_file_path;
-- Remove migrated fields from the book table
ALTER TABLE book DROP COLUMN file_name;
ALTER TABLE book DROP COLUMN file_sub_path;
ALTER TABLE book DROP COLUMN book_type;
ALTER TABLE book DROP COLUMN file_size_kb;
ALTER TABLE book DROP COLUMN initial_hash;
ALTER TABLE book DROP COLUMN current_hash;
ALTER TABLE book DROP COLUMN archive_type;
DROP TABLE book_file_old;