diff --git a/server/migrations/v2.15.2-index-creation.js b/server/migrations/v2.15.2-index-creation.js new file mode 100644 index 00000000..8f5b9d52 --- /dev/null +++ b/server/migrations/v2.15.2-index-creation.js @@ -0,0 +1,78 @@ +/** + * @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. + */ + +/** + * This upward migration script adds indexes to speed up queries on the `BookAuthor`, `BookSeries`, and `PodcastEpisode` tables. + * + * @param {MigrationOptions} options - an object containing the migration context. + * @returns {Promise} - 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') + await queryInterface.addIndex('BookAuthor', ['authorId'], { + name: 'bookAuthor_authorId' + }) + + // Create index for bookSeries + logger.info('[2.15.2 migration] Creating index for bookSeries') + await queryInterface.addIndex('BookSeries', ['seriesId'], { + name: 'bookSeries_seriesId' + }) + + // Delete existing podcastEpisode index + logger.info('[2.15.2 migration] Deleting existing podcastEpisode index') + await queryInterface.removeIndex('PodcastEpisode', 'podcast_episode_created_at') + + // Create index for podcastEpisode and createdAt + logger.info('[2.15.2 migration] Creating index for podcastEpisode and createdAt') + await queryInterface.addIndex('PodcastEpisode', ['createdAt', 'podcastId'], { + name: 'podcastEpisode_createdAt_podcastId' + }) + + // Completed migration + logger.info('[2.15.2 migration] UPGRADE END: 2.15.2-index-creation') +} + +/** + * This downward migration script removes the newly created indexes and re-adds the old index on the `PodcastEpisode` table. + * + * @param {MigrationOptions} options - an object containing the migration context. + * @returns {Promise} - 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') + await queryInterface.removeIndex('BookAuthor', 'bookAuthor_authorId') + + // Remove index for bookSeries + logger.info('[2.15.2 migration] Removing index for bookSeries') + await queryInterface.removeIndex('BookSeries', 'bookSeries_seriesId') + + // Delete existing podcastEpisode index + logger.info('[2.15.2 migration] Deleting existing podcastEpisode index') + await queryInterface.removeIndex('PodcastEpisode', 'podcastEpisode_createdAt_podcastId') + + // Create index for podcastEpisode and createdAt + logger.info('[2.15.2 migration] Creating index for podcastEpisode createdAt') + await queryInterface.addIndex('PodcastEpisode', ['createdAt'], { + name: 'podcast_episode_created_at' + }) + + // Finished migration + logger.info('[2.15.2 migration] DOWNGRADE END: 2.15.2-index-creation') +} + +module.exports = { up, down } diff --git a/server/models/BookAuthor.js b/server/models/BookAuthor.js index 8e093671..d7d65728 100644 --- a/server/models/BookAuthor.js +++ b/server/models/BookAuthor.js @@ -54,7 +54,13 @@ class BookAuthor extends Model { sequelize, modelName: 'bookAuthor', timestamps: true, - updatedAt: false + updatedAt: false, + indexes: [ + { + name: 'bookAuthor_authorId', + fields: ['authorId'] + } + ] } ) diff --git a/server/models/BookSeries.js b/server/models/BookSeries.js index fad54718..31eccb9f 100644 --- a/server/models/BookSeries.js +++ b/server/models/BookSeries.js @@ -43,7 +43,13 @@ class BookSeries extends Model { sequelize, modelName: 'bookSeries', timestamps: true, - updatedAt: false + updatedAt: false, + indexes: [ + { + name: 'bookSeries_seriesId', + fields: ['seriesId'] + } + ] } ) diff --git a/server/models/PodcastEpisode.js b/server/models/PodcastEpisode.js index 1707fbd5..937e0b31 100644 --- a/server/models/PodcastEpisode.js +++ b/server/models/PodcastEpisode.js @@ -157,7 +157,7 @@ class PodcastEpisode extends Model { modelName: 'podcastEpisode', indexes: [ { - fields: ['createdAt'] + fields: ['createdAt', 'podcastId'] } ] }