From f9c0e52f18ea4c696f41c2900e06fd2f476236cf Mon Sep 17 00:00:00 2001 From: mikiher Date: Wed, 19 Feb 2025 17:39:32 +0200 Subject: [PATCH] Add title triggers in new databases --- server/Database.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/server/Database.js b/server/Database.js index 0bdc3e90..498e9e5e 100644 --- a/server/Database.js +++ b/server/Database.js @@ -190,6 +190,8 @@ class Database { await this.buildModels(force) Logger.info(`[Database] Db initialized with models:`, Object.keys(this.sequelize.models).join(', ')) + await this.addTriggers() + await this.loadData() Logger.info(`[Database] running ANALYZE`) @@ -771,6 +773,43 @@ class Database { return textQuery } + /** + * This is used to create necessary triggers for new databases. + * It adds triggers to update libraryItems.title[IgnorePrefix] when (books|podcasts).title[IgnorePrefix] is updated + */ + async addTriggers() { + await this.addTriggerIfNotExists('books', 'title', 'id', 'libraryItems', 'title', 'mediaId') + await this.addTriggerIfNotExists('books', 'titleIgnorePrefix', 'id', 'libraryItems', 'titleIgnorePrefix', 'mediaId') + await this.addTriggerIfNotExists('podcasts', 'title', 'id', 'libraryItems', 'title', 'mediaId') + await this.addTriggerIfNotExists('podcasts', 'titleIgnorePrefix', 'id', 'libraryItems', 'titleIgnorePrefix', 'mediaId') + } + + async addTriggerIfNotExists(sourceTable, sourceColumn, sourceIdColumn, targetTable, targetColumn, targetIdColumn) { + const action = `update_${targetTable}_${targetColumn}` + const fromSource = sourceTable === 'books' ? '' : `_from_${sourceTable}_${sourceColumn}` + const triggerName = this.convertToSnakeCase(`${action}${fromSource}`) + + const [[{ count }]] = await this.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='trigger' AND name='${triggerName}'`) + if (count > 0) return // Trigger already exists + + Logger.info(`[Database] Adding trigger ${triggerName}`) + + await this.sequelize.query(` + CREATE TRIGGER ${triggerName} + AFTER UPDATE OF ${sourceColumn} ON ${sourceTable} + FOR EACH ROW + BEGIN + UPDATE ${targetTable} + SET ${targetColumn} = NEW.${sourceColumn} + WHERE ${targetTable}.${targetIdColumn} = NEW.${sourceIdColumn}; + END; + `) + } + + convertToSnakeCase(str) { + return str.replace(/([A-Z])/g, '_$1').toLowerCase() + } + TextSearchQuery = class { constructor(sequelize, supportsUnaccent, query) { this.sequelize = sequelize