Skip to content

fix(medusa): prevent plugin:db:generate crash when model files export…#14718

Open
shubhamchoudhary-2003 wants to merge 4 commits intomedusajs:developfrom
Eryx-Innovations-Lab:fix/plugin-db-generate-enum-crash
Open

fix(medusa): prevent plugin:db:generate crash when model files export…#14718
shubhamchoudhary-2003 wants to merge 4 commits intomedusajs:developfrom
Eryx-Innovations-Lab:fix/plugin-db-generate-enum-crash

Conversation

@shubhamchoudhary-2003
Copy link
Copy Markdown

@shubhamchoudhary-2003 shubhamchoudhary-2003 commented Feb 6, 2026

fix(medusa): prevent plugin:db:generate crash when model files export enums

MetadataStorage.getMetadataFromDecorator() returns a truthy EntityMetadata for any input with a .name property, causing TypeScript enums to be incorrectly treated as database entities. This crashes MikroORM when it tries to process them during migration generation.

Replace the MetadataStorage check with typeof === "function", which correctly identifies MikroORM class entities while rejecting enum objects. This matches the proven pattern already used in load-internal.ts cleanupResources for the regular db:generate path.

Summary

What — Fix entity detection in plugin:db:generate to prevent TypeScript enums from being treated as MikroORM entities.

Why — When model files export enums, MikroORM crashes during migration generation because enums are mistakenly identified as database entities. MetadataStorage.getMetadataFromDecorator() returns truthy for anything with a .name property, which includes compiled TypeScript enums.

How — Replaced the MetadataStorage.getMetadataFromDecorator() check with typeof potentialEntity === "function" in the entity filter inside getEntitiesForModule() (packages/medusa/src/commands/plugin/db/generate.ts). Class constructors are functions; enum objects are not — so this correctly filters out enums while keeping real entities. This aligns with the same pattern already used in load-internal.ts cleanupResources.

Testing — Create a plugin with a model file that exports both a MikroORM entity and a TypeScript enum, then run npx medusa plugin:db:generate. Before this fix the command crashes; after it generates migrations correctly.


Examples

// packages/my-plugin/src/models/my-entity.ts

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

@Entity()
export class MyEntity {
  @PrimaryKey()
  id: string

  @Enum(() => MyStatus)
  status: MyStatus
}
# Reproduce
npx medusa plugin:db:generate

# Before fix: MikroORM crashes trying to process the enum as an entity
# After fix: migration generated successfully

Checklist

  • I have added a changeset for this PR
    • Every non-breaking change should be marked as a patch
    • To add a changeset, run yarn changeset and follow the prompts
  • The changes are covered by relevant tests
  • I have verified the code works as intended locally
  • I have linked the related issue(s) if applicable

Additional Context

The regular (non-plugin) db:generate path in load-internal.ts already uses the same typeof === "function" pattern in its cleanupResources function. This PR aligns the plugin path with that existing approach.


Note

Medium Risk
Changes entity/resource detection for migrations and module loading; if MetadataStorage.PATH_SYMBOL isn’t present in some valid entities, they may be skipped and migrations could be incomplete, but the change is small and localized.

Overview
Prevents plugin:db:generate from crashing when model files export non-entity values by tightening the entity filter to only accept DML entities or exports marked with MikroORM MetadataStorage.PATH_SYMBOL.

Aligns the module auto-loader’s cleanupResources logic with the same PATH_SYMBOL check so only real entities/services are kept when scanning module directories.

Written by Cursor Bugbot for commit 1d5e0a9. This will update automatically on new commits. Configure here.

… enums

MetadataStorage.getMetadataFromDecorator() returns a truthy EntityMetadata
for any input with a .name property, causing TypeScript enums to be
incorrectly treated as database entities. This crashes MikroORM when it
tries to process them during migration generation.

Replace the MetadataStorage check with typeof === "function", which
correctly identifies MikroORM class entities while rejecting enum objects.
This matches the proven pattern already used in load-internal.ts
cleanupResources for the regular db:generate path.
@shubhamchoudhary-2003 shubhamchoudhary-2003 requested a review from a team as a code owner February 6, 2026 11:17
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 6, 2026

🦋 Changeset detected

