From a34fcfab351a79badfb24255602ca3c6ff60bffd Mon Sep 17 00:00:00 2001 From: debashish Date: Tue, 21 Oct 2025 19:20:10 +0530 Subject: [PATCH] fix(): Remove redundant indexes to resolve schema warnings (#13736) 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.). --- .changeset/poor-llamas-march.md | 9 + .../src/migrations/.snapshot-medusa-cart.json | 54 -- .../src/migrations/Migration20251017153909.ts | 33 + .../cart/src/models/line-item-adjustment.ts | 4 +- .../cart/src/models/line-item-tax-line.ts | 2 +- packages/modules/cart/src/models/line-item.ts | 2 +- .../src/models/shipping-method-adjustment.ts | 2 +- .../src/models/shipping-method-tax-line.ts | 2 +- .../cart/src/models/shipping-method.ts | 2 +- .../migrations/.snapshot-medusa-customer.json | 30 +- .../src/migrations/Migration20251010130829.ts | 17 + .../.snapshot-medusa-inventory.json | 33 +- .../src/migrations/Migration20251010131115.ts | 17 + .../migrations/.snapshot-medusa-order.json | 595 ++++++++++-------- .../src/migrations/Migration20251017155709.ts | 25 + .../modules/order/src/models/order-item.ts | 4 +- .../order/src/models/order-shipping-method.ts | 4 +- .../src/migrations/Migration20251011090511.ts | 18 + 18 files changed, 501 insertions(+), 352 deletions(-) create mode 100644 .changeset/poor-llamas-march.md create mode 100644 packages/modules/cart/src/migrations/Migration20251017153909.ts create mode 100644 packages/modules/customer/src/migrations/Migration20251010130829.ts create mode 100644 packages/modules/inventory/src/migrations/Migration20251010131115.ts create mode 100644 packages/modules/order/src/migrations/Migration20251017155709.ts create mode 100644 packages/modules/product/src/migrations/Migration20251011090511.ts diff --git a/.changeset/poor-llamas-march.md b/.changeset/poor-llamas-march.md new file mode 100644 index 0000000000..2364bf1b81 --- /dev/null +++ b/.changeset/poor-llamas-march.md @@ -0,0 +1,9 @@ +--- +"@medusajs/cart": patch +"@medusajs/customer": patch +"@medusajs/inventory": patch +"@medusajs/order": patch +"@medusajs/product": patch +--- + +fix(): Remove redundant indexes from product, cart, order, customer, and inventory modules to improve database schema health. diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index aa60359243..0099d6a7a3 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -895,15 +895,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_deleted_at\" ON \"cart_line_item\" (deleted_at) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_line_item_cart_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL" - }, { "keyName": "IDX_line_item_variant_id", "columnNames": [], @@ -1116,15 +1107,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_adjustment_promotion_id\" ON \"cart_line_item_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL" }, - { - "keyName": "IDX_adjustment_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" - }, { "keyName": "cart_line_item_adjustment_pkey", "columnNames": [ @@ -1297,15 +1279,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_tax_line_tax_rate_id\" ON \"cart_line_item_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" }, - { - "keyName": "IDX_tax_line_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" - }, { "keyName": "cart_line_item_tax_line_pkey", "columnNames": [ @@ -1482,15 +1455,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_shipping_method_cart_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL" - }, { "keyName": "IDX_shipping_method_option_id", "columnNames": [], @@ -1681,15 +1645,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_adjustment_promotion_id\" ON \"cart_shipping_method_adjustment\" (promotion_id) WHERE deleted_at IS NULL AND promotion_id IS NOT NULL" }, - { - "keyName": "IDX_adjustment_shipping_method_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" - }, { "keyName": "cart_shipping_method_adjustment_pkey", "columnNames": [ @@ -1846,15 +1801,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_deleted_at\" ON \"cart_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_tax_line_shipping_method_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL" - }, { "keyName": "IDX_shipping_method_tax_line_tax_rate_id", "columnNames": [], diff --git a/packages/modules/cart/src/migrations/Migration20251017153909.ts b/packages/modules/cart/src/migrations/Migration20251017153909.ts new file mode 100644 index 0000000000..28b76cc1d1 --- /dev/null +++ b/packages/modules/cart/src/migrations/Migration20251017153909.ts @@ -0,0 +1,33 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20251017153909 extends Migration { + + override async up(): Promise { + this.addSql(`drop index if exists "IDX_line_item_cart_id";`); + + this.addSql(`drop index if exists "IDX_adjustment_item_id";`); + + this.addSql(`drop index if exists "IDX_tax_line_item_id";`); + + this.addSql(`drop index if exists "IDX_shipping_method_cart_id";`); + + this.addSql(`drop index if exists "IDX_adjustment_shipping_method_id";`); + + this.addSql(`drop index if exists "IDX_tax_line_shipping_method_id";`); + } + + override async down(): Promise { + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;`); + } + +} diff --git a/packages/modules/cart/src/models/line-item-adjustment.ts b/packages/modules/cart/src/models/line-item-adjustment.ts index 0a79e5631f..8268068517 100644 --- a/packages/modules/cart/src/models/line-item-adjustment.ts +++ b/packages/modules/cart/src/models/line-item-adjustment.ts @@ -24,8 +24,8 @@ const LineItemAdjustment = model on: ["promotion_id"], where: "deleted_at IS NULL AND promotion_id IS NOT NULL", }, - { - name: "IDX_adjustment_item_id", + { + name: "IDX_cart_line_item_adjustment_item_id", on: ["item_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/cart/src/models/line-item-tax-line.ts b/packages/modules/cart/src/models/line-item-tax-line.ts index 73f5b790c6..125af2e36d 100644 --- a/packages/modules/cart/src/models/line-item-tax-line.ts +++ b/packages/modules/cart/src/models/line-item-tax-line.ts @@ -27,7 +27,7 @@ const LineItemTaxLine = model where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", }, { - name: "IDX_tax_line_item_id", + name: "IDX_cart_line_item_tax_line_item_id", on: ["item_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/cart/src/models/line-item.ts b/packages/modules/cart/src/models/line-item.ts index c79b3d4ccd..d35f9e44c4 100644 --- a/packages/modules/cart/src/models/line-item.ts +++ b/packages/modules/cart/src/models/line-item.ts @@ -46,7 +46,7 @@ const LineItem = model ) .indexes([ { - name: "IDX_line_item_cart_id", + name: "IDX_cart_line_item_cart_id", on: ["cart_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/cart/src/models/shipping-method-adjustment.ts b/packages/modules/cart/src/models/shipping-method-adjustment.ts index 935c791cc2..93cc36b82e 100644 --- a/packages/modules/cart/src/models/shipping-method-adjustment.ts +++ b/packages/modules/cart/src/models/shipping-method-adjustment.ts @@ -27,7 +27,7 @@ const ShippingMethodAdjustment = model where: "deleted_at IS NULL AND promotion_id IS NOT NULL", }, { - name: "IDX_adjustment_shipping_method_id", + name: "IDX_cart_shipping_method_adjustment_shipping_method_id", on: ["shipping_method_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/cart/src/models/shipping-method-tax-line.ts b/packages/modules/cart/src/models/shipping-method-tax-line.ts index 544956aa56..4a8333ddb6 100644 --- a/packages/modules/cart/src/models/shipping-method-tax-line.ts +++ b/packages/modules/cart/src/models/shipping-method-tax-line.ts @@ -22,7 +22,7 @@ const ShippingMethodTaxLine = model ) .indexes([ { - name: "IDX_tax_line_shipping_method_id", + name: "IDX_cart_shipping_method_tax_line_shipping_method_id", on: ["shipping_method_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/cart/src/models/shipping-method.ts b/packages/modules/cart/src/models/shipping-method.ts index b3a303c2c6..945c0cef0e 100644 --- a/packages/modules/cart/src/models/shipping-method.ts +++ b/packages/modules/cart/src/models/shipping-method.ts @@ -31,7 +31,7 @@ const ShippingMethod = model ) .indexes([ { - name: "IDX_shipping_method_cart_id", + name: "IDX_cart_shipping_method_cart_id", on: ["cart_id"], where: "deleted_at IS NULL", }, diff --git a/packages/modules/customer/src/migrations/.snapshot-medusa-customer.json b/packages/modules/customer/src/migrations/.snapshot-medusa-customer.json index 7a56c83375..b56f1ee795 100644 --- a/packages/modules/customer/src/migrations/.snapshot-medusa-customer.json +++ b/packages/modules/customer/src/migrations/.snapshot-medusa-customer.json @@ -128,6 +128,7 @@ "keyName": "IDX_customer_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_deleted_at\" ON \"customer\" (deleted_at) WHERE deleted_at IS NULL" @@ -136,6 +137,7 @@ "keyName": "IDX_customer_email_has_account_unique", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_customer_email_has_account_unique\" ON \"customer\" (email, has_account) WHERE deleted_at IS NULL" @@ -146,12 +148,14 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } ], "checks": [], - "foreignKeys": {} + "foreignKeys": {}, + "nativeEnums": {} }, { "columns": { @@ -341,6 +345,7 @@ "keyName": "IDX_customer_address_customer_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_address_customer_id\" ON \"customer_address\" (customer_id) WHERE deleted_at IS NULL" @@ -349,6 +354,7 @@ "keyName": "IDX_customer_address_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_address_deleted_at\" ON \"customer_address\" (deleted_at) WHERE deleted_at IS NULL" @@ -357,6 +363,7 @@ "keyName": "IDX_customer_address_unique_customer_billing", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_customer_address_unique_customer_billing\" ON \"customer_address\" (customer_id) WHERE \"is_default_billing\" = true AND deleted_at IS NULL" @@ -365,6 +372,7 @@ "keyName": "IDX_customer_address_unique_customer_shipping", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_customer_address_unique_customer_shipping\" ON \"customer_address\" (customer_id) WHERE \"is_default_shipping\" = true AND deleted_at IS NULL" @@ -375,6 +383,7 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } @@ -394,7 +403,8 @@ "deleteRule": "cascade", "updateRule": "cascade" } - } + }, + "nativeEnums": {} }, { "columns": { @@ -474,6 +484,7 @@ "keyName": "IDX_customer_group_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_group_deleted_at\" ON \"customer_group\" (deleted_at) WHERE deleted_at IS NULL" @@ -482,6 +493,7 @@ "keyName": "IDX_customer_group_name_unique", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_customer_group_name_unique\" ON \"customer_group\" (name) WHERE deleted_at IS NULL" @@ -492,12 +504,14 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } ], "checks": [], - "foreignKeys": {} + "foreignKeys": {}, + "nativeEnums": {} }, { "columns": { @@ -586,6 +600,7 @@ "keyName": "IDX_customer_group_customer_customer_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_group_customer_customer_id\" ON \"customer_group_customer\" (customer_id) WHERE deleted_at IS NULL" @@ -594,6 +609,7 @@ "keyName": "IDX_customer_group_customer_customer_group_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_group_customer_customer_group_id\" ON \"customer_group_customer\" (customer_group_id) WHERE deleted_at IS NULL" @@ -602,6 +618,7 @@ "keyName": "IDX_customer_group_customer_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_customer_group_customer_deleted_at\" ON \"customer_group_customer\" (deleted_at) WHERE deleted_at IS NULL" @@ -612,12 +629,15 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } ], "checks": [], - "foreignKeys": {} + "foreignKeys": {}, + "nativeEnums": {} } - ] + ], + "nativeEnums": {} } diff --git a/packages/modules/customer/src/migrations/Migration20251010130829.ts b/packages/modules/customer/src/migrations/Migration20251010130829.ts new file mode 100644 index 0000000000..6302e74c29 --- /dev/null +++ b/packages/modules/customer/src/migrations/Migration20251010130829.ts @@ -0,0 +1,17 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20251010130829 extends Migration { + + override async up(): Promise { + this.addSql( + 'DROP INDEX IF EXISTS "IDX_customer_group_name";' + ); + } + + override async down(): Promise { + this.addSql( + 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_customer_group_name" ON "customer_group" ("name") WHERE "deleted_at" IS NULL;' + ); + } + +} diff --git a/packages/modules/inventory/src/migrations/.snapshot-medusa-inventory.json b/packages/modules/inventory/src/migrations/.snapshot-medusa-inventory.json index 8b04031e1b..9a76e5dcc9 100644 --- a/packages/modules/inventory/src/migrations/.snapshot-medusa-inventory.json +++ b/packages/modules/inventory/src/migrations/.snapshot-medusa-inventory.json @@ -182,22 +182,16 @@ "keyName": "IDX_inventory_item_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_inventory_item_deleted_at\" ON \"inventory_item\" (deleted_at) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_inventory_item_deleted_at", - "columnNames": [], - "composite": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_inventory_item_deleted_at\" ON \"inventory_item\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, { "keyName": "IDX_inventory_item_sku", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_inventory_item_sku\" ON \"inventory_item\" (sku) WHERE deleted_at IS NULL" @@ -208,12 +202,14 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } ], "checks": [], - "foreignKeys": {} + "foreignKeys": {}, + "nativeEnums": {} }, { "columns": { @@ -350,6 +346,7 @@ "keyName": "IDX_inventory_level_inventory_item_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_inventory_level_inventory_item_id\" ON \"inventory_level\" (inventory_item_id) WHERE deleted_at IS NULL" @@ -358,6 +355,7 @@ "keyName": "IDX_inventory_level_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_inventory_level_deleted_at\" ON \"inventory_level\" (deleted_at) WHERE deleted_at IS NULL" @@ -366,6 +364,7 @@ "keyName": "IDX_inventory_level_location_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_inventory_level_location_id\" ON \"inventory_level\" (location_id) WHERE deleted_at IS NULL" @@ -374,6 +373,7 @@ "keyName": "IDX_inventory_level_location_id_inventory_item_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_inventory_level_location_id_inventory_item_id\" ON \"inventory_level\" (inventory_item_id, location_id) WHERE deleted_at IS NULL" @@ -384,6 +384,7 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } @@ -403,7 +404,8 @@ "deleteRule": "cascade", "updateRule": "cascade" } - } + }, + "nativeEnums": {} }, { "columns": { @@ -547,6 +549,7 @@ "keyName": "IDX_reservation_item_inventory_item_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_reservation_item_inventory_item_id\" ON \"reservation_item\" (inventory_item_id) WHERE deleted_at IS NULL" @@ -555,6 +558,7 @@ "keyName": "IDX_reservation_item_deleted_at", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_reservation_item_deleted_at\" ON \"reservation_item\" (deleted_at) WHERE deleted_at IS NULL" @@ -563,6 +567,7 @@ "keyName": "IDX_reservation_item_line_item_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_reservation_item_line_item_id\" ON \"reservation_item\" (line_item_id) WHERE deleted_at IS NULL" @@ -571,6 +576,7 @@ "keyName": "IDX_reservation_item_location_id", "columnNames": [], "composite": false, + "constraint": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_reservation_item_location_id\" ON \"reservation_item\" (location_id) WHERE deleted_at IS NULL" @@ -581,6 +587,7 @@ "id" ], "composite": false, + "constraint": true, "primary": true, "unique": true } @@ -600,7 +607,9 @@ "deleteRule": "cascade", "updateRule": "cascade" } - } + }, + "nativeEnums": {} } - ] + ], + "nativeEnums": {} } diff --git a/packages/modules/inventory/src/migrations/Migration20251010131115.ts b/packages/modules/inventory/src/migrations/Migration20251010131115.ts new file mode 100644 index 0000000000..3e61f89610 --- /dev/null +++ b/packages/modules/inventory/src/migrations/Migration20251010131115.ts @@ -0,0 +1,17 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20251010131115 extends Migration { + + override async up(): Promise { + this.addSql( + 'DROP INDEX IF EXISTS "IDX_inventory_level_item_location";' + ); + } + + override async down(): Promise { + this.addSql( + 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_inventory_level_item_location" ON "inventory_level" (inventory_item_id, location_id) WHERE deleted_at IS NULL;' + ); + } + +} diff --git a/packages/modules/order/src/migrations/.snapshot-medusa-order.json b/packages/modules/order/src/migrations/.snapshot-medusa-order.json index 3d043a65d6..3af9c75022 100644 --- a/packages/modules/order/src/migrations/.snapshot-medusa-order.json +++ b/packages/modules/order/src/migrations/.snapshot-medusa-order.json @@ -1,5 +1,7 @@ { - "namespaces": ["public"], + "namespaces": [ + "public" + ], "name": "public", "tables": [ { @@ -164,7 +166,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_address_deleted_at\" ON \"order_address\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_address_deleted_at\" ON \"order_address\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_address_customer_id", @@ -177,7 +179,9 @@ }, { "keyName": "order_address_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -380,7 +384,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_deleted_at\" ON \"order\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_deleted_at\" ON \"order\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_display_id", @@ -465,7 +469,9 @@ }, { "keyName": "order_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -476,18 +482,26 @@ "foreignKeys": { "order_shipping_address_id_foreign": { "constraintName": "order_shipping_address_id_foreign", - "columnNames": ["shipping_address_id"], + "columnNames": [ + "shipping_address_id" + ], "localTableName": "public.order", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_address", "deleteRule": "set null", "updateRule": "cascade" }, "order_billing_address_id_foreign": { "constraintName": "order_billing_address_id_foreign", - "columnNames": ["billing_address_id"], + "columnNames": [ + "billing_address_id" + ], "localTableName": "public.order", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_address", "deleteRule": "set null", "updateRule": "cascade" @@ -750,16 +764,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_deleted_at\" ON \"order_change\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_change_order_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_order_id\" ON \"order_change\" (order_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_deleted_at\" ON \"order_change\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_change_return_id", @@ -817,7 +822,9 @@ }, { "keyName": "order_change_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -828,9 +835,13 @@ "foreignKeys": { "order_change_order_id_foreign": { "constraintName": "order_change_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_change", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "updateRule": "cascade" } @@ -1037,16 +1048,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_action_deleted_at\" ON \"order_change_action\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_change_action_order_change_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_action_order_change_id\" ON \"order_change_action\" (order_change_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_change_action_deleted_at\" ON \"order_change_action\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_change_action_order_id", @@ -1104,7 +1106,9 @@ }, { "keyName": "order_change_action_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -1115,9 +1119,13 @@ "foreignKeys": { "order_change_action_order_change_id_foreign": { "constraintName": "order_change_action_order_change_id_foreign", - "columnNames": ["order_change_id"], + "columnNames": [ + "order_change_id" + ], "localTableName": "public.order_change_action", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_change", "deleteRule": "cascade", "updateRule": "cascade" @@ -1242,16 +1250,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_credit_line_deleted_at\" ON \"order_credit_line\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_credit_line_order_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_credit_line_order_id\" ON \"order_credit_line\" (order_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_credit_line_deleted_at\" ON \"order_credit_line\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_credit_line_deleted_at", @@ -1264,7 +1263,9 @@ }, { "keyName": "order_credit_line_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -1275,9 +1276,13 @@ "foreignKeys": { "order_credit_line_order_id_foreign": { "constraintName": "order_credit_line_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_credit_line", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "deleteRule": "cascade", "updateRule": "cascade" @@ -1578,7 +1583,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_deleted_at\" ON \"order_line_item\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_deleted_at\" ON \"order_line_item\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_line_item_deleted_at", @@ -1618,7 +1623,9 @@ }, { "keyName": "order_line_item_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -1916,25 +1923,16 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_item_deleted_at\" ON \"order_item\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_item_deleted_at\" ON \"order_item\" (deleted_at) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_order_item_order_id", + "keyName": "IDX_order_item_order_id_version", "columnNames": [], "composite": false, "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_item_order_id\" ON \"order_item\" (order_id) WHERE deleted_at IS NULL" - }, - { - "keyName": "IDX_order_item_version", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_item_version\" ON \"order_item\" (version) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_item_order_id_version\" ON \"order_item\" (order_id, version) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_item_item_id", @@ -1956,7 +1954,9 @@ }, { "keyName": "order_item_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -1967,18 +1967,26 @@ "foreignKeys": { "order_item_order_id_foreign": { "constraintName": "order_item_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "deleteRule": "cascade", "updateRule": "cascade" }, "order_item_item_id_foreign": { "constraintName": "order_item_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.order_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "updateRule": "cascade" } @@ -2121,7 +2129,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_adjustment_deleted_at\" ON \"order_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_adjustment_deleted_at\" ON \"order_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_order_line_item_adjustment_item_id", @@ -2134,7 +2142,9 @@ }, { "keyName": "order_line_item_adjustment_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2145,9 +2155,13 @@ "foreignKeys": { "order_line_item_adjustment_item_id_foreign": { "constraintName": "order_line_item_adjustment_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.order_line_item_adjustment", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "deleteRule": "cascade", "updateRule": "cascade" @@ -2281,11 +2295,13 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_tax_line_deleted_at\" ON \"order_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_line_item_tax_line_deleted_at\" ON \"order_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "order_line_item_tax_line_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2296,9 +2312,13 @@ "foreignKeys": { "order_line_item_tax_line_item_id_foreign": { "constraintName": "order_line_item_tax_line_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.order_line_item_tax_line", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "deleteRule": "cascade", "updateRule": "cascade" @@ -2443,7 +2463,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_deleted_at\" ON \"order_shipping_method\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_deleted_at\" ON \"order_shipping_method\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_shipping_method_shipping_option_id", @@ -2456,7 +2476,9 @@ }, { "keyName": "order_shipping_method_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2593,11 +2615,13 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_adjustment_deleted_at\" ON \"order_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_adjustment_deleted_at\" ON \"order_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "order_shipping_method_adjustment_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2608,9 +2632,13 @@ "foreignKeys": { "order_shipping_method_adjustment_shipping_method_id_foreign": { "constraintName": "order_shipping_method_adjustment_shipping_method_id_foreign", - "columnNames": ["shipping_method_id"], + "columnNames": [ + "shipping_method_id" + ], "localTableName": "public.order_shipping_method_adjustment", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_shipping_method", "deleteRule": "cascade", "updateRule": "cascade" @@ -2744,11 +2772,13 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_tax_line_deleted_at\" ON \"order_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_method_tax_line_deleted_at\" ON \"order_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "order_shipping_method_tax_line_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2759,9 +2789,13 @@ "foreignKeys": { "order_shipping_method_tax_line_shipping_method_id_foreign": { "constraintName": "order_shipping_method_tax_line_shipping_method_id_foreign", - "columnNames": ["shipping_method_id"], + "columnNames": [ + "shipping_method_id" + ], "localTableName": "public.order_shipping_method_tax_line", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_shipping_method", "deleteRule": "cascade", "updateRule": "cascade" @@ -2860,7 +2894,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_summary_deleted_at\" ON \"order_summary\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_summary_deleted_at\" ON \"order_summary\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_summary_order_id_version", @@ -2882,7 +2916,9 @@ }, { "keyName": "order_summary_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -2893,9 +2929,13 @@ "foreignKeys": { "order_summary_order_id_foreign": { "constraintName": "order_summary_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_summary", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "deleteRule": "cascade", "updateRule": "cascade" @@ -3112,7 +3152,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_deleted_at\" ON \"return\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_deleted_at\" ON \"return\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_return_display_id", @@ -3132,15 +3172,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_deleted_at\" ON \"return\" (deleted_at) WHERE deleted_at IS NOT NULL" }, - { - "keyName": "IDX_return_order_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_order_id\" ON \"return\" (order_id) WHERE deleted_at IS NULL" - }, { "keyName": "IDX_return_exchange_id", "columnNames": [], @@ -3161,7 +3192,9 @@ }, { "keyName": "return_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -3172,26 +3205,38 @@ "foreignKeys": { "return_order_id_foreign": { "constraintName": "return_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.return", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "updateRule": "cascade" }, "return_exchange_id_foreign": { "constraintName": "return_exchange_id_foreign", - "columnNames": ["exchange_id"], + "columnNames": [ + "exchange_id" + ], "localTableName": "public.return", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_exchange", "deleteRule": "set null", "updateRule": "cascade" }, "return_claim_id_foreign": { "constraintName": "return_claim_id_foreign", - "columnNames": ["claim_id"], + "columnNames": [ + "claim_id" + ], "localTableName": "public.return", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_claim", "deleteRule": "set null", "updateRule": "cascade" @@ -3354,7 +3399,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_deleted_at\" ON \"order_exchange\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_deleted_at\" ON \"order_exchange\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_exchange_display_id", @@ -3394,7 +3439,9 @@ }, { "keyName": "order_exchange_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -3405,17 +3452,25 @@ "foreignKeys": { "order_exchange_order_id_foreign": { "constraintName": "order_exchange_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_exchange", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "updateRule": "cascade" }, "order_exchange_return_id_foreign": { "constraintName": "order_exchange_return_id_foreign", - "columnNames": ["return_id"], + "columnNames": [ + "return_id" + ], "localTableName": "public.order_exchange", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return", "deleteRule": "set null", "updateRule": "cascade" @@ -3549,25 +3604,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_item_deleted_at\" ON \"order_exchange_item\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_exchange_item_exchange_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_item_exchange_id\" ON \"order_exchange_item\" (exchange_id) WHERE deleted_at IS NULL" - }, - { - "keyName": "IDX_order_exchange_item_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_item_item_id\" ON \"order_exchange_item\" (item_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_exchange_item_deleted_at\" ON \"order_exchange_item\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_exchange_item_deleted_at", @@ -3580,7 +3617,9 @@ }, { "keyName": "order_exchange_item_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -3591,18 +3630,26 @@ "foreignKeys": { "order_exchange_item_exchange_id_foreign": { "constraintName": "order_exchange_item_exchange_id_foreign", - "columnNames": ["exchange_id"], + "columnNames": [ + "exchange_id" + ], "localTableName": "public.order_exchange_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_exchange", "deleteRule": "cascade", "updateRule": "cascade" }, "order_exchange_item_item_id_foreign": { "constraintName": "order_exchange_item_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.order_exchange_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "updateRule": "cascade" } @@ -3645,7 +3692,10 @@ "autoincrement": false, "primary": false, "nullable": false, - "enumItems": ["refund", "replace"], + "enumItems": [ + "refund", + "replace" + ], "mappedType": "enum" }, "no_notification": { @@ -3764,7 +3814,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_deleted_at\" ON \"order_claim\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_deleted_at\" ON \"order_claim\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_claim_display_id", @@ -3804,7 +3854,9 @@ }, { "keyName": "order_claim_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -3815,17 +3867,25 @@ "foreignKeys": { "order_claim_order_id_foreign": { "constraintName": "order_claim_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_claim", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "updateRule": "cascade" }, "order_claim_return_id_foreign": { "constraintName": "order_claim_return_id_foreign", - "columnNames": ["return_id"], + "columnNames": [ + "return_id" + ], "localTableName": "public.order_claim", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return", "deleteRule": "set null", "updateRule": "cascade" @@ -4014,7 +4074,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_deleted_at\" ON \"order_transaction\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_deleted_at\" ON \"order_transaction\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_transaction_reference_id", @@ -4025,15 +4085,6 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_reference_id\" ON \"order_transaction\" (reference_id) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_order_transaction_order_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_transaction_order_id\" ON \"order_transaction\" (order_id) WHERE deleted_at IS NULL" - }, { "keyName": "IDX_order_transaction_return_id", "columnNames": [], @@ -4090,7 +4141,9 @@ }, { "keyName": "order_transaction_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -4101,36 +4154,52 @@ "foreignKeys": { "order_transaction_order_id_foreign": { "constraintName": "order_transaction_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_transaction", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "deleteRule": "cascade", "updateRule": "cascade" }, "order_transaction_return_id_foreign": { "constraintName": "order_transaction_return_id_foreign", - "columnNames": ["return_id"], + "columnNames": [ + "return_id" + ], "localTableName": "public.order_transaction", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return", "deleteRule": "cascade", "updateRule": "cascade" }, "order_transaction_exchange_id_foreign": { "constraintName": "order_transaction_exchange_id_foreign", - "columnNames": ["exchange_id"], + "columnNames": [ + "exchange_id" + ], "localTableName": "public.order_transaction", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_exchange", "deleteRule": "cascade", "updateRule": "cascade" }, "order_transaction_claim_id_foreign": { "constraintName": "order_transaction_claim_id_foreign", - "columnNames": ["claim_id"], + "columnNames": [ + "claim_id" + ], "localTableName": "public.order_transaction", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_claim", "deleteRule": "cascade", "updateRule": "cascade" @@ -4283,16 +4352,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_deleted_at\" ON \"order_shipping\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_shipping_order_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_order_id\" ON \"order_shipping\" (order_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_deleted_at\" ON \"order_shipping\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_shipping_return_id", @@ -4322,13 +4382,13 @@ "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_claim_id\" ON \"order_shipping\" (claim_id) WHERE claim_id IS NOT NULL AND deleted_at IS NULL" }, { - "keyName": "IDX_order_shipping_version", + "keyName": "IDX_order_shipping_order_id_version", "columnNames": [], "composite": false, "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_version\" ON \"order_shipping\" (version) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_shipping_order_id_version\" ON \"order_shipping\" (order_id, version) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_shipping_shipping_method_id", @@ -4350,7 +4410,9 @@ }, { "keyName": "order_shipping_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -4361,45 +4423,65 @@ "foreignKeys": { "order_shipping_order_id_foreign": { "constraintName": "order_shipping_order_id_foreign", - "columnNames": ["order_id"], + "columnNames": [ + "order_id" + ], "localTableName": "public.order_shipping", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order", "deleteRule": "cascade", "updateRule": "cascade" }, "order_shipping_return_id_foreign": { "constraintName": "order_shipping_return_id_foreign", - "columnNames": ["return_id"], + "columnNames": [ + "return_id" + ], "localTableName": "public.order_shipping", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return", "deleteRule": "cascade", "updateRule": "cascade" }, "order_shipping_exchange_id_foreign": { "constraintName": "order_shipping_exchange_id_foreign", - "columnNames": ["exchange_id"], + "columnNames": [ + "exchange_id" + ], "localTableName": "public.order_shipping", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_exchange", "deleteRule": "set null", "updateRule": "cascade" }, "order_shipping_claim_id_foreign": { "constraintName": "order_shipping_claim_id_foreign", - "columnNames": ["claim_id"], + "columnNames": [ + "claim_id" + ], "localTableName": "public.order_shipping", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_claim", "deleteRule": "set null", "updateRule": "cascade" }, "order_shipping_shipping_method_id_foreign": { "constraintName": "order_shipping_shipping_method_id_foreign", - "columnNames": ["shipping_method_id"], + "columnNames": [ + "shipping_method_id" + ], "localTableName": "public.order_shipping", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_shipping_method", "updateRule": "cascade" } @@ -4557,25 +4639,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_deleted_at\" ON \"order_claim_item\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_order_claim_item_claim_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_claim_id\" ON \"order_claim_item\" (claim_id) WHERE deleted_at IS NULL" - }, - { - "keyName": "IDX_order_claim_item_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_item_id\" ON \"order_claim_item\" (item_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_deleted_at\" ON \"order_claim_item\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_claim_item_deleted_at", @@ -4588,7 +4652,9 @@ }, { "keyName": "order_claim_item_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -4599,18 +4665,26 @@ "foreignKeys": { "order_claim_item_claim_id_foreign": { "constraintName": "order_claim_item_claim_id_foreign", - "columnNames": ["claim_id"], + "columnNames": [ + "claim_id" + ], "localTableName": "public.order_claim_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_claim", "deleteRule": "cascade", "updateRule": "cascade" }, "order_claim_item_item_id_foreign": { "constraintName": "order_claim_item_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.order_claim_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "updateRule": "cascade" } @@ -4707,7 +4781,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_image_deleted_at\" ON \"order_claim_item_image\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_image_deleted_at\" ON \"order_claim_item_image\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_order_claim_item_image_deleted_at", @@ -4718,18 +4792,11 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_image_deleted_at\" ON \"order_claim_item_image\" (deleted_at) WHERE deleted_at IS NOT NULL" }, - { - "keyName": "IDX_order_claim_item_image_claim_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_order_claim_item_image_claim_item_id\" ON \"order_claim_item_image\" (claim_item_id) WHERE deleted_at IS NULL" - }, { "keyName": "order_claim_item_image_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -4740,9 +4807,13 @@ "foreignKeys": { "order_claim_item_image_claim_item_id_foreign": { "constraintName": "order_claim_item_image_claim_item_id_foreign", - "columnNames": ["claim_item_id"], + "columnNames": [ + "claim_item_id" + ], "localTableName": "public.order_claim_item_image", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_claim_item", "deleteRule": "cascade", "updateRule": "cascade" @@ -4858,7 +4929,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_reason_deleted_at\" ON \"return_reason\" (deleted_at) WHERE deleted_at IS NOT NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_reason_deleted_at\" ON \"return_reason\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_return_reason_deleted_at", @@ -4878,18 +4949,11 @@ "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_reason_value\" ON \"return_reason\" (value) WHERE deleted_at IS NULL" }, - { - "keyName": "IDX_return_reason_parent_return_reason_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_reason_parent_return_reason_id\" ON \"return_reason\" (parent_return_reason_id) WHERE deleted_at IS NULL" - }, { "keyName": "return_reason_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -4900,9 +4964,13 @@ "foreignKeys": { "return_reason_parent_return_reason_id_foreign": { "constraintName": "return_reason_parent_return_reason_id_foreign", - "columnNames": ["parent_return_reason_id"], + "columnNames": [ + "parent_return_reason_id" + ], "localTableName": "public.return_reason", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return_reason", "deleteRule": "set null", "updateRule": "cascade" @@ -5092,34 +5160,7 @@ "constraint": false, "primary": false, "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_item_deleted_at\" ON \"return_item\" (deleted_at) WHERE deleted_at IS NOT NULL" - }, - { - "keyName": "IDX_return_item_return_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_item_return_id\" ON \"return_item\" (return_id) WHERE deleted_at IS NULL" - }, - { - "keyName": "IDX_return_item_reason_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_item_reason_id\" ON \"return_item\" (reason_id) WHERE deleted_at IS NULL" - }, - { - "keyName": "IDX_return_item_item_id", - "columnNames": [], - "composite": false, - "constraint": false, - "primary": false, - "unique": false, - "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_item_item_id\" ON \"return_item\" (item_id) WHERE deleted_at IS NULL" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_return_item_deleted_at\" ON \"return_item\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "IDX_return_item_deleted_at", @@ -5132,7 +5173,9 @@ }, { "keyName": "return_item_pkey", - "columnNames": ["id"], + "columnNames": [ + "id" + ], "composite": false, "constraint": true, "primary": true, @@ -5143,27 +5186,39 @@ "foreignKeys": { "return_item_reason_id_foreign": { "constraintName": "return_item_reason_id_foreign", - "columnNames": ["reason_id"], + "columnNames": [ + "reason_id" + ], "localTableName": "public.return_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return_reason", "deleteRule": "set null", "updateRule": "cascade" }, "return_item_return_id_foreign": { "constraintName": "return_item_return_id_foreign", - "columnNames": ["return_id"], + "columnNames": [ + "return_id" + ], "localTableName": "public.return_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.return", "deleteRule": "cascade", "updateRule": "cascade" }, "return_item_item_id_foreign": { "constraintName": "return_item_item_id_foreign", - "columnNames": ["item_id"], + "columnNames": [ + "item_id" + ], "localTableName": "public.return_item", - "referencedColumnNames": ["id"], + "referencedColumnNames": [ + "id" + ], "referencedTableName": "public.order_line_item", "updateRule": "cascade" } diff --git a/packages/modules/order/src/migrations/Migration20251017155709.ts b/packages/modules/order/src/migrations/Migration20251017155709.ts new file mode 100644 index 0000000000..ffb0332b15 --- /dev/null +++ b/packages/modules/order/src/migrations/Migration20251017155709.ts @@ -0,0 +1,25 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20251017155709 extends Migration { + + override async up(): Promise { + this.addSql(`drop index if exists "IDX_order_item_version";`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_item_order_id_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`); + + this.addSql(`drop index if exists "IDX_order_shipping_version";`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_order_id_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`); + } + + override async down(): Promise { + this.addSql(`drop index if exists "IDX_order_item_order_id_version";`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_item_version" ON "order_item" (order_id, version) WHERE deleted_at IS NULL;`); + + this.addSql(`drop index if exists "IDX_order_shipping_order_id_version";`); + + this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_order_shipping_version" ON "order_shipping" (order_id, version) WHERE deleted_at IS NULL;`); + } + +} diff --git a/packages/modules/order/src/models/order-item.ts b/packages/modules/order/src/models/order-item.ts index 27195eef1a..753622204f 100644 --- a/packages/modules/order/src/models/order-item.ts +++ b/packages/modules/order/src/models/order-item.ts @@ -34,8 +34,8 @@ const _OrderItem = model where: "deleted_at IS NULL", }, { - name: "IDX_order_item_version", - on: ["version"], + name: "IDX_order_item_order_id_version", + on: ["order_id", "version"], unique: false, where: "deleted_at IS NULL", }, diff --git a/packages/modules/order/src/models/order-shipping-method.ts b/packages/modules/order/src/models/order-shipping-method.ts index e2518c6b9f..720cd0cd59 100644 --- a/packages/modules/order/src/models/order-shipping-method.ts +++ b/packages/modules/order/src/models/order-shipping-method.ts @@ -61,8 +61,8 @@ const _OrderShipping = model where: "claim_id IS NOT NULL AND deleted_at IS NULL", }, { - name: "IDX_order_shipping_version", - on: ["version"], + name: "IDX_order_shipping_order_id_version", + on: ["order_id", "version"], unique: false, where: "deleted_at IS NULL", }, diff --git a/packages/modules/product/src/migrations/Migration20251011090511.ts b/packages/modules/product/src/migrations/Migration20251011090511.ts new file mode 100644 index 0000000000..cddcb8ca85 --- /dev/null +++ b/packages/modules/product/src/migrations/Migration20251011090511.ts @@ -0,0 +1,18 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20251011090511 extends Migration { +// UP: Fixes the bug by dropping the bad index from product_collection. + override async up(): Promise { + this.addSql( + 'DROP INDEX IF EXISTS "IDX_product_category_deleted_at";' + ); + } + + // DOWN: Reverts the fix by re-creating the original bug. + override async down(): Promise { + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_product_category_deleted_at" ON "product_collection" ("deleted_at");' + ); + } + +}