From 8b39b01269dd29b0fcb19976ceaa0ce2bac9e7ca Mon Sep 17 00:00:00 2001 From: Selfhost Alt Date: Thu, 14 Sep 2023 22:35:33 -0700 Subject: [PATCH 1/3] Scan for empty book series more efficiently --- server/Database.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/Database.js b/server/Database.js index a959d0a9..fe4362f4 100644 --- a/server/Database.js +++ b/server/Database.js @@ -684,7 +684,15 @@ class Database { // Remove empty series const emptySeries = await this.seriesModel.findAll({ - where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)`), 0) + attributes: ['series.id', 'series.name', [this.sequelize.fn('COUNT', '*'), 'book_count']], + include: [ + { + model: this.bookSeriesModel, + required: false + } + ], + group:["series.id", 'series.name'], + having: { 'book_count': 0 } }) for (const series of emptySeries) { Logger.warn(`Found series "${series.name}" with no books - removing it`) From 19cf3bfb9f6c22df0095c65eacdd2d2f2d9f4778 Mon Sep 17 00:00:00 2001 From: Selfhost Alt Date: Fri, 15 Sep 2023 13:32:21 -0700 Subject: [PATCH 2/3] Fix query to actually return empty series --- server/Database.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/Database.js b/server/Database.js index fe4362f4..64a67a1b 100644 --- a/server/Database.js +++ b/server/Database.js @@ -684,15 +684,13 @@ class Database { // Remove empty series const emptySeries = await this.seriesModel.findAll({ - attributes: ['series.id', 'series.name', [this.sequelize.fn('COUNT', '*'), 'book_count']], include: [ { model: this.bookSeriesModel, required: false } ], - group:["series.id", 'series.name'], - having: { 'book_count': 0 } + where:{ '$bookSeries.id$': null } }) for (const series of emptySeries) { Logger.warn(`Found series "${series.name}" with no books - removing it`) From 87eaacea220e43df03cf02d0a5b8a870e509a7ae Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 17 Sep 2023 15:53:25 -0500 Subject: [PATCH 3/3] Fix empty podcast and empty book queries when cleaning db on init --- server/Database.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/server/Database.js b/server/Database.js index 64a67a1b..5b8a71e2 100644 --- a/server/Database.js +++ b/server/Database.js @@ -666,7 +666,11 @@ class Database { async cleanDatabase() { // Remove invalid Podcast records const podcastsWithNoLibraryItem = await this.podcastModel.findAll({ - where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM libraryItems li WHERE li.mediaId = podcast.id)`), 0) + include: { + model: this.libraryItemModel, + required: false + }, + where: { '$libraryItem.id$': null } }) for (const podcast of podcastsWithNoLibraryItem) { Logger.warn(`Found podcast "${podcast.title}" with no libraryItem - removing it`) @@ -675,7 +679,11 @@ class Database { // Remove invalid Book records const booksWithNoLibraryItem = await this.bookModel.findAll({ - where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM libraryItems li WHERE li.mediaId = book.id)`), 0) + include: { + model: this.libraryItemModel, + required: false + }, + where: { '$libraryItem.id$': null } }) for (const book of booksWithNoLibraryItem) { Logger.warn(`Found book "${book.title}" with no libraryItem - removing it`) @@ -684,13 +692,11 @@ class Database { // Remove empty series const emptySeries = await this.seriesModel.findAll({ - include: [ - { - model: this.bookSeriesModel, - required: false - } - ], - where:{ '$bookSeries.id$': null } + include: { + model: this.bookSeriesModel, + required: false + }, + where: { '$bookSeries.id$': null } }) for (const series of emptySeries) { Logger.warn(`Found series "${series.name}" with no books - removing it`)