Latest commit: 6aedc6b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 76 packages
Name Type
@medusajs/medusa Patch
@medusajs/test-utils Patch
@medusajs/medusa-oas-cli Patch
integration-tests-http Patch
@medusajs/analytics Patch
@medusajs/api-key Patch
@medusajs/auth Patch
@medusajs/caching Patch
@medusajs/cart Patch
@medusajs/currency Patch
@medusajs/customer Patch
@medusajs/file Patch
@medusajs/fulfillment Patch
@medusajs/index Patch
@medusajs/inventory Patch
@medusajs/link-modules Patch
@medusajs/locking Patch
@medusajs/notification Patch
@medusajs/order Patch
@medusajs/payment Patch
@medusajs/pricing Patch
@medusajs/product Patch
@medusajs/promotion Patch
@medusajs/rbac Patch
@medusajs/region Patch
@medusajs/sales-channel Patch
@medusajs/settings Patch
@medusajs/stock-location Patch
@medusajs/store Patch
@medusajs/tax Patch
@medusajs/translation Patch
@medusajs/user Patch
@medusajs/workflow-engine-inmemory Patch
@medusajs/workflow-engine-redis Patch
@medusajs/draft-order Patch
@medusajs/oas-github-ci Patch
@medusajs/cache-inmemory Patch
@medusajs/cache-redis Patch
@medusajs/event-bus-local Patch
@medusajs/event-bus-redis Patch
@medusajs/analytics-local Patch
@medusajs/analytics-posthog Patch
@medusajs/auth-emailpass Patch
@medusajs/auth-github Patch
@medusajs/auth-google Patch
@medusajs/caching-redis Patch
@medusajs/file-local Patch
@medusajs/file-s3 Patch
@medusajs/fulfillment-manual Patch
@medusajs/locking-postgres Patch
@medusajs/locking-redis Patch
@medusajs/notification-local Patch
@medusajs/notification-sendgrid Patch
@medusajs/payment-stripe Patch
@medusajs/core-flows Patch
@medusajs/framework Patch
@medusajs/js-sdk Patch
@medusajs/modules-sdk Patch
@medusajs/orchestration Patch
@medusajs/types Patch
@medusajs/utils Patch
@medusajs/workflows-sdk Patch
@medusajs/cli Patch
@medusajs/deps Patch
@medusajs/telemetry Patch
@medusajs/admin-bundler Patch
@medusajs/admin-sdk Patch
@medusajs/admin-shared Patch
@medusajs/admin-vite-plugin Patch
@medusajs/dashboard Patch
@medusajs/icons Patch
@medusajs/toolbox Patch
@medusajs/ui-preset Patch
create-medusa-app Patch
medusa-dev-cli Patch
@medusajs/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 6, 2026

@shubhamchoudhary-2003 is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@shubhamchoudhary-2003
Copy link
Copy Markdown
Author

@carlos-r-l-rodrigues any update

Copy link
Copy Markdown
Contributor

@NicolasGorga NicolasGorga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just Adrien's comment.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@shubhamchoudhary-2003 shubhamchoudhary-2003 force-pushed the fix/plugin-db-generate-enum-crash branch from 1d5e0a9 to 9aa2231 Compare March 24, 2026 19:16
@cursor
Copy link
Copy Markdown

cursor bot commented Mar 24, 2026

You have used all of your free Bugbot PR reviews.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

Copy link
Copy Markdown
Contributor

@NicolasGorga NicolasGorga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a changeset is missing. Should have same message as the PR title :)

Copy link
Copy Markdown
Contributor

@NicolasGorga NicolasGorga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution Shubham!

@medusa-os-bot
Copy link
Copy Markdown

medusa-os-bot bot commented Apr 9, 2026

Thank you for your contribution!

After reviewing this PR, we need a few things addressed before we can move forward:

Required changes:

  • Add a linked issue — per our contribution guidelines, non-trivial bug fixes should reference an issue (via a closing keyword like closes #<number>). If no issue exists yet, please open one first so the bug is tracked.
  • Add tests — your own checklist notes this is missing. Please add at least a unit test for getEntitiesForModule() in packages/medusa/src/commands/plugin/db/generate.ts that verifies enum exports are filtered out while class entities are kept.

The fix itself looks correct and aligns with the cleanupResources pattern already used in load-internal.ts. Thanks for tracking this down!

Copy link
Copy Markdown
Contributor

@NicolasGorga NicolasGorga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @shubhamchoudhary-2003 could you add tests? See for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants