fix: gracefully handle migrations that cannot be reverted (#8087)

This commit is contained in:
Harminder Virk
2024-07-11 17:40:34 +05:30
committed by GitHub
parent 45c573b03a
commit e489b0c037
2 changed files with 27 additions and 0 deletions
@@ -15,6 +15,7 @@ export type MigrationsEvents = {
migrated: [UmzugMigration]
reverting: [UmzugMigration]
reverted: [UmzugMigration]
"revert:skipped": [UmzugMigration & { reason: string }]
}
/**
@@ -105,6 +106,29 @@ export class Migrations extends EventEmitter<MigrationsEvents> {
try {
return await migrator.down(options)
} catch (error) {
/**
* This is a very ugly hack to recover from an exception thrown by
* MikrORM when the `down` method is not implemented by the
* migration.
*
* We cannot check if "down" method exists on the migration, because it
* always exists (as inherited from the parent class). Also, throwing
* an exception is important, so that Mikro ORM does not consider the
* given migration as reverted.
*/
if (
error?.migration &&
error?.cause?.message === "This migration cannot be reverted"
) {
this.emit("revert:skipped", {
...error.migration,
reason: "Missing down method",
})
return []
}
throw error
} finally {
migrator["umzug"].clearListeners()
await connection.close(true)
@@ -42,6 +42,9 @@ export function buildRevertMigrationScript({ moduleName, pathToMigrations }) {
migrations.on("reverted", (migration) => {
logger.info(` ✔ Reverted ${migration.name}`)
})
migrations.on("revert:skipped", (migration) => {
logger.info(` ✔ Skipped ${migration.name}. ${migration.reason}`)
})
try {
const result = await migrations.revert()