From 35e2681ea9d1193b259e5a73b3643dc876b8a48c Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 19 Oct 2024 15:45:14 -0500 Subject: [PATCH] Update index creation migration to be idempotent --- server/migrations/v2.15.2-index-creation.js | 33 +++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/server/migrations/v2.15.2-index-creation.js b/server/migrations/v2.15.2-index-creation.js index 4e9915c2..f1302dd2 100644 --- a/server/migrations/v2.15.2-index-creation.js +++ b/server/migrations/v2.15.2-index-creation.js @@ -19,15 +19,25 @@ async function up({ context: { queryInterface, logger } }) { // Create index for bookAuthors logger.info('[2.15.2 migration] Creating index for bookAuthors') - await queryInterface.addIndex('bookAuthors', ['authorId'], { - name: 'bookAuthor_authorId' - }) + const bookAuthorsIndexes = await queryInterface.showIndex('bookAuthors') + if (!bookAuthorsIndexes.some((index) => index.name === 'bookAuthor_authorId')) { + await queryInterface.addIndex('bookAuthors', ['authorId'], { + name: 'bookAuthor_authorId' + }) + } else { + logger.info('[2.15.2 migration] Index bookAuthor_authorId already exists') + } // Create index for bookSeries logger.info('[2.15.2 migration] Creating index for bookSeries') - await queryInterface.addIndex('bookSeries', ['seriesId'], { - name: 'bookSeries_seriesId' - }) + const bookSeriesIndexes = await queryInterface.showIndex('bookSeries') + if (!bookSeriesIndexes.some((index) => index.name === 'bookSeries_seriesId')) { + await queryInterface.addIndex('bookSeries', ['seriesId'], { + name: 'bookSeries_seriesId' + }) + } else { + logger.info('[2.15.2 migration] Index bookSeries_seriesId already exists') + } // Delete existing podcastEpisode index logger.info('[2.15.2 migration] Deleting existing podcastEpisode index') @@ -35,9 +45,14 @@ async function up({ context: { queryInterface, logger } }) { // Create index for podcastEpisode and createdAt logger.info('[2.15.2 migration] Creating index for podcastEpisode and createdAt') - await queryInterface.addIndex('podcastEpisodes', ['createdAt', 'podcastId'], { - name: 'podcastEpisode_createdAt_podcastId' - }) + const podcastEpisodesIndexes = await queryInterface.showIndex('podcastEpisodes') + if (!podcastEpisodesIndexes.some((index) => index.name === 'podcastEpisode_createdAt_podcastId')) { + await queryInterface.addIndex('podcastEpisodes', ['createdAt', 'podcastId'], { + name: 'podcastEpisode_createdAt_podcastId' + }) + } else { + logger.info('[2.15.2 migration] Index podcastEpisode_createdAt_podcastId already exists') + } // Completed migration logger.info('[2.15.2 migration] UPGRADE END: 2.15.2-index-creation')