mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
Database clean author computed columns at startup
This commit is contained in:
parent
94bd8f4110
commit
6b97fc0ce6
1 changed files with 35 additions and 1 deletions
|
|
@ -203,12 +203,44 @@ class Database {
|
||||||
await this.addTriggers()
|
await this.addTriggers()
|
||||||
|
|
||||||
await this.loadData()
|
await this.loadData()
|
||||||
|
await this.rebuildAuthorRows()
|
||||||
|
|
||||||
Logger.info(`[Database] running ANALYZE`)
|
Logger.info(`[Database] running ANALYZE`)
|
||||||
await this.sequelize.query('ANALYZE')
|
await this.sequelize.query('ANALYZE')
|
||||||
Logger.info(`[Database] ANALYZE completed`)
|
Logger.info(`[Database] ANALYZE completed`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rebuild all author rows so derived fields stay in sync with the model logic.
|
||||||
|
*/
|
||||||
|
async rebuildAuthorRows() {
|
||||||
|
const pageSize = 500
|
||||||
|
let offset = 0
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const authors = await this.authorModel.findAll({
|
||||||
|
attributes: ['id', 'name', 'libraryId'],
|
||||||
|
order: [['id', 'ASC']],
|
||||||
|
limit: pageSize,
|
||||||
|
offset
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!authors.length) break
|
||||||
|
|
||||||
|
for (const authorRef of authors) {
|
||||||
|
const author = await this.authorModel.findByPk(authorRef.id)
|
||||||
|
if (!author) continue
|
||||||
|
author.changed('name', true)
|
||||||
|
await author.save({ silent: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += authors.length
|
||||||
|
if (authors.length < pageSize) break
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.debug(`Sanitized ${offset} author rows`)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to db
|
* Connect to db
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
|
|
@ -624,7 +656,9 @@ class Database {
|
||||||
if (!this.libraryFilterData[libraryId]) {
|
if (!this.libraryFilterData[libraryId]) {
|
||||||
return (await this.authorModel.getByNameAndLibrary(authorName, libraryId))?.id || null
|
return (await this.authorModel.getByNameAndLibrary(authorName, libraryId))?.id || null
|
||||||
}
|
}
|
||||||
return this.libraryFilterData[libraryId].authors.find((au) => au.name === authorName)?.id || null
|
const searchName = this.authorModel.normalizeSearchName(authorName)
|
||||||
|
if (!searchName) return null
|
||||||
|
return this.libraryFilterData[libraryId].authors.find((au) => this.authorModel.normalizeSearchName(au.name) === searchName)?.id || null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue