fix(medusa,cli): Resources not correctly loaded when running the migration scripts (#11108)
FIXES FRMW-2888 **What** Since the migration scripts runner run as part of the migrate script, we were cleaning up the resources but we didn't take into consideration that links are loading automagically by just importing the links. The issue is that once a link have been imported, the next import will rely on the cached module and therefore when cleaning the resources, re importing those links is not enough. Instead, we are now treating the run-script as a separate command that we run during running the migration so that resources are isolated and loaded according to the need of each script themselves
This commit is contained in:
committed by
GitHub
parent
6e5912612c
commit
eace10e66b
@@ -204,6 +204,16 @@ function buildLocalCommands(cli, isLocalProject) {
|
|||||||
})
|
})
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
.command({
|
||||||
|
command: "db:migrate:scripts",
|
||||||
|
desc: "Run all migration scripts",
|
||||||
|
handler: handlerP(
|
||||||
|
getCommandHandler("db/run-scripts", (args, cmd) => {
|
||||||
|
process.env.NODE_ENV = process.env.NODE_ENV || `development`
|
||||||
|
return cmd(args)
|
||||||
|
})
|
||||||
|
),
|
||||||
|
})
|
||||||
.command({
|
.command({
|
||||||
command: "db:rollback [modules...]",
|
command: "db:rollback [modules...]",
|
||||||
desc: "Rollback last batch of executed migrations for a given module",
|
desc: "Rollback last batch of executed migrations for a given module",
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
import { join } from "path"
|
import { MEDUSA_CLI_PATH, MedusaAppLoader } from "@medusajs/framework"
|
||||||
|
import { LinkLoader } from "@medusajs/framework/links"
|
||||||
|
import { logger } from "@medusajs/framework/logger"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
mergePluginModules,
|
mergePluginModules,
|
||||||
} from "@medusajs/framework/utils"
|
} from "@medusajs/framework/utils"
|
||||||
import { LinkLoader } from "@medusajs/framework/links"
|
import { join } from "path"
|
||||||
import { logger } from "@medusajs/framework/logger"
|
|
||||||
import { MedusaAppLoader } from "@medusajs/framework"
|
|
||||||
|
|
||||||
import { syncLinks } from "./sync-links"
|
import { fork } from "child_process"
|
||||||
import { ensureDbExists } from "../utils"
|
import path from "path"
|
||||||
import { initializeContainer } from "../../loaders"
|
import { initializeContainer } from "../../loaders"
|
||||||
import { getResolvedPlugins } from "../../loaders/helpers/resolve-plugins"
|
import { getResolvedPlugins } from "../../loaders/helpers/resolve-plugins"
|
||||||
import { runMigrationScripts } from "./run-scripts"
|
import { ensureDbExists } from "../utils"
|
||||||
|
import { syncLinks } from "./sync-links"
|
||||||
const TERMINAL_SIZE = process.stdout.columns
|
const TERMINAL_SIZE = process.stdout.columns
|
||||||
|
|
||||||
|
const cliPath = path.resolve(MEDUSA_CLI_PATH, "..", "..", "cli.js")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A low-level utility to migrate the database. This util should
|
* A low-level utility to migrate the database. This util should
|
||||||
* never exit the process implicitly.
|
* never exit the process implicitly.
|
||||||
@@ -77,9 +79,18 @@ export async function migrate({
|
|||||||
* Run migration scripts
|
* Run migration scripts
|
||||||
*/
|
*/
|
||||||
console.log(new Array(TERMINAL_SIZE).join("-"))
|
console.log(new Array(TERMINAL_SIZE).join("-"))
|
||||||
await runMigrationScripts({
|
const childProcess = fork(cliPath, ["db:migrate:scripts"], {
|
||||||
directory,
|
cwd: directory,
|
||||||
container,
|
env: process.env,
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
childProcess.on("error", (error) => {
|
||||||
|
reject(error)
|
||||||
|
})
|
||||||
|
childProcess.on("close", () => {
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
import { dirname, join } from "path"
|
import { dirname, join } from "path"
|
||||||
|
|
||||||
import { MedusaModule } from "@medusajs/framework/modules-sdk"
|
import { MedusaModule } from "@medusajs/framework/modules-sdk"
|
||||||
import { MedusaContainer } from "@medusajs/types"
|
import { MedusaContainer, PluginDetails } from "@medusajs/types"
|
||||||
import { initializeContainer } from "../../loaders"
|
import { initializeContainer } from "../../loaders"
|
||||||
import { getResolvedPlugins } from "../../loaders/helpers/resolve-plugins"
|
import { getResolvedPlugins } from "../../loaders/helpers/resolve-plugins"
|
||||||
import { ensureDbExists } from "../utils"
|
import { ensureDbExists } from "../utils"
|
||||||
@@ -22,46 +22,30 @@ const TERMINAL_SIZE = process.stdout.columns
|
|||||||
*/
|
*/
|
||||||
export async function runMigrationScripts({
|
export async function runMigrationScripts({
|
||||||
directory,
|
directory,
|
||||||
container,
|
|
||||||
}: {
|
}: {
|
||||||
directory: string
|
directory: string
|
||||||
container?: MedusaContainer
|
|
||||||
}): Promise<boolean> {
|
}): Promise<boolean> {
|
||||||
/**
|
|
||||||
* Clear all module instances to prevent cache from kicking in
|
|
||||||
*/
|
|
||||||
MedusaModule.clearInstances()
|
|
||||||
|
|
||||||
let container_: MedusaContainer = container!
|
|
||||||
let onApplicationPrepareShutdown: () => Promise<void> = async () =>
|
let onApplicationPrepareShutdown: () => Promise<void> = async () =>
|
||||||
Promise.resolve()
|
Promise.resolve()
|
||||||
let onApplicationShutdown: () => Promise<void> = async () => Promise.resolve()
|
let onApplicationShutdown: () => Promise<void> = async () => Promise.resolve()
|
||||||
|
let container_: MedusaContainer
|
||||||
|
let plugins: PluginDetails[]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/**
|
container_ = await initializeContainer(directory)
|
||||||
* Setup
|
|
||||||
*/
|
|
||||||
container_ ??= await initializeContainer(directory)
|
|
||||||
await ensureDbExists(container_)
|
await ensureDbExists(container_)
|
||||||
|
|
||||||
const configModule = container_.resolve(
|
const configModule = container_.resolve(
|
||||||
ContainerRegistrationKeys.CONFIG_MODULE
|
ContainerRegistrationKeys.CONFIG_MODULE
|
||||||
)
|
)
|
||||||
|
|
||||||
const plugins = await getResolvedPlugins(directory, configModule, true)
|
plugins = await getResolvedPlugins(directory, configModule, true)
|
||||||
|
|
||||||
mergePluginModules(configModule, plugins)
|
mergePluginModules(configModule, plugins)
|
||||||
|
|
||||||
const linksSourcePaths = plugins.map((plugin) =>
|
const resources = await loadResources(plugins)
|
||||||
join(plugin.resolve, "links")
|
onApplicationPrepareShutdown = resources.onApplicationPrepareShutdown
|
||||||
)
|
onApplicationShutdown = resources.onApplicationShutdown
|
||||||
await new LinkLoader(linksSourcePaths).load()
|
|
||||||
|
|
||||||
const medusaAppResources = await new MedusaAppLoader().load()
|
|
||||||
onApplicationPrepareShutdown =
|
|
||||||
medusaAppResources.onApplicationPrepareShutdown
|
|
||||||
onApplicationShutdown = medusaAppResources.onApplicationShutdown
|
|
||||||
await medusaAppResources.onApplicationStart()
|
|
||||||
|
|
||||||
const scriptsSourcePaths = [
|
const scriptsSourcePaths = [
|
||||||
join(dirname(require.resolve("@medusajs/medusa")), "migration-scripts"),
|
join(dirname(require.resolve("@medusajs/medusa")), "migration-scripts"),
|
||||||
@@ -97,7 +81,42 @@ export async function runMigrationScripts({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = async function ({ directory }: { directory: string }) {
|
async function loadResources(plugins: any): Promise<{
|
||||||
|
onApplicationPrepareShutdown: () => Promise<void>
|
||||||
|
onApplicationShutdown: () => Promise<void>
|
||||||
|
}> {
|
||||||
|
/**
|
||||||
|
* Clear all module instances to prevent cache from kicking in
|
||||||
|
*/
|
||||||
|
MedusaModule.clearInstances()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup
|
||||||
|
*/
|
||||||
|
|
||||||
|
const linksSourcePaths = plugins.map((plugin) =>
|
||||||
|
join(plugin.resolve, "links")
|
||||||
|
)
|
||||||
|
await new LinkLoader(linksSourcePaths).load()
|
||||||
|
|
||||||
|
const medusaAppResources = await new MedusaAppLoader().load()
|
||||||
|
const onApplicationPrepareShutdown =
|
||||||
|
medusaAppResources.onApplicationPrepareShutdown
|
||||||
|
const onApplicationShutdown = medusaAppResources.onApplicationShutdown
|
||||||
|
await medusaAppResources.onApplicationStart()
|
||||||
|
|
||||||
|
return {
|
||||||
|
onApplicationPrepareShutdown,
|
||||||
|
onApplicationShutdown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const main = async function ({
|
||||||
|
directory,
|
||||||
|
}: {
|
||||||
|
directory: string
|
||||||
|
container?: MedusaContainer
|
||||||
|
}) {
|
||||||
try {
|
try {
|
||||||
const migrated = await runMigrationScripts({
|
const migrated = await runMigrationScripts({
|
||||||
directory,
|
directory,
|
||||||
|
|||||||
Reference in New Issue
Block a user