Commit b1c4aee
fix(run-scripts): load WorkflowLoader during db:migrate so workflow hooks are registered (#15260)
## What
Workflow hooks defined in `src/workflows/` (and plugin workflow directories) are silently skipped during `medusa db:migrate` execution.
Closes #14125
## Root Cause
`loadResources()` in `packages/medusa/src/commands/db/run-scripts.ts` calls `LinkLoader` and `MedusaAppLoader` but never calls `WorkflowLoader`. This means any hook registered on a built-in workflow (e.g. `createProductsWorkflow.hooks.productsCreated`) does not fire when that workflow is invoked from a migration script — even though the workflow itself runs successfully and no error is thrown.
Compare to `packages/medusa/src/loaders/index.ts` (lines 205–207), where `WorkflowLoader` is already called with all plugin paths. That call was simply missing from `run-scripts.ts`.
## Fix
Add a `WorkflowLoader` call in `loadResources()` after `onApplicationStart()`, mirroring the exact pattern already used in `loaders/index.ts`:
```ts
const workflowsSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "workflows")
)
await new WorkflowLoader(workflowsSourcePaths, container).load()
```
`getResolvedPlugins()` already includes the application root directory as a plugin entry (via the third `true` argument), so no additional path handling is needed.
## Changes
| File | Change |
|---|---|
| `packages/medusa/src/commands/db/run-scripts.ts` | Import `WorkflowLoader`; call it after `onApplicationStart()` in `loadResources()` |
| `packages/medusa/src/commands/__tests__/run-scripts.spec.ts` | Add `WorkflowLoader` mock; add 4 new tests covering path derivation, `load()` invocation, call ordering, and zero-plugin edge case |
## Testing
Four new unit tests added in the existing `run-scripts.spec.ts` test suite:
- **path derivation** — verifies `WorkflowLoader` is constructed with `<plugin.resolve>/workflows` paths and the caller's container
- **load() called** — verifies `WorkflowLoader.load()` is invoked exactly once
- **call ordering** — verifies `WorkflowLoader` runs *after* `onApplicationStart()` so all module registrations are available
- **zero-plugin edge case** — verifies behaviour when `getResolvedPlugins` returns an empty array
## Impact / Risk
- ✅ **Purely additive** — no existing behaviour is changed
- ✅ **Consistent** with `loaders/index.ts` which already uses this exact pattern
- ✅ **No breaking changes** — `WorkflowLoader` is a no-op when the source paths contain no workflow files
Co-authored-by: Akib <2947572+akib@users.noreply.github.com>1 parent 3d23fa7 commit b1c4aee
3 files changed
Lines changed: 118 additions & 0 deletions
File tree
- .changeset
- packages/medusa/src/commands
- __tests__
- db
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 102 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
100 | 105 | | |
101 | 106 | | |
102 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
103 | 113 | | |
104 | 114 | | |
105 | 115 | | |
| |||
228 | 238 | | |
229 | 239 | | |
230 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
231 | 333 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
118 | 129 | | |
119 | 130 | | |
120 | 131 | | |
| |||
0 commit comments