Ensure series-column-unique migration is idempotent

This commit is contained in:
advplyr 2024-09-24 16:47:09 -05:00
parent 66b290577c
commit 8a7b5cc87d
2 changed files with 15 additions and 8 deletions

View File

@ -38,6 +38,7 @@ class MigrationManager {
if (!(await fs.pathExists(this.configPath))) throw new Error(`Config path does not exist: ${this.configPath}`) if (!(await fs.pathExists(this.configPath))) throw new Error(`Config path does not exist: ${this.configPath}`)
this.migrationsDir = path.join(this.configPath, 'migrations') this.migrationsDir = path.join(this.configPath, 'migrations')
await fs.ensureDir(this.migrationsDir)
this.serverVersion = this.extractVersionFromTag(serverVersion) this.serverVersion = this.extractVersionFromTag(serverVersion)
if (!this.serverVersion) throw new Error(`Invalid server version: ${serverVersion}. Expected a version tag like v1.2.3.`) if (!this.serverVersion) throw new Error(`Invalid server version: ${serverVersion}. Expected a version tag like v1.2.3.`)
@ -222,8 +223,6 @@ class MigrationManager {
} }
async copyMigrationsToConfigDir() { async copyMigrationsToConfigDir() {
await fs.ensureDir(this.migrationsDir) // Ensure the target directory exists
if (!(await fs.pathExists(this.migrationsSourceDir))) return if (!(await fs.pathExists(this.migrationsSourceDir))) return
const files = await fs.readdir(this.migrationsSourceDir) const files = await fs.readdir(this.migrationsSourceDir)

View File

@ -16,7 +16,15 @@
*/ */
async function up({ context: { queryInterface, logger } }) { async function up({ context: { queryInterface, logger } }) {
// Upwards migration script // Upwards migration script
logger.info('UPGRADE BEGIN: 2.13.5-series-column-unique ') logger.info('[2.13.5 migration] UPGRADE BEGIN: 2.13.5-series-column-unique ')
// Check if the unique index already exists
const seriesIndexes = await queryInterface.showIndex('Series')
if (seriesIndexes.some((index) => index.name === 'unique_series_name_per_library')) {
logger.info('[2.13.5 migration] Unique index on Series.name and Series.libraryId already exists')
logger.info('[2.13.5 migration] UPGRADE END: 2.13.5-series-column-unique ')
return
}
// The steps taken to deduplicate the series are as follows: // The steps taken to deduplicate the series are as follows:
// 1. Find all duplicate series in the `Series` table. // 1. Find all duplicate series in the `Series` table.
@ -173,9 +181,9 @@ async function up({ context: { queryInterface, logger } }) {
unique: true, unique: true,
name: 'unique_series_name_per_library' name: 'unique_series_name_per_library'
}) })
logger.info('Added unique index on Series.name and Series.libraryId') logger.info('[2.13.5 migration] Added unique index on Series.name and Series.libraryId')
logger.info('UPGRADE END: 2.13.5-series-column-unique ') logger.info('[2.13.5 migration] UPGRADE END: 2.13.5-series-column-unique ')
} }
/** /**
@ -186,13 +194,13 @@ async function up({ context: { queryInterface, logger } }) {
*/ */
async function down({ context: { queryInterface, logger } }) { async function down({ context: { queryInterface, logger } }) {
// Downward migration script // Downward migration script
logger.info('DOWNGRADE BEGIN: 2.13.5-series-column-unique ') logger.info('[2.13.5 migration] DOWNGRADE BEGIN: 2.13.5-series-column-unique ')
// Remove the unique index // Remove the unique index
await queryInterface.removeIndex('Series', 'unique_series_name_per_library') await queryInterface.removeIndex('Series', 'unique_series_name_per_library')
logger.info('Removed unique index on Series.name and Series.libraryId') logger.info('[2.13.5 migration] Removed unique index on Series.name and Series.libraryId')
logger.info('DOWNGRADE END: 2.13.5-series-column-unique ') logger.info('[2.13.5 migration] DOWNGRADE END: 2.13.5-series-column-unique ')
} }
module.exports = { up, down } module.exports = { up, down }