Feat: Improvements to the migrations CLI and workflow (#8060)
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { upperCaseFirst } from "../../common"
|
||||
import { mikroOrmCreateConnection } from "../../dal"
|
||||
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||
import { Migrations } from "../../migrations"
|
||||
|
||||
const TERMINAL_SIZE = process.stdout.columns
|
||||
|
||||
/**
|
||||
* Utility function to build a migration script that will revert the migrations.
|
||||
* Only used in mikro orm based modules.
|
||||
* @param moduleName
|
||||
* @param models
|
||||
* @param pathToMigrations
|
||||
*/
|
||||
export function buildRevertMigrationScript({
|
||||
moduleName,
|
||||
models,
|
||||
pathToMigrations,
|
||||
}) {
|
||||
export function buildRevertMigrationScript({ moduleName, pathToMigrations }) {
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of reverting the migrations.
|
||||
@@ -34,34 +28,30 @@ export function buildRevertMigrationScript({
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
console.log("")
|
||||
logger.info(`MODULE: ${moduleName}`)
|
||||
|
||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
||||
const entities = Object.values(models).map((model) => {
|
||||
if (DmlEntity.isDmlEntity(model)) {
|
||||
return toMikroORMEntity(model)
|
||||
}
|
||||
const orm = await mikroOrmCreateConnection(dbData, [], pathToMigrations)
|
||||
const migrations = new Migrations(orm)
|
||||
|
||||
return model
|
||||
}) as unknown as EntitySchema[]
|
||||
|
||||
const orm = await mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
migrations.on("reverting", (migration) => {
|
||||
logger.info(` ● Reverting ${migration.name}`)
|
||||
})
|
||||
migrations.on("reverted", (migration) => {
|
||||
logger.info(` ✔ Reverted ${migration.name}`)
|
||||
})
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
await migrator.down()
|
||||
|
||||
logger?.info(`${upperCaseFirst(moduleName)} module migration executed`)
|
||||
const result = await migrations.revert()
|
||||
if (result.length) {
|
||||
logger.info("Reverted successfully")
|
||||
} else {
|
||||
logger.info("Skipped. Nothing to revert")
|
||||
}
|
||||
} catch (error) {
|
||||
logger?.error(
|
||||
`${upperCaseFirst(
|
||||
moduleName
|
||||
)} module migration failed to run - Error: ${error.errros ?? error}`
|
||||
)
|
||||
logger.error(`Failed with error ${error.message}`, error)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { upperCaseFirst } from "../../common"
|
||||
import { mikroOrmCreateConnection } from "../../dal"
|
||||
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||
import { Migrations } from "../../migrations"
|
||||
|
||||
const TERMINAL_SIZE = process.stdout.columns
|
||||
|
||||
/**
|
||||
* Utility function to build a migration script that will run the migrations.
|
||||
* Only used in mikro orm based modules.
|
||||
* @param moduleName
|
||||
* @param models
|
||||
* @param pathToMigrations
|
||||
*/
|
||||
export function buildMigrationScript({ moduleName, models, pathToMigrations }) {
|
||||
export function buildMigrationScript({ moduleName, pathToMigrations }) {
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of running the migrations.
|
||||
@@ -29,48 +28,30 @@ export function buildMigrationScript({ moduleName, models, pathToMigrations }) {
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||
console.log("")
|
||||
logger.info(`MODULE: ${moduleName}`)
|
||||
|
||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
||||
const entities = Object.values(models).map((model) => {
|
||||
if (DmlEntity.isDmlEntity(model)) {
|
||||
return toMikroORMEntity(model)
|
||||
}
|
||||
const orm = await mikroOrmCreateConnection(dbData, [], pathToMigrations)
|
||||
const migrations = new Migrations(orm)
|
||||
|
||||
return model
|
||||
}) as unknown as EntitySchema[]
|
||||
|
||||
const orm = await mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
migrations.on("migrating", (migration) => {
|
||||
logger.info(` ● Migrating ${migration.name}`)
|
||||
})
|
||||
migrations.on("migrated", (migration) => {
|
||||
logger.info(` ✔ Migrated ${migration.name}`)
|
||||
})
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
const pendingMigrations = await migrator.getPendingMigrations()
|
||||
|
||||
if (pendingMigrations.length) {
|
||||
logger.info(
|
||||
`Pending migrations: ${JSON.stringify(pendingMigrations, null, 2)}`
|
||||
)
|
||||
|
||||
await migrator.up({
|
||||
migrations: pendingMigrations.map((m) => m.name),
|
||||
})
|
||||
|
||||
logger.info(
|
||||
`${upperCaseFirst(moduleName)} module: ${
|
||||
pendingMigrations.length
|
||||
} migration files executed`
|
||||
)
|
||||
const result = await migrations.run()
|
||||
if (result.length) {
|
||||
logger.info("Completed successfully")
|
||||
} else {
|
||||
logger.info("Skipped. Database is upto-date")
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`${upperCaseFirst(
|
||||
moduleName
|
||||
)} module migration failed to run - Error: ${error.errros ?? error}`
|
||||
)
|
||||
logger.error(`Failed with error ${error.message}`, error)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user