feat(migrations): CLI generate command (#8103)
This commit is contained in:
committed by
GitHub
parent
4c2e9a3239
commit
104b00d4e9
@@ -2,6 +2,7 @@ import {
|
||||
Constructor,
|
||||
IModuleService,
|
||||
InternalModuleDeclaration,
|
||||
LoaderOptions,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
ModuleExports,
|
||||
@@ -32,6 +33,11 @@ type ModuleResource = {
|
||||
normalizedPath: string
|
||||
}
|
||||
|
||||
type MigrationFunction = (
|
||||
options: LoaderOptions<any>,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
) => Promise<void>
|
||||
|
||||
export async function loadInternalModule(
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
@@ -171,7 +177,11 @@ export async function loadInternalModule(
|
||||
export async function loadModuleMigrations(
|
||||
resolution: ModuleResolution,
|
||||
moduleExports?: ModuleExports
|
||||
): Promise<[Function | undefined, Function | undefined]> {
|
||||
): Promise<{
|
||||
runMigrations?: MigrationFunction
|
||||
revertMigration?: MigrationFunction
|
||||
generateMigration?: MigrationFunction
|
||||
}> {
|
||||
let loadedModule: ModuleExports
|
||||
try {
|
||||
loadedModule =
|
||||
@@ -179,6 +189,7 @@ export async function loadModuleMigrations(
|
||||
|
||||
let runMigrations = loadedModule.runMigrations
|
||||
let revertMigration = loadedModule.revertMigration
|
||||
let generateMigration = loadedModule.generateMigration
|
||||
|
||||
if (!runMigrations || !revertMigration) {
|
||||
const moduleResources = await loadResources(
|
||||
@@ -189,6 +200,7 @@ export async function loadModuleMigrations(
|
||||
|
||||
const migrationScriptOptions = {
|
||||
moduleName: resolution.definition.key,
|
||||
models: moduleResources.models,
|
||||
pathToMigrations: join(moduleResources.normalizedPath, "migrations"),
|
||||
}
|
||||
|
||||
@@ -199,11 +211,15 @@ export async function loadModuleMigrations(
|
||||
revertMigration ??= ModulesSdkUtils.buildRevertMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
|
||||
generateMigration ??= ModulesSdkUtils.buildGenerateMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
}
|
||||
|
||||
return [runMigrations, revertMigration]
|
||||
return { runMigrations, revertMigration, generateMigration }
|
||||
} catch {
|
||||
return [undefined, undefined]
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user