Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-mice-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): prevent plugin:db:generate crash when model files export enums - adds test coverage
2 changes: 1 addition & 1 deletion packages/medusa/src/commands/plugin/db/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function getEntitiesForModule(path: string) {
(potentialEntity) => {
return (
DmlEntity.isDmlEntity(potentialEntity) ||
!!MetadataStorage.getMetadataFromDecorator(potentialEntity as any)
typeof potentialEntity === "function"
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { MedusaService, Module } from "@medusajs/framework/utils"
export default Module("module1_with_enum", {
service: class Module1Service extends MedusaService({}) {},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { model } from "@medusajs/framework/utils"

// This enum export previously caused plugin:db:generate to crash
export enum MyStatus {
ACTIVE = "active",
INACTIVE = "inactive",
}

const model1 = model.define("module_model_1_with_enum", {
id: model.id().primaryKey(),
name: model.text(),
})

export default model1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ describe("plugin-generate", () => {
)
)
await module1.remove("migrations")

const module1WithEnum = new FileSystem(
join(
__dirname,
"..",
"__fixtures__",
"plugins-1-with-enum",
"src",
"modules",
"module-1"
)
)
await module1WithEnum.remove("migrations")
})

describe("main function", () => {
Expand Down Expand Up @@ -89,5 +102,21 @@ describe("plugin-generate", () => {

expect(process.exit).toHaveBeenCalledWith(1)
})
it("should successfully generate migrations when model files export enums alongside entities", async () => {
await main({
directory: join(__dirname, "..", "__fixtures__", "plugins-1-with-enum"),
})
expect(logger.info).toHaveBeenNthCalledWith(1, "Generating migrations...")
expect(logger.info).toHaveBeenNthCalledWith(
2,
"Generating migrations for module module1_with_enum..."
)
expect(logger.info).toHaveBeenNthCalledWith(
3,
expect.stringContaining("Migration created")
)
expect(logger.info).toHaveBeenNthCalledWith(4, "Migrations generated")
expect(process.exit).toHaveBeenCalledWith()
})
})
})