mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
Move normalization functions into migration file for rollback
This commit is contained in:
parent
4714af0ed1
commit
5378e64eab
1 changed files with 38 additions and 1 deletions
|
|
@ -21,6 +21,43 @@ const AUTHOR_SEARCH_INDEX = 'author_search_name'
|
||||||
const AUTHOR_LAST_FIRST_INDEX = 'author_last_first'
|
const AUTHOR_LAST_FIRST_INDEX = 'author_last_first'
|
||||||
const UNIQUE_SEARCH_INDEX = 'unique_author_search_name_per_library'
|
const UNIQUE_SEARCH_INDEX = 'unique_author_search_name_per_library'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all punctionation, diacritics, and whitespace and convert to lowercase for searching and matching
|
||||||
|
* (copied from Author model to ensure consistent normalization between the migration and the model)
|
||||||
|
* @param {string} name
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
async function normalizeSearchName(name) {
|
||||||
|
if (!name?.trim()) return null
|
||||||
|
return name
|
||||||
|
.normalize('NFKC') // Standardize compatibility characters
|
||||||
|
.normalize('NFD') // Split accents into combining marks
|
||||||
|
.toLocaleLowerCase('und')
|
||||||
|
.replace(/[\p{P}\p{Z}\p{M}\s]+/gu, '') // Remove punctuation, whitespace, and diacritics
|
||||||
|
.trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate derived fields. Returns null if name is empty after normalization
|
||||||
|
* (copied from Author model to ensure consistent normalization between the migration and the model)
|
||||||
|
* @param {string} name
|
||||||
|
* @returns { lastFirst: string?, searchName: string? }
|
||||||
|
*/
|
||||||
|
async function buildAuthorDerivedFields(name) {
|
||||||
|
const searchName = normalizeSearchName(name)
|
||||||
|
if (!searchName) {
|
||||||
|
return {
|
||||||
|
lastFirst: null, // populated after migration complete by server startup
|
||||||
|
searchName: null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
lastFirst: null, // populated after migration complete by server startup
|
||||||
|
searchName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function indexExists(queryInterface, tableName, indexName) {
|
async function indexExists(queryInterface, tableName, indexName) {
|
||||||
const indexes = await queryInterface.showIndex(tableName)
|
const indexes = await queryInterface.showIndex(tableName)
|
||||||
return indexes.some((index) => index.name === indexName)
|
return indexes.some((index) => index.name === indexName)
|
||||||
|
|
@ -62,7 +99,7 @@ async function backfillAuthorSearchName(queryInterface, logger, transaction, Aut
|
||||||
|
|
||||||
logger.info(`${loggerPrefix} backfilling derived author fields for ${authors.length} authors`)
|
logger.info(`${loggerPrefix} backfilling derived author fields for ${authors.length} authors`)
|
||||||
for (const author of authors) {
|
for (const author of authors) {
|
||||||
const derivedFields = Author.buildAuthorDerivedFields(author.name)
|
const derivedFields = buildAuthorDerivedFields(author.name)
|
||||||
await queryInterface.sequelize.query(
|
await queryInterface.sequelize.query(
|
||||||
`UPDATE ${AUTHORS_TABLE}
|
`UPDATE ${AUTHORS_TABLE}
|
||||||
SET lastFirst = :lastFirst,
|
SET lastFirst = :lastFirst,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue