diff --git a/server/setup/migrationsPg.ts b/server/setup/migrationsPg.ts index 0b8af06bc..01ff36beb 100644 --- a/server/setup/migrationsPg.ts +++ b/server/setup/migrationsPg.ts @@ -128,7 +128,7 @@ async function executeScripts() { console.log(`Starting migrations from version ${startVersion}`); const migrationsToRun = migrations.filter((migration) => - semver.gt(migration.version, startVersion) + shouldRunMigration(migration.version, startVersion) ); console.log( @@ -179,3 +179,23 @@ async function executeScripts() { throw error; } } + +function shouldRunMigration(migrationVersion: string, currentVersion: string) { + const migration = semver.parse(migrationVersion); + const current = semver.parse(currentVersion); + + // Treat x.y.z-rc.* as equivalent to x.y.z so restarts on RC builds do not re-run the same migration. + if ( + migration && + current && + migration.prerelease.length === 0 && + current.prerelease[0] === "rc" && + migration.major === current.major && + migration.minor === current.minor && + migration.patch === current.patch + ) { + return false; + } + + return semver.gt(migrationVersion, currentVersion); +} diff --git a/server/setup/migrationsSqlite.ts b/server/setup/migrationsSqlite.ts index 837b039f7..83b5b9bc6 100644 --- a/server/setup/migrationsSqlite.ts +++ b/server/setup/migrationsSqlite.ts @@ -169,7 +169,7 @@ async function executeScripts() { console.log(`Starting migrations from version ${startVersion}`); const migrationsToRun = migrations.filter((migration) => - semver.gt(migration.version, startVersion) + shouldRunMigration(migration.version, startVersion) ); console.log( @@ -223,3 +223,23 @@ async function executeScripts() { throw error; } } + +function shouldRunMigration(migrationVersion: string, currentVersion: string) { + const migration = semver.parse(migrationVersion); + const current = semver.parse(currentVersion); + + // Treat x.y.z-rc.* as equivalent to x.y.z so restarts on RC builds do not re-run the same migration. + if ( + migration && + current && + migration.prerelease.length === 0 && + current.prerelease[0] === "rc" && + migration.major === current.major && + migration.minor === current.minor && + migration.patch === current.patch + ) { + return false; + } + + return semver.gt(migrationVersion, currentVersion); +}