Skip to content

Conversation

DDA1O1
Copy link

@DDA1O1 DDA1O1 commented Oct 11, 2025

Fixes #13735

What

This Pull Request introduces new database migrations to remove multiple redundant indexes across several core modules, including product, cart, order, customer, and inventory.

Why

As detailed in issue #13735, a fresh Medusa installation produces numerous "Duplicate Index" warnings. These legacy indexes add unnecessary write overhead and provide no performance benefit. This PR cleans up the schema to resolve these warnings and improve database health.

How

I have added one new, reversible migration file for each of the five affected modules:

  • @medusajs/product
  • @medusajs/cart
  • @medusajs/order
  • @medusajs/customer
  • @medusajs/inventory

Each migration's up() method safely drops the older, redundant index, and its down() method re-creates it, ensuring the change is fully reversible and non-destructive.

Testing

I have tested these migrations by following the local development workflow outlined in the CONTRIBUTING.md guide.

  1. Setup:

    • Cloned my forked Medusa repository locally .
    • Created a separate, fresh test project using npx create-medusa-app@latest my-medusa-store.
    • The test project's PostgreSQL database, which already contained the schema with the duplicate indexes.
  2. Linking Local Source Code:

    • In the test project's package.json, I modified all @medusajs/* dependencies and resolutions to point to the local packages in my forked repository (e.g., "@medusajs/product": "file:../medusa/packages/modules/product").
    • From the test project's directory, I ran yarn install to link the local, modified Medusa source code into its node_modules.
  3. Build & Migration:

    • Inside my forked Medusa repository, I ran yarn build to compile the new TypeScript migration files.
    • From the root of the test project, I then executed the migration command: npx medusa migration run.
  4. Verification:

    • The command successfully identified and ran only the new migration files I had created.
    • I also confirmed via direct SQL queries that the old, redundant indexes were correctly dropped from all affected tables (product_collection, customer_group, etc.).

Several tables within the cart module had duplicate indexes created by older migrations. These provide no performance benefit and add unnecessary overhead to write operations (INSERT, UPDATE).

This commit introduces a new migration to safely drop the older, redundant indexes, keeping the more explicitly named ones.

Removes the following indexes:
- `IDX_line_item_cart_id` from `cart_line_item`
- `IDX_adjustment_item_id` from `cart_line_item_adjustment`
- `IDX_tax_line_item_id` from `cart_line_item_tax_line`
- `IDX_shipping_method_cart_id` from `cart_shipping_method`
- `IDX_adjustment_shipping_method_id` from `cart_shipping_method_adjustment`
- `IDX_tax_line_shipping_method_id` from `cart_shipping_method_tax_line`
Database linters identified several instances of duplicate indexes on the `order_item` and `order_shipping` tables.
An analysis of the database schema identified two functionally identical unique indexes on the `name` column of the `customer_group` table: `IDX_customer_group_name` and `IDX_customer_group_name_unique`.
A database schema analysis revealed two functionally identical unique composite indexes on the `inventory_level` table: `IDX_inventory_level_item_location` and `IDX_inventory_level_location_id_inventory_item_id`.
@DDA1O1 DDA1O1 requested a review from a team as a code owner October 11, 2025 08:24
Copy link

changeset-bot bot commented Oct 11, 2025

🦋 Changeset detected

Latest commit: 3d6bacd

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

This PR includes changesets to release 74 packages
Name Type
@medusajs/cart Patch
@medusajs/customer Patch
@medusajs/inventory Patch
@medusajs/order Patch
@medusajs/product Patch
@medusajs/medusa Patch
integration-tests-http Patch
@medusajs/test-utils Patch
@medusajs/medusa-oas-cli Patch
@medusajs/analytics Patch
@medusajs/api-key Patch
@medusajs/auth Patch
@medusajs/caching Patch
@medusajs/currency Patch
@medusajs/file Patch
@medusajs/fulfillment Patch
@medusajs/index Patch
@medusajs/link-modules Patch
@medusajs/locking Patch
@medusajs/notification Patch
@medusajs/payment Patch
@medusajs/pricing Patch
@medusajs/promotion Patch
@medusajs/region Patch
@medusajs/sales-channel Patch
@medusajs/settings Patch
@medusajs/stock-location Patch
@medusajs/store Patch
@medusajs/tax 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

Copy link

vercel bot commented Oct 11, 2025

@DDA1O1 is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

cursor[bot]

This comment was marked as outdated.

@DDA1O1 DDA1O1 force-pushed the fix/db-duplicate-indexes branch from fcc7791 to 00d2b9d Compare October 11, 2025 09:09
@DDA1O1 DDA1O1 requested a review from willbouch October 14, 2025 12:46
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@DDA1O1 DDA1O1 changed the title fix(db): Remove redundant indexes to resolve schema warnings fix(): Remove redundant indexes to resolve schema warnings Oct 14, 2025
@DDA1O1
Copy link
Author

DDA1O1 commented Oct 14, 2025

hey @willbouch , I've made all the changes you suggested.

willbouch
willbouch previously approved these changes Oct 14, 2025
cursor[bot]

This comment was marked as outdated.

@willbouch willbouch dismissed their stale review October 17, 2025 12:37

Missed something

@willbouch
Copy link
Contributor

Hey @DDA1O1. It looks like those indexes (at least some I checked) are also defined in the models (so the TS files defining models like "Order" or "OrderShipping". For example, here

WE should clean that up/make sure the stuff there is up to date

@DDA1O1
Copy link
Author

DDA1O1 commented Oct 17, 2025

Hey @DDA1O1. It looks like those indexes (at least some I checked) are also defined in the models (so the TS files defining models like "Order" or "OrderShipping". For example, here

WE should clean that up/make sure the stuff there is up to date

hey thanks for letting me know definitely going to clean the dropped indexes

@DDA1O1 DDA1O1 requested a review from willbouch October 17, 2025 13:57
@willbouch
Copy link
Contributor

I see you cleaned them up, but the thing is that I feel some are missing no? You cleaned up duplicates, that's fine, but it looks like the model now do not define the IDX that actually live in the database. The model has one IDX defined, which you removed, but the database had 2 IDX and you removed one. That means that the model is missing the actual IDX missing no?

@DDA1O1
Copy link
Author

DDA1O1 commented Oct 17, 2025

I should have talked to you before implementing any change. Is there anything else that i should watch out or should i define the indexes that actually live in the database

@willbouch
Copy link
Contributor

@DDA1O1 Yeah I did not know before yesterday that the IDS were defined in the model too, sorry on that one. That being said, I believe you should just clean up the ones that are not in the model. Even if the name of the index is not the best one of the 2, it is simpler to get rid of those ones that are not in the model I think

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.

[Bug]: Duplicate indexes on multiple tables in a fresh installation

2 participants