2024-10-19 10:40:17 -07:00
|
|
|
/**
|
|
|
|
|
* @typedef MigrationContext
|
|
|
|
|
* @property {import('sequelize').QueryInterface} queryInterface - a suquelize QueryInterface object.
|
|
|
|
|
* @property {import('../Logger')} logger - a Logger object.
|
|
|
|
|
*
|
|
|
|
|
* @typedef MigrationOptions
|
|
|
|
|
* @property {MigrationContext} context - an object containing the migration context.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2024-10-19 11:20:29 -07:00
|
|
|
* This upward migration script adds indexes to speed up queries on the `BookAuthor`, `BookSeries`, and `podcastEpisodes` tables.
|
2024-10-19 10:40:17 -07:00
|
|
|
*
|
|
|
|
|
* @param {MigrationOptions} options - an object containing the migration context.
|
|
|
|
|
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
|
|
|
|
|
*/
|
|
|
|
|
async function up({ context: { queryInterface, logger } }) {
|
|
|
|
|
// Upwards migration script
|
|
|
|
|
logger.info('[2.15.2 migration] UPGRADE BEGIN: 2.15.2-index-creation')
|
|
|
|
|
|
|
|
|
|
// Create index for bookAuthors
|
|
|
|
|
logger.info('[2.15.2 migration] Creating index for bookAuthors')
|
2024-10-19 15:45:14 -05:00
|
|
|
const bookAuthorsIndexes = await queryInterface.showIndex('bookAuthors')
|
2026-03-03 17:42:49 -05:00
|
|
|
if (!hasIndex(bookAuthorsIndexes, 'bookAuthor_authorId')) {
|
|
|
|
|
await addIndexIfMissing(queryInterface, 'bookAuthors', ['authorId'], 'bookAuthor_authorId', logger)
|
2024-10-19 15:45:14 -05:00
|
|
|
} else {
|
|
|
|
|
logger.info('[2.15.2 migration] Index bookAuthor_authorId already exists')
|
|
|
|
|
}
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Create index for bookSeries
|
|
|
|
|
logger.info('[2.15.2 migration] Creating index for bookSeries')
|
2024-10-19 15:45:14 -05:00
|
|
|
const bookSeriesIndexes = await queryInterface.showIndex('bookSeries')
|
2026-03-03 17:42:49 -05:00
|
|
|
if (!hasIndex(bookSeriesIndexes, 'bookSeries_seriesId')) {
|
|
|
|
|
await addIndexIfMissing(queryInterface, 'bookSeries', ['seriesId'], 'bookSeries_seriesId', logger)
|
2024-10-19 15:45:14 -05:00
|
|
|
} else {
|
|
|
|
|
logger.info('[2.15.2 migration] Index bookSeries_seriesId already exists')
|
|
|
|
|
}
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Delete existing podcastEpisode index
|
|
|
|
|
logger.info('[2.15.2 migration] Deleting existing podcastEpisode index')
|
2026-03-03 17:34:36 -05:00
|
|
|
await removeIndexIfExists(queryInterface, 'podcastEpisodes', 'podcast_episodes_created_at', logger)
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Create index for podcastEpisode and createdAt
|
|
|
|
|
logger.info('[2.15.2 migration] Creating index for podcastEpisode and createdAt')
|
2024-10-19 15:45:14 -05:00
|
|
|
const podcastEpisodesIndexes = await queryInterface.showIndex('podcastEpisodes')
|
2026-03-03 17:42:49 -05:00
|
|
|
if (!hasIndex(podcastEpisodesIndexes, 'podcastEpisode_createdAt_podcastId')) {
|
|
|
|
|
await addIndexIfMissing(queryInterface, 'podcastEpisodes', ['createdAt', 'podcastId'], 'podcastEpisode_createdAt_podcastId', logger)
|
2024-10-19 15:45:14 -05:00
|
|
|
} else {
|
|
|
|
|
logger.info('[2.15.2 migration] Index podcastEpisode_createdAt_podcastId already exists')
|
|
|
|
|
}
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Completed migration
|
|
|
|
|
logger.info('[2.15.2 migration] UPGRADE END: 2.15.2-index-creation')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-10-19 11:20:29 -07:00
|
|
|
* This downward migration script removes the newly created indexes and re-adds the old index on the `podcastEpisodes` table.
|
2024-10-19 10:40:17 -07:00
|
|
|
*
|
|
|
|
|
* @param {MigrationOptions} options - an object containing the migration context.
|
|
|
|
|
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
|
|
|
|
|
*/
|
|
|
|
|
async function down({ context: { queryInterface, logger } }) {
|
|
|
|
|
// Downward migration script
|
|
|
|
|
logger.info('[2.15.2 migration] DOWNGRADE BEGIN: 2.15.2-index-creation')
|
|
|
|
|
|
|
|
|
|
// Remove index for bookAuthors
|
|
|
|
|
logger.info('[2.15.2 migration] Removing index for bookAuthors')
|
2024-10-19 11:20:29 -07:00
|
|
|
await queryInterface.removeIndex('bookAuthors', 'bookAuthor_authorId')
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Remove index for bookSeries
|
|
|
|
|
logger.info('[2.15.2 migration] Removing index for bookSeries')
|
2024-10-19 11:20:29 -07:00
|
|
|
await queryInterface.removeIndex('bookSeries', 'bookSeries_seriesId')
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Delete existing podcastEpisode index
|
|
|
|
|
logger.info('[2.15.2 migration] Deleting existing podcastEpisode index')
|
2026-03-03 17:34:36 -05:00
|
|
|
await removeIndexIfExists(queryInterface, 'podcastEpisodes', 'podcastEpisode_createdAt_podcastId', logger)
|
2024-10-19 10:40:17 -07:00
|
|
|
|
|
|
|
|
// Create index for podcastEpisode and createdAt
|
2024-10-19 11:38:34 -07:00
|
|
|
logger.info('[2.15.2 migration] Creating original index for podcastEpisode createdAt')
|
2024-10-19 11:20:29 -07:00
|
|
|
await queryInterface.addIndex('podcastEpisodes', ['createdAt'], {
|
2024-10-19 11:38:34 -07:00
|
|
|
name: 'podcast_episodes_created_at'
|
2024-10-19 10:40:17 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Finished migration
|
|
|
|
|
logger.info('[2.15.2 migration] DOWNGRADE END: 2.15.2-index-creation')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { up, down }
|
2026-03-03 17:34:36 -05:00
|
|
|
|
|
|
|
|
async function removeIndexIfExists(queryInterface, tableName, indexName, logger) {
|
|
|
|
|
const indexes = await queryInterface.showIndex(tableName)
|
2026-03-03 17:42:49 -05:00
|
|
|
const hasIndexWithName = hasIndex(indexes, indexName)
|
2026-03-03 17:34:36 -05:00
|
|
|
|
2026-03-03 17:42:49 -05:00
|
|
|
if (!hasIndexWithName) {
|
2026-03-03 17:34:36 -05:00
|
|
|
logger.info(`[2.15.2 migration] Index ${indexName} does not exist, skipping removeIndex`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await queryInterface.removeIndex(tableName, indexName)
|
|
|
|
|
}
|
2026-03-03 17:42:49 -05:00
|
|
|
|
|
|
|
|
function hasIndex(indexes, indexName) {
|
|
|
|
|
const expected = String(indexName || '').toLowerCase()
|
|
|
|
|
return indexes.some((index) => String(index?.name || index?.indexName || '').toLowerCase() === expected)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function addIndexIfMissing(queryInterface, tableName, fields, indexName, logger) {
|
|
|
|
|
try {
|
|
|
|
|
await queryInterface.addIndex(tableName, fields, { name: indexName })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const alreadyExists =
|
|
|
|
|
(error?.name === 'SequelizeDatabaseError' && /already exists/i.test(error?.message || '')) ||
|
|
|
|
|
error?.original?.code === '42P07'
|
|
|
|
|
if (!alreadyExists) throw error
|
|
|
|
|
|
|
|
|
|
logger.info(`[2.15.2 migration] Index ${indexName} already exists`)
|
|
|
|
|
}
|
|
|
|
|
}
|