fix: gracefully handle migrations that cannot be reverted (#8087)
This commit is contained in:
1 parent
45c573b03a
commit
e489b0c037
2 files changed
+27
No files matched your search
@@ -15,6 +15,7 @@ export type MigrationsEvents = {
|
|||||||
migrated: [UmzugMigration]
|
migrated: [UmzugMigration]
|
||||||
reverting: [UmzugMigration]
|
reverting: [UmzugMigration]
|
||||||
reverted: [UmzugMigration]
|
reverted: [UmzugMigration]
|
||||||
|
"revert:skipped": [UmzugMigration & { reason: string }]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,6 +106,29 @@ export class Migrations extends EventEmitter<MigrationsEvents> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
return await migrator.down(options)
|
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 {
|
} finally {
|
||||||
migrator["umzug"].clearListeners()
|
migrator["umzug"].clearListeners()
|
||||||
await connection.close(true)
|
await connection.close(true)
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ export function buildRevertMigrationScript({ moduleName, pathToMigrations }) {
|
|||||||
migrations.on("reverted", (migration) => {
|
migrations.on("reverted", (migration) => {
|
||||||
logger.info(` ✔ Reverted ${migration.name}`)
|
logger.info(` ✔ Reverted ${migration.name}`)
|
||||||
})
|
})
|
||||||
|
migrations.on("revert:skipped", (migration) => {
|
||||||
|
logger.info(` ✔ Skipped ${migration.name}. ${migration.reason}`)
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await migrations.revert()
|
const result = await migrations.revert()
|
||||||
|
|||||||
Reference in new issue
Block a user