mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Add title triggers in new databases
This commit is contained in:
		
							parent
							
								
									6290cfaeb1
								
							
						
					
					
						commit
						f9c0e52f18
					
				| @ -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 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user