From d3de1e57ec8f070a2c3843566916c7ffad3c2906 Mon Sep 17 00:00:00 2001 From: mikiher Date: Mon, 17 Mar 2025 14:55:14 +0200 Subject: [PATCH] Fix test and update triggers for new db --- server/Database.js | 8 +++----- ...2.20.0-improve-author-sort-queries.test.js | 19 ++++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/server/Database.js b/server/Database.js index c898fb61a..52827e3fb 100644 --- a/server/Database.js +++ b/server/Database.js @@ -815,10 +815,10 @@ class Database { { name: 'authorNamesFirstLast', source: `${authors}.name`, spec: { type: Sequelize.STRING, allowNull: true } }, { name: 'authorNamesLastFirst', source: `${authors}.lastFirst`, spec: { type: Sequelize.STRING, allowNull: true } } ] - const columnNames = columns.map((column) => column.name).join(', ') - const columnSourcesExpression = columns.map((column) => `GROUP_CONCAT(${column.source}, ', ')`).join(', ') - const authorsJoin = `${authors} JOIN ${bookAuthors} ON ${authors}.id = ${bookAuthors}.authorId` const authorsSort = `${bookAuthors}.createdAt ASC` + const columnNames = columns.map((column) => column.name).join(', ') + const columnSourcesExpression = columns.map((column) => `GROUP_CONCAT(${column.source}, ', ' ORDER BY ${authorsSort})`).join(', ') + const authorsJoin = `${authors} JOIN ${bookAuthors} ON ${authors}.id = ${bookAuthors}.authorId` const addBookAuthorsTriggerIfNotExists = async (action) => { const modifiedRecord = action === 'delete' ? 'OLD' : 'NEW' @@ -827,7 +827,6 @@ class Database { SELECT ${columnSourcesExpression} FROM ${authorsJoin} WHERE ${bookAuthors}.bookId = ${modifiedRecord}.bookId - ORDER BY ${authorsSort} ` 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 @@ -852,7 +851,6 @@ class Database { SELECT ${columnSourcesExpression} FROM ${authorsJoin} WHERE ${bookAuthors}.bookId = ${libraryItems}.mediaId - ORDER BY ${authorsSort} ` const [[{ count }]] = await this.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='trigger' AND name='${triggerName}'`) diff --git a/test/server/migrations/v2.20.0-improve-author-sort-queries.test.js b/test/server/migrations/v2.20.0-improve-author-sort-queries.test.js index 0079eacee..76cf704ae 100644 --- a/test/server/migrations/v2.20.0-improve-author-sort-queries.test.js +++ b/test/server/migrations/v2.20.0-improve-author-sort-queries.test.js @@ -58,7 +58,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { await queryInterface.bulkInsert('bookAuthors', [ { id: 1, bookId: 1, authorId: 1, createdAt: '2025-01-01 00:00:00.000 +00:00' }, { id: 2, bookId: 2, authorId: 2, createdAt: '2025-01-02 00:00:00.000 +00:00' }, - { id: 3, bookId: 1, authorId: 3, createdAt: '2025-01-03 00:00:00.000 +00:00' } + { id: 3, bookId: 1, authorId: 3, createdAt: '2024-12-31 00:00:00.000 +00:00' } ]) await queryInterface.bulkInsert('podcastEpisodes', [ @@ -86,7 +86,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { const [libraryItems] = await queryInterface.sequelize.query('SELECT * FROM libraryItems') expect(libraryItems).to.deep.equal([ - { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Doe, John Smith', authorNamesLastFirst: 'Doe, John, Smith, John' }, + { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Smith, John Doe', authorNamesLastFirst: 'Smith, John, Doe, John' }, { id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' } ]) }) @@ -106,10 +106,9 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { BEGIN UPDATE libraryItems SET (authorNamesFirstLast, authorNamesLastFirst) = ( - SELECT GROUP_CONCAT(authors.name, ', '), GROUP_CONCAT(authors.lastFirst, ', ') + SELECT GROUP_CONCAT(authors.name, ', ' ORDER BY bookAuthors.createdAt ASC), GROUP_CONCAT(authors.lastFirst, ', ' ORDER BY bookAuthors.createdAt ASC) FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId WHERE bookAuthors.bookId = NEW.bookId - ORDER BY bookAuthors.createdAt ASC ) WHERE mediaId = NEW.bookId; END @@ -128,10 +127,9 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { BEGIN UPDATE libraryItems SET (authorNamesFirstLast, authorNamesLastFirst) = ( - SELECT GROUP_CONCAT(authors.name, ', '), GROUP_CONCAT(authors.lastFirst, ', ') + SELECT GROUP_CONCAT(authors.name, ', ' ORDER BY bookAuthors.createdAt ASC), GROUP_CONCAT(authors.lastFirst, ', ' ORDER BY bookAuthors.createdAt ASC) FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId WHERE bookAuthors.bookId = OLD.bookId - ORDER BY bookAuthors.createdAt ASC ) WHERE mediaId = OLD.bookId; END @@ -150,10 +148,9 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { BEGIN UPDATE libraryItems SET (authorNamesFirstLast, authorNamesLastFirst) = ( - SELECT GROUP_CONCAT(authors.name, ', '), GROUP_CONCAT(authors.lastFirst, ', ') + SELECT GROUP_CONCAT(authors.name, ', ' ORDER BY bookAuthors.createdAt ASC), GROUP_CONCAT(authors.lastFirst, ', ' ORDER BY bookAuthors.createdAt ASC) FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId WHERE bookAuthors.bookId = libraryItems.mediaId - ORDER BY bookAuthors.createdAt ASC ) WHERE mediaId IN (SELECT bookId FROM bookAuthors WHERE authorId = NEW.id); END @@ -194,7 +191,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { // check that the libraryItems table was updated const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`) expect(libraryItems).to.deep.equal([ - { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Wayne, John Smith', authorNamesLastFirst: 'Wayne, John, Smith, John' }, + { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Smith, John Wayne', authorNamesLastFirst: 'Smith, John, Wayne, John' }, { id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' } ]) }) @@ -211,7 +208,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { // check that the libraryItems table was updated const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`) expect(libraryItems).to.deep.equal([ - { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Doe, John Smith, John Wayne', authorNamesLastFirst: 'Doe, John, Smith, John, Wayne, John' }, + { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Smith, John Doe, John Wayne', authorNamesLastFirst: 'Smith, John, Doe, John, Wayne, John' }, { id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' } ]) }) @@ -272,7 +269,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => { const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`) expect(libraryItems).to.deep.equal([ - { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Doe, John Smith', authorNamesLastFirst: 'Doe, John, Smith, John' }, + { id: 1, mediaId: 1, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'John Smith, John Doe', authorNamesLastFirst: 'Smith, John, Doe, John' }, { id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' } ]) })