Fix multiple bugs

This commit is contained in:
Tiberiu Ichim 2026-02-06 22:35:03 +02:00
parent ef3f39191d
commit f511e0046d
5 changed files with 198 additions and 188 deletions

View file

@ -139,9 +139,12 @@ Following the initial implementation, several critical areas were improved:
- **Issue**: Metadata `save()` calls for newly created series/authors were not awaited. - **Issue**: Metadata `save()` calls for newly created series/authors were not awaited.
- **Fix**: Ensured all `.save()` calls are properly awaited. - **Fix**: Ensured all `.save()` calls are properly awaited.
### 4. Transactional Integrity - FIXED ### 4. Transactional Integrity & Lock Optimization - FIXED
- **Issue**: The move logic was not wrapped in a DB transaction. - **Issue**: The move logic was not wrapped in a DB transaction, and long-running file moves inside transactions would lock the SQLite database for several seconds (causing `SQLITE_BUSY`).
- **Fix**: Wrapped the DB update portion of `handleMoveLibraryItem` in a Sequelize transaction that is committed only if all updates succeed. - **Fix**:
- Wrapped the DB update portion of `handleMoveLibraryItem` in a Sequelize transaction.
- **Optimization**: Moved the `fs.move` operation **outside** the transaction. The files are moved first, then the transaction handles the lightning-fast DB updates. If the DB update fails, the files are moved back to their original location.
- **Transaction Propagation**: Updated `Series`, `Author`, and `BookAuthor` model helpers to correctly accept and propagate the transaction object.
### 5. Scanner "ENOTDIR" Error - FIXED ### 5. Scanner "ENOTDIR" Error - FIXED
- **Issue**: Single-file items (e.g., `.m4b`) were being scanned as directories, leading to `ENOTDIR` errors and causing items to appear with the "has issues" icon. - **Issue**: Single-file items (e.g., `.m4b`) were being scanned as directories, leading to `ENOTDIR` errors and causing items to appear with the "has issues" icon.

View file

