Skip to content

Commit 0bc0995

Browse files
committed
fix(medusa): prevent plugin:db:generate crash when model files export enums
- Add test coverage for enum exports alongside entities - Add fixture plugin with enum + entity model - Closes #15077
1 parent 6f0465f commit 0bc0995

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

.changeset/cyan-mice-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/medusa": patch
3+
---
4+
5+
fix(medusa): prevent plugin:db:generate crash when model files export enums - adds test coverage

packages/medusa/src/commands/plugin/db/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function getEntitiesForModule(path: string) {
7777
(potentialEntity) => {
7878
return (
7979
DmlEntity.isDmlEntity(potentialEntity) ||
80-
!!MetadataStorage.getMetadataFromDecorator(potentialEntity as any)
80+
typeof potentialEntity === "function"
8181
)
8282
}
8383
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { MedusaService, Module } from "@medusajs/framework/utils"
2+
export default Module("module1_with_enum", {
3+
service: class Module1Service extends MedusaService({}) {},
4+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { model } from "@medusajs/framework/utils"
2+
3+
// This enum export previously caused plugin:db:generate to crash
4+
export enum MyStatus {
5+
ACTIVE = "active",
6+
INACTIVE = "inactive",
7+
}
8+
9+
const model1 = model.define("module_model_1_with_enum", {
10+
id: model.id().primaryKey(),
11+
name: model.text(),
12+
})
13+
14+
export default model1

packages/medusa/src/commands/plugin/db/integration-tests/__tests__/plugin-generate.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ describe("plugin-generate", () => {
2828
)
2929
)
3030
await module1.remove("migrations")
31+
32+
const module1WithEnum = new FileSystem(
33+
join(
34+
__dirname,
35+
"..",
36+
"__fixtures__",
37+
"plugins-1-with-enum",
38+
"src",
39+
"modules",
40+
"module-1"
41+
)
42+
)
43+
await module1WithEnum.remove("migrations")
3144
})
3245

3346
describe("main function", () => {
@@ -89,5 +102,21 @@ describe("plugin-generate", () => {
89102

90103
expect(process.exit).toHaveBeenCalledWith(1)
91104
})
105+
it("should successfully generate migrations when model files export enums alongside entities", async () => {
106+
await main({
107+
directory: join(__dirname, "..", "__fixtures__", "plugins-1-with-enum"),
108+
})
109+
expect(logger.info).toHaveBeenNthCalledWith(1, "Generating migrations...")
110+
expect(logger.info).toHaveBeenNthCalledWith(
111+
2,
112+
"Generating migrations for module module1_with_enum..."
113+
)
114+
expect(logger.info).toHaveBeenNthCalledWith(
115+
3,
116+
expect.stringContaining("Migration created")
117+
)
118+
expect(logger.info).toHaveBeenNthCalledWith(4, "Migrations generated")
119+
expect(process.exit).toHaveBeenCalledWith()
120+
})
92121
})
93122
})

0 commit comments

Comments
 (0)