From 713bdcbc419b6b5db9af68effd3b0984be93aa41 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Sat, 9 Nov 2024 13:10:46 -0700 Subject: [PATCH 1/3] Add: migration for mediaId to use UUID instead of UUIDV4 --- server/migrations/v2.16.3-uuid-replacement.js | 50 +++++++++++++++++++ server/models/LibraryItem.js | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 server/migrations/v2.16.3-uuid-replacement.js diff --git a/server/migrations/v2.16.3-uuid-replacement.js b/server/migrations/v2.16.3-uuid-replacement.js new file mode 100644 index 0000000000..66bf21ac98 --- /dev/null +++ b/server/migrations/v2.16.3-uuid-replacement.js @@ -0,0 +1,50 @@ +/** + * @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 changes the `mediaId` column in the `libraryItems` table to be a UUID and match other 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.16.3 migration] UPGRADE BEGIN: 2.16.3-uuid-replacement') + + // Change mediaId column to using the query interface + logger.info('[2.16.3 migration] Changing mediaId column to UUID') + await queryInterface.changeColumn('libraryItems', 'mediaId', { + type: 'UUID' + }) + + // Completed migration + logger.info('[2.16.3 migration] UPGRADE END: 2.16.3-uuid-replacement') +} + +/** + * This downward migration script changes the `mediaId` column in the `libraryItems` table to be a UUIDV4 again. + * + * @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.16.3 migration] DOWNGRADE BEGIN: 2.16.3-uuid-replacement') + + // Change mediaId column to using the query interface + logger.info('[2.16.3 migration] Changing mediaId column to UUIDV4') + await queryInterface.changeColumn('libraryItems', 'mediaId', { + type: 'UUIDV4' + }) + + // Completed migration + logger.info('[2.16.3 migration] DOWNGRADE END: 2.16.3-uuid-replacement') +} + +module.exports = { up, down } diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index 17c3b12586..c2a0178584 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -1059,7 +1059,7 @@ class LibraryItem extends Model { ino: DataTypes.STRING, path: DataTypes.STRING, relPath: DataTypes.STRING, - mediaId: DataTypes.UUIDV4, + mediaId: DataTypes.UUID, mediaType: DataTypes.STRING, isFile: DataTypes.BOOLEAN, isMissing: DataTypes.BOOLEAN, From 161a3f4da925821d1f2ff41e0d9954291588a308 Mon Sep 17 00:00:00 2001 From: Nicholas Wallace Date: Sat, 9 Nov 2024 13:20:59 -0700 Subject: [PATCH 2/3] Update migrations changelog for 2.16.3 --- server/migrations/changelog.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/migrations/changelog.md b/server/migrations/changelog.md index 3623300f4c..bffd4682d6 100644 --- a/server/migrations/changelog.md +++ b/server/migrations/changelog.md @@ -2,8 +2,9 @@ Please add a record of every database migration that you create to this file. This will help us keep track of changes to the database schema over time. -| Server Version | Migration Script Name | Description | -| -------------- | ---------------------------- | ------------------------------------------------------------------------------------ | -| v2.15.0 | v2.15.0-series-column-unique | Series must have unique names in the same library | -| v2.15.1 | v2.15.1-reindex-nocase | Fix potential db corruption issues due to bad sqlite extension introduced in v2.12.0 | -| v2.15.2 | v2.15.2-index-creation | Creates author, series, and podcast episode indexes | +| Server Version | Migration Script Name | Description | +| -------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------- | +| v2.15.0 | v2.15.0-series-column-unique | Series must have unique names in the same library | +| v2.15.1 | v2.15.1-reindex-nocase | Fix potential db corruption issues due to bad sqlite extension introduced in v2.12.0 | +| v2.15.2 | v2.15.2-index-creation | Creates author, series, and podcast episode indexes | +| v2.16.3 | v2.16.3-uuid-replacement | Changes `mediaId` column in `libraryItem` table to match the primary key type of `books` and `podcasts` | From 2b7e3f0efe6fae0d6138cb95ac72224f81b31bfc Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 17 Nov 2024 15:45:21 -0600 Subject: [PATCH 3/3] Update uuid migration to v2.17.0 and for all tables still using UUIDv4 --- server/migrations/changelog.md | 12 +-- server/migrations/v2.16.3-uuid-replacement.js | 50 ---------- server/migrations/v2.17.0-uuid-replacement.js | 98 +++++++++++++++++++ server/models/Feed.js | 2 +- server/models/MediaItemShare.js | 2 +- server/models/MediaProgress.js | 2 +- server/models/PlaybackSession.js | 2 +- server/models/PlaylistMediaItem.js | 2 +- 8 files changed, 109 insertions(+), 61 deletions(-) delete mode 100644 server/migrations/v2.16.3-uuid-replacement.js create mode 100644 server/migrations/v2.17.0-uuid-replacement.js diff --git a/server/migrations/changelog.md b/server/migrations/changelog.md index bffd4682d6..8960ade2f7 100644 --- a/server/migrations/changelog.md +++ b/server/migrations/changelog.md @@ -2,9 +2,9 @@ Please add a record of every database migration that you create to this file. This will help us keep track of changes to the database schema over time. -| Server Version | Migration Script Name | Description | -| -------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------- | -| v2.15.0 | v2.15.0-series-column-unique | Series must have unique names in the same library | -| v2.15.1 | v2.15.1-reindex-nocase | Fix potential db corruption issues due to bad sqlite extension introduced in v2.12.0 | -| v2.15.2 | v2.15.2-index-creation | Creates author, series, and podcast episode indexes | -| v2.16.3 | v2.16.3-uuid-replacement | Changes `mediaId` column in `libraryItem` table to match the primary key type of `books` and `podcasts` | +| Server Version | Migration Script Name | Description | +| -------------- | ---------------------------- | ------------------------------------------------------------------------------------ | +| v2.15.0 | v2.15.0-series-column-unique | Series must have unique names in the same library | +| v2.15.1 | v2.15.1-reindex-nocase | Fix potential db corruption issues due to bad sqlite extension introduced in v2.12.0 | +| v2.15.2 | v2.15.2-index-creation | Creates author, series, and podcast episode indexes | +| v2.17.0 | v2.17.0-uuid-replacement | Changes the data type of columns with UUIDv4 to UUID matching the associated model | diff --git a/server/migrations/v2.16.3-uuid-replacement.js b/server/migrations/v2.16.3-uuid-replacement.js deleted file mode 100644 index 66bf21ac98..0000000000 --- a/server/migrations/v2.16.3-uuid-replacement.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @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 changes the `mediaId` column in the `libraryItems` table to be a UUID and match other 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.16.3 migration] UPGRADE BEGIN: 2.16.3-uuid-replacement') - - // Change mediaId column to using the query interface - logger.info('[2.16.3 migration] Changing mediaId column to UUID') - await queryInterface.changeColumn('libraryItems', 'mediaId', { - type: 'UUID' - }) - - // Completed migration - logger.info('[2.16.3 migration] UPGRADE END: 2.16.3-uuid-replacement') -} - -/** - * This downward migration script changes the `mediaId` column in the `libraryItems` table to be a UUIDV4 again. - * - * @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.16.3 migration] DOWNGRADE BEGIN: 2.16.3-uuid-replacement') - - // Change mediaId column to using the query interface - logger.info('[2.16.3 migration] Changing mediaId column to UUIDV4') - await queryInterface.changeColumn('libraryItems', 'mediaId', { - type: 'UUIDV4' - }) - - // Completed migration - logger.info('[2.16.3 migration] DOWNGRADE END: 2.16.3-uuid-replacement') -} - -module.exports = { up, down } diff --git a/server/migrations/v2.17.0-uuid-replacement.js b/server/migrations/v2.17.0-uuid-replacement.js new file mode 100644 index 0000000000..6460b79526 --- /dev/null +++ b/server/migrations/v2.17.0-uuid-replacement.js @@ -0,0 +1,98 @@ +/** + * @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 changes table columns with data type UUIDv4 to UUID to match associated models. + * + * @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.17.0 migration] UPGRADE BEGIN: 2.17.0-uuid-replacement') + + logger.info('[2.17.0 migration] Changing libraryItems.mediaId column to UUID') + await queryInterface.changeColumn('libraryItems', 'mediaId', { + type: 'UUID' + }) + + logger.info('[2.17.0 migration] Changing feeds.entityId column to UUID') + await queryInterface.changeColumn('feeds', 'entityId', { + type: 'UUID' + }) + + logger.info('[2.17.0 migration] Changing mediaItemShares.mediaItemId column to UUID') + await queryInterface.changeColumn('mediaItemShares', 'mediaItemId', { + type: 'UUID' + }) + + logger.info('[2.17.0 migration] Changing playbackSessions.mediaItemId column to UUID') + await queryInterface.changeColumn('playbackSessions', 'mediaItemId', { + type: 'UUID' + }) + + logger.info('[2.17.0 migration] Changing playlistMediaItems.mediaItemId column to UUID') + await queryInterface.changeColumn('playlistMediaItems', 'mediaItemId', { + type: 'UUID' + }) + + logger.info('[2.17.0 migration] Changing mediaProgresses.mediaItemId column to UUID') + await queryInterface.changeColumn('mediaProgresses', 'mediaItemId', { + type: 'UUID' + }) + + // Completed migration + logger.info('[2.17.0 migration] UPGRADE END: 2.17.0-uuid-replacement') +} + +/** + * This downward migration script changes table columns data type back to UUIDv4. + * + * @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.17.0 migration] DOWNGRADE BEGIN: 2.17.0-uuid-replacement') + + logger.info('[2.17.0 migration] Changing libraryItems.mediaId column to UUIDV4') + await queryInterface.changeColumn('libraryItems', 'mediaId', { + type: 'UUIDV4' + }) + + logger.info('[2.17.0 migration] Changing feeds.entityId column to UUIDV4') + await queryInterface.changeColumn('feeds', 'entityId', { + type: 'UUIDV4' + }) + + logger.info('[2.17.0 migration] Changing mediaItemShares.mediaItemId column to UUIDV4') + await queryInterface.changeColumn('mediaItemShares', 'mediaItemId', { + type: 'UUIDV4' + }) + + logger.info('[2.17.0 migration] Changing playbackSessions.mediaItemId column to UUIDV4') + await queryInterface.changeColumn('playbackSessions', 'mediaItemId', { + type: 'UUIDV4' + }) + + logger.info('[2.17.0 migration] Changing playlistMediaItems.mediaItemId column to UUIDV4') + await queryInterface.changeColumn('playlistMediaItems', 'mediaItemId', { + type: 'UUIDV4' + }) + + logger.info('[2.17.0 migration] Changing mediaProgresses.mediaItemId column to UUIDV4') + await queryInterface.changeColumn('mediaProgresses', 'mediaItemId', { + type: 'UUIDV4' + }) + + // Completed migration + logger.info('[2.17.0 migration] DOWNGRADE END: 2.17.0-uuid-replacement') +} + +module.exports = { up, down } diff --git a/server/models/Feed.js b/server/models/Feed.js index 72321da926..4f51e66d9e 100644 --- a/server/models/Feed.js +++ b/server/models/Feed.js @@ -274,7 +274,7 @@ class Feed extends Model { }, slug: DataTypes.STRING, entityType: DataTypes.STRING, - entityId: DataTypes.UUIDV4, + entityId: DataTypes.UUID, entityUpdatedAt: DataTypes.DATE, serverAddress: DataTypes.STRING, feedURL: DataTypes.STRING, diff --git a/server/models/MediaItemShare.js b/server/models/MediaItemShare.js index ffdc3ddd1f..38b8dbbf4b 100644 --- a/server/models/MediaItemShare.js +++ b/server/models/MediaItemShare.js @@ -109,7 +109,7 @@ class MediaItemShare extends Model { defaultValue: DataTypes.UUIDV4, primaryKey: true }, - mediaItemId: DataTypes.UUIDV4, + mediaItemId: DataTypes.UUID, mediaItemType: DataTypes.STRING, slug: DataTypes.STRING, pash: DataTypes.STRING, diff --git a/server/models/MediaProgress.js b/server/models/MediaProgress.js index d6a527f742..80204ef5cc 100644 --- a/server/models/MediaProgress.js +++ b/server/models/MediaProgress.js @@ -93,7 +93,7 @@ class MediaProgress extends Model { defaultValue: DataTypes.UUIDV4, primaryKey: true }, - mediaItemId: DataTypes.UUIDV4, + mediaItemId: DataTypes.UUID, mediaItemType: DataTypes.STRING, duration: DataTypes.FLOAT, currentTime: DataTypes.FLOAT, diff --git a/server/models/PlaybackSession.js b/server/models/PlaybackSession.js index c7c6323af6..196fbda6ce 100644 --- a/server/models/PlaybackSession.js +++ b/server/models/PlaybackSession.js @@ -179,7 +179,7 @@ class PlaybackSession extends Model { defaultValue: DataTypes.UUIDV4, primaryKey: true }, - mediaItemId: DataTypes.UUIDV4, + mediaItemId: DataTypes.UUID, mediaItemType: DataTypes.STRING, displayTitle: DataTypes.STRING, displayAuthor: DataTypes.STRING, diff --git a/server/models/PlaylistMediaItem.js b/server/models/PlaylistMediaItem.js index 25e7b8c553..1c53bea115 100644 --- a/server/models/PlaylistMediaItem.js +++ b/server/models/PlaylistMediaItem.js @@ -45,7 +45,7 @@ class PlaylistMediaItem extends Model { defaultValue: DataTypes.UUIDV4, primaryKey: true }, - mediaItemId: DataTypes.UUIDV4, + mediaItemId: DataTypes.UUID, mediaItemType: DataTypes.STRING, order: DataTypes.INTEGER },