diff --git a/server/migrations/changelog.md b/server/migrations/changelog.md index 0fcbe675..4f826725 100644 --- a/server/migrations/changelog.md +++ b/server/migrations/changelog.md @@ -16,3 +16,4 @@ Please add a record of every database migration that you create to this file. Th | v2.19.1 | v2.19.1-copy-title-to-library-items | Copies title and titleIgnorePrefix to the libraryItems table, creates update triggers and indices | | v2.19.4 | v2.19.4-improve-podcast-queries | Adds numEpisodes to podcasts, adds podcastId to mediaProgresses, copies podcast title to libraryItems | | v2.20.0 | v2.20.0-improve-author-sort-queries | Adds AuthorNames(FirstLast\|LastFirst) to libraryItems to improve author sort queries | +| v2.31.1 | v2.31.1-update-metadata-json-with-id | Adds ids to the locally stored metadata.json to help file moves keep track of the items | diff --git a/server/migrations/v2.31.1-update-metadata-json-with-id.js b/server/migrations/v2.31.1-update-metadata-json-with-id.js new file mode 100644 index 00000000..4e3ae780 --- /dev/null +++ b/server/migrations/v2.31.1-update-metadata-json-with-id.js @@ -0,0 +1,48 @@ +/** + * @typedef MigrationContext + * @property {import('sequelize').QueryInterface} queryInterface - a sequelize QueryInterface object. + * @property {import('../Logger')} logger - a Logger object. + * + * @typedef MigrationOptions + * @property {MigrationContext} context - an object containing the migration context. + */ + +const migrationVersion = '2.31.1' +const migrationName = `${migrationVersion}-update-metadata-json-with-id` +const loggerPrefix = `[${migrationVersion} migration]` + +/** + * This upward migration creates a sessions table and apiKeys table. + * + * @param {MigrationOptions} options - an object containing the migration context. + * @returns {Promise} - A promise that resolves when the migration is complete. + */ +async function up({ context: { logger } }) { + // Upwards migration script + logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`) + // Re-save all metadata json files, the id field will be added + const libraryItems = await Database.libraryItemModel.findAll() + for (const libraryItem of libraryItems) { + await libraryItem.saveMetadataFile() + } + logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`) +} + +/** + * This downward migration script removes the sessions table and apiKeys 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: { logger } }) { + // Downward migration script + logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`) + // Re-save all metadata json files, the id field will be removed + const libraryItems = await Database.libraryItemModel.findAll() + for (const libraryItem of libraryItems) { + await libraryItem.saveMetadataFile() + } + logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`) +} + +module.exports = { up, down }