fix(scans): prevent shelf associations from being cleared during metadata operations (#1947)

* fix: ensure book metadata is eagerly loaded for updates and rescans

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* Fix failing tests in LibraryRescanHelperTest and MetadataControllerTest

- Update LibraryRescanHelper to handle null and deleted books gracefully during rescan.
- Update LibraryRescanHelperTest to mock BookRepository correctly.
- Update MetadataControllerTest to mock findAllWithMetadataByIds.

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-20 14:39:09 -07:00
committed by GitHub
parent ab36fa4ab6
commit 1b4bdc2ddb
5 changed files with 32 additions and 24 deletions
@@ -64,7 +64,9 @@ public class MetadataController {
@Parameter(description = "Metadata update wrapper") @RequestBody MetadataUpdateWrapper metadataUpdateWrapper,
@Parameter(description = "ID of the book") @PathVariable long bookId,
@Parameter(description = "Merge categories") @RequestParam(defaultValue = "true") boolean mergeCategories) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
BookEntity bookEntity = bookRepository.findAllWithMetadataByIds(java.util.Collections.singleton(bookId)).stream()
.findFirst()
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
MetadataUpdateContext context = MetadataUpdateContext.builder()
.bookEntity(bookEntity)
@@ -9,6 +9,7 @@ import com.adityachandel.booklore.model.entity.LibraryEntity;
import com.adityachandel.booklore.model.websocket.TaskProgressPayload;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.metadata.BookMetadataUpdater;
import com.adityachandel.booklore.service.metadata.extractor.MetadataExtractorFactory;
@@ -32,13 +33,15 @@ public class LibraryRescanHelper {
private final BookMetadataUpdater bookMetadataUpdater;
private final NotificationService notificationService;
private final TaskCancellationManager cancellationManager;
private final BookRepository bookRepository;
public LibraryRescanHelper(LibraryRepository libraryRepository, MetadataExtractorFactory metadataExtractorFactory, @Lazy BookMetadataUpdater bookMetadataUpdater, NotificationService notificationService, TaskCancellationManager cancellationManager) {
public LibraryRescanHelper(LibraryRepository libraryRepository, MetadataExtractorFactory metadataExtractorFactory, @Lazy BookMetadataUpdater bookMetadataUpdater, NotificationService notificationService, TaskCancellationManager cancellationManager, BookRepository bookRepository) {
this.libraryRepository = libraryRepository;
this.metadataExtractorFactory = metadataExtractorFactory;
this.bookMetadataUpdater = bookMetadataUpdater;
this.notificationService = notificationService;
this.cancellationManager = cancellationManager;
this.bookRepository = bookRepository;
}
@Transactional
@@ -46,9 +49,7 @@ public class LibraryRescanHelper {
LibraryEntity library = libraryRepository.findById(context.getLibraryId()).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(context.getLibraryId()));
List<BookEntity> bookEntities = library.getBookEntities().stream()
.filter(b -> b != null && (b.getDeleted() == null || !b.getDeleted()))
.toList();
List<BookEntity> bookEntities = bookRepository.findAllWithMetadataByLibraryId(library.getId());
log.info("Found {} book(s) to process in library id={}", bookEntities.size(), library.getId());
@@ -58,6 +59,10 @@ public class LibraryRescanHelper {
sendTaskProgressNotification(taskId, 0, String.format("Starting rescan for library: %s", library.getName()), TaskStatus.IN_PROGRESS);
for (BookEntity bookEntity : bookEntities) {
if (bookEntity == null || (bookEntity.getDeleted() != null && bookEntity.getDeleted())) {
continue;
}
if (taskId != null && cancellationManager.isTaskCancelled(taskId)) {
log.info("Library rescan for library {} was cancelled", library.getId());
sendTaskProgressNotification(taskId, (processedBooks * 100) / totalBooks,
@@ -237,7 +237,9 @@ public class BookMetadataService {
}
private BookMetadata updateCover(Long bookId, BiConsumer<MetadataWriter, BookEntity> writerAction) {
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
BookEntity bookEntity = bookRepository.findAllWithMetadataByIds(java.util.Collections.singleton(bookId)).stream()
.findFirst()
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
bookEntity.getMetadata().setCoverUpdatedOn(Instant.now());
MetadataPersistenceSettings settings = appSettingService.getAppSettings().getMetadataPersistenceSettings();
boolean saveToOriginalFile = settings.isSaveToOriginalFile();