@ -46,9 +46,8 @@ const ShareManager = require('../managers/ShareManager')
* @param {import('../models/LibraryItem')} libraryItem * @param {import('../models/LibraryItem')} libraryItem
* @param {import('../models/Library')} targetLibrary * @param {import('../models/Library')} targetLibrary
* @param {import('../models/LibraryFolder')} targetFolder * @param {import('../models/LibraryFolder')} targetFolder
* @param {import('sequelize').Transaction} [transaction]
*/ */
async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, transaction = null) { async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder) {
const oldPath = libraryItem.path const oldPath = libraryItem.path
const oldLibraryId = libraryItem.libraryId const oldLibraryId = libraryItem.libraryId
@ -73,6 +72,9 @@ async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, t
Logger.info(`[LibraryItemController] Moving item "${libraryItem.media.title}" from "${oldPath}" to "${newPath}"`) Logger.info(`[LibraryItemController] Moving item "${libraryItem.media.title}" from "${oldPath}" to "${newPath}"`)
await fs.move(oldPath, newPath) await fs.move(oldPath, newPath)
// Update database within a transaction
const transaction = await Database.sequelize.transaction()
try {
// Update library item in database // Update library item in database
libraryItem.libraryId = targetLibrary.id libraryItem.libraryId = targetLibrary.id
libraryItem.libraryFolderId = targetFolder.id libraryItem.libraryFolderId = targetFolder.id
@ -240,6 +242,12 @@ async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, t
} }
} }
await transaction.commit()
} catch (dbError) {
if (transaction) await transaction.rollback()
throw dbError
}
// Emit socket events for UI updates // Emit socket events for UI updates
SocketAuthority.emitter('item_removed', { SocketAuthority.emitter('item_removed', {
id: libraryItem.id, id: libraryItem.id,
@ -1107,20 +1115,12 @@ class LibraryItemController {
continue continue
} }
const transaction = await Database.sequelize.transaction() await handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder)
try {
await handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, transaction)
await transaction.commit()
successCount++ successCount++
} catch (error) { } catch (error) {
if (transaction) await transaction.rollback()
failCount++ failCount++
errors.push({ id: libraryItem.id, error: error.message || 'Failed to move item' }) errors.push({ id: libraryItem.id, error: error.message || 'Failed to move item' })
} }
} catch (error) {
failCount++
errors.push({ id: libraryItem.id, error: error.message || 'Unknown error' })
}
} }
// Reset filter data and clear caches once after batch move // Reset filter data and clear caches once after batch move
@ -1565,14 +1565,7 @@ class LibraryItemController {
} }
try { try {
const transaction = await Database.sequelize.transaction() await handleMoveLibraryItem(req.libraryItem, targetLibrary, targetFolder)
try {
await handleMoveLibraryItem(req.libraryItem, targetLibrary, targetFolder, transaction)
await transaction.commit()
} catch (error) {
await transaction.rollback()
throw error
}
await Database.resetLibraryIssuesFilterData(sourceLibrary.id) await Database.resetLibraryIssuesFilterData(sourceLibrary.id)
await Database.resetLibraryIssuesFilterData(targetLibrary.id) await Database.resetLibraryIssuesFilterData(targetLibrary.id)

View file

@ -50,16 +50,18 @@ class Author extends Model {
* *
* @param {string} authorName * @param {string} authorName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Author>} * @returns {Promise<Author>}
*/ */
static async getByNameAndLibrary(authorName, libraryId) { static async getByNameAndLibrary(authorName, libraryId, transaction = null) {
return this.findOne({ return this.findOne({
where: [ where: [
where(fn('lower', col('name')), authorName.toLowerCase()), where(fn('lower', col('name')), authorName.toLowerCase()),
{ {
libraryId libraryId
} }
] ],
transaction
}) })
} }
@ -111,16 +113,20 @@ class Author extends Model {
* *
* @param {string} name * @param {string} name
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Author>} * @returns {Promise<Author>}
*/ */
static async findOrCreateByNameAndLibrary(name, libraryId) { static async findOrCreateByNameAndLibrary(name, libraryId, transaction = null) {
const author = await this.getByNameAndLibrary(name, libraryId) const author = await this.getByNameAndLibrary(name, libraryId, transaction)
if (author) return author if (author) return author
return this.create({ return this.create(
{
name, name,
lastFirst: this.getLastFirst(name), lastFirst: this.getLastFirst(name),
libraryId libraryId
}) },
{ transaction }
)
} }
/** /**

View file

@ -27,13 +27,15 @@ class BookAuthor extends Model {
* Get number of books for author * Get number of books for author
* *
* @param {string} authorId * @param {string} authorId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<number>} * @returns {Promise<number>}
*/ */
static getCountForAuthor(authorId) { static getCountForAuthor(authorId, transaction = null) {
return this.count({ return this.count({
where: { where: {
authorId authorId
} },
transaction
}) })
} }

View file

@ -41,16 +41,18 @@ class Series extends Model {
* *
* @param {string} seriesName * @param {string} seriesName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Series>} * @returns {Promise<Series>}
*/ */
static async getByNameAndLibrary(seriesName, libraryId) { static async getByNameAndLibrary(seriesName, libraryId, transaction = null) {
return this.findOne({ return this.findOne({
where: [ where: [
where(fn('lower', col('name')), seriesName.toLowerCase()), where(fn('lower', col('name')), seriesName.toLowerCase()),
{ {
libraryId libraryId
} }
] ],
transaction
}) })
} }
@ -70,16 +72,20 @@ class Series extends Model {
* *
* @param {string} seriesName * @param {string} seriesName
* @param {string} libraryId * @param {string} libraryId
* @param {import('sequelize').Transaction} [transaction]
* @returns {Promise<Series>} * @returns {Promise<Series>}
*/ */
static async findOrCreateByNameAndLibrary(seriesName, libraryId) { static async findOrCreateByNameAndLibrary(seriesName, libraryId, transaction = null) {
const series = await this.getByNameAndLibrary(seriesName, libraryId) const series = await this.getByNameAndLibrary(seriesName, libraryId, transaction)
if (series) return series if (series) return series
return this.create({ return this.create(
{
name: seriesName, name: seriesName,
nameIgnorePrefix: getTitleIgnorePrefix(seriesName), nameIgnorePrefix: getTitleIgnorePrefix(seriesName),
libraryId libraryId
}) },
{ transaction }
)
} }
/** /**