From d19bb909b3fb1cfc117d2bb044f7d701f8d8f284 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 22 Nov 2024 17:20:31 -0600 Subject: [PATCH] Fix:Server crash deleting library that has playback sessions #3634 --- server/Database.js | 5 ----- server/controllers/LibraryController.js | 15 ++++++++++++++- server/models/Library.js | 13 ------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/server/Database.js b/server/Database.js index 9bce2605..95e13c6b 100644 --- a/server/Database.js +++ b/server/Database.js @@ -406,11 +406,6 @@ class Database { return Promise.all(oldBooks.map((oldBook) => this.models.book.saveFromOld(oldBook))) } - removeLibrary(libraryId) { - if (!this.sequelize) return false - return this.models.library.removeById(libraryId) - } - createBulkCollectionBooks(collectionBooks) { if (!this.sequelize) return false return this.models.collectionBook.bulkCreate(collectionBooks) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 0bd499f1..84d6193d 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -504,8 +504,21 @@ class LibraryController { await this.handleDeleteLibraryItem(libraryItem.mediaType, libraryItem.id, mediaItemIds) } + // Set PlaybackSessions libraryId to null + const [sessionsUpdated] = await Database.playbackSessionModel.update( + { + libraryId: null + }, + { + where: { + libraryId: req.library.id + } + } + ) + Logger.info(`[LibraryController] Updated ${sessionsUpdated} playback sessions to remove library id`) + const libraryJson = req.library.toOldJSON() - await Database.removeLibrary(req.library.id) + await req.library.destroy() // Re-order libraries await Database.libraryModel.resetDisplayOrder() diff --git a/server/models/Library.js b/server/models/Library.js index 4a69e4cd..708880aa 100644 --- a/server/models/Library.js +++ b/server/models/Library.js @@ -107,19 +107,6 @@ class Library extends Model { }) } - /** - * Destroy library by id - * @param {string} libraryId - * @returns - */ - static removeById(libraryId) { - return this.destroy({ - where: { - id: libraryId - } - }) - } - /** * Get all library ids * @returns {Promise} array of library ids