Fix test and update triggers for new db

This commit is contained in:
mikiher 2025-03-17 14:55:14 +02:00
parent 4b3c0d65dc
commit d3de1e57ec
2 changed files with 11 additions and 16 deletions

View file

@ -815,10 +815,10 @@ class Database {
{ name: 'authorNamesFirstLast', source: `${authors}.name`, spec: { type: Sequelize.STRING, allowNull: true } }, { name: 'authorNamesFirstLast', source: `${authors}.name`, spec: { type: Sequelize.STRING, allowNull: true } },
{ name: 'authorNamesLastFirst', source: `${authors}.lastFirst`, 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 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 addBookAuthorsTriggerIfNotExists = async (action) => {
const modifiedRecord = action === 'delete' ? 'OLD' : 'NEW' const modifiedRecord = action === 'delete' ? 'OLD' : 'NEW'
@ -827,7 +827,6 @@ class Database {
SELECT ${columnSourcesExpression} SELECT ${columnSourcesExpression}
FROM ${authorsJoin} FROM ${authorsJoin}
WHERE ${bookAuthors}.bookId = ${modifiedRecord}.bookId 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}'`) 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 if (count > 0) return // Trigger already exists
@ -852,7 +851,6 @@ class Database {
SELECT ${columnSourcesExpression} SELECT ${columnSourcesExpression}
FROM ${authorsJoin} FROM ${authorsJoin}
WHERE ${bookAuthors}.bookId = ${libraryItems}.mediaId 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}'`) const [[{ count }]] = await this.sequelize.query(`SELECT COUNT(*) as count FROM sqlite_master WHERE type='trigger' AND name='${triggerName}'`)

View file

@ -58,7 +58,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => {
await queryInterface.bulkInsert('bookAuthors', [ await queryInterface.bulkInsert('bookAuthors', [
{ id: 1, bookId: 1, authorId: 1, createdAt: '2025-01-01 00:00:00.000 +00:00' }, { 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: 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', [ 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') const [libraryItems] = await queryInterface.sequelize.query('SELECT * FROM libraryItems')
expect(libraryItems).to.deep.equal([ 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' } { 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 BEGIN
UPDATE libraryItems UPDATE libraryItems
SET (authorNamesFirstLast, authorNamesLastFirst) = ( 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 FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId
WHERE bookAuthors.bookId = NEW.bookId WHERE bookAuthors.bookId = NEW.bookId
ORDER BY bookAuthors.createdAt ASC
) )
WHERE mediaId = NEW.bookId; WHERE mediaId = NEW.bookId;
END END
@ -128,10 +127,9 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => {
BEGIN BEGIN
UPDATE libraryItems UPDATE libraryItems
SET (authorNamesFirstLast, authorNamesLastFirst) = ( 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 FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId
WHERE bookAuthors.bookId = OLD.bookId WHERE bookAuthors.bookId = OLD.bookId
ORDER BY bookAuthors.createdAt ASC
) )
WHERE mediaId = OLD.bookId; WHERE mediaId = OLD.bookId;
END END
@ -150,10 +148,9 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => {
BEGIN BEGIN
UPDATE libraryItems UPDATE libraryItems
SET (authorNamesFirstLast, authorNamesLastFirst) = ( 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 FROM authors JOIN bookAuthors ON authors.id = bookAuthors.authorId
WHERE bookAuthors.bookId = libraryItems.mediaId WHERE bookAuthors.bookId = libraryItems.mediaId
ORDER BY bookAuthors.createdAt ASC
) )
WHERE mediaId IN (SELECT bookId FROM bookAuthors WHERE authorId = NEW.id); WHERE mediaId IN (SELECT bookId FROM bookAuthors WHERE authorId = NEW.id);
END END
@ -194,7 +191,7 @@ describe('Migration v2.20.0-improve-author-sort-queries', () => {
// check that the libraryItems table was updated // check that the libraryItems table was updated
const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`) const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
expect(libraryItems).to.deep.equal([ 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' } { 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 // check that the libraryItems table was updated
const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`) const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
expect(libraryItems).to.deep.equal([ 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' } { 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`) const [libraryItems] = await queryInterface.sequelize.query(`SELECT * FROM libraryItems`)
expect(libraryItems).to.deep.equal([ 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' } { id: 2, mediaId: 2, mediaType: 'book', libraryId: 1, authorNamesFirstLast: 'Jane Smith', authorNamesLastFirst: 'Smith, Jane' }
]) ])
}) })