Skip to content

Commit 4e01722

Browse files
authored
Merge pull request #2103 from selfhost-alt/faster-scan-for-empty-series
Scan for empty book series more efficiently
2 parents 817be40 + 87eaace commit 4e01722

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

server/Database.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,11 @@ class Database {
666666
async cleanDatabase() {
667667
// Remove invalid Podcast records
668668
const podcastsWithNoLibraryItem = await this.podcastModel.findAll({
669-
where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM libraryItems li WHERE li.mediaId = podcast.id)`), 0)
669+
include: {
670+
model: this.libraryItemModel,
671+
required: false
672+
},
673+
where: { '$libraryItem.id$': null }
670674
})
671675
for (const podcast of podcastsWithNoLibraryItem) {
672676
Logger.warn(`Found podcast "${podcast.title}" with no libraryItem - removing it`)
@@ -675,7 +679,11 @@ class Database {
675679

676680
// Remove invalid Book records
677681
const booksWithNoLibraryItem = await this.bookModel.findAll({
678-
where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM libraryItems li WHERE li.mediaId = book.id)`), 0)
682+
include: {
683+
model: this.libraryItemModel,
684+
required: false
685+
},
686+
where: { '$libraryItem.id$': null }
679687
})
680688
for (const book of booksWithNoLibraryItem) {
681689
Logger.warn(`Found book "${book.title}" with no libraryItem - removing it`)
@@ -684,7 +692,11 @@ class Database {
684692

685693
// Remove empty series
686694
const emptySeries = await this.seriesModel.findAll({
687-
where: Sequelize.where(Sequelize.literal(`(SELECT count(*) FROM bookSeries bs WHERE bs.seriesId = series.id)`), 0)
695+
include: {
696+
model: this.bookSeriesModel,
697+
required: false
698+
},
699+
where: { '$bookSeries.id$': null }
688700
})
689701
for (const series of emptySeries) {
690702
Logger.warn(`Found series "${series.name}" with no books - removing it`)

0 commit comments

Comments
 (0)