1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20260106185528 extends Migration {
4 override async up(): Promise<void> {
5 this.addSql(
6 `alter table if exists "order_shipping_method_adjustment" add column if not exists "version" integer not null default 1;`
7 )
8 this.addSql(
9 `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_order_shipping_method_adjustment_version_shipping_method" ON "order_shipping_method_adjustment" ("version", "shipping_method_id") WHERE deleted_at IS NULL;`
10 )
11 this.addSql(`WITH latest_order_shipping_version AS (
12 SELECT
13 os.shipping_method_id AS shipping_method_id,
14 MAX(os.version) AS version
15 FROM "order_shipping" os
16 WHERE os.deleted_at IS NULL
17 GROUP BY os.shipping_method_id
18 )
19 UPDATE "order_shipping_method_adjustment" osma
20 SET version = losv.version
21 FROM latest_order_shipping_version losv
22 WHERE osma.shipping_method_id = losv.shipping_method_id
23 AND osma.version <> losv.version
24 `)
25 }
26
27 override async down(): Promise<void> {
28 this.addSql(
29 `drop index if exists "IDX_order_shipping_method_adjustment_version_shipping_method";`
30 )
31 this.addSql(
32 `alter table if exists "order_shipping_method_adjustment" drop column if exists "version";`
33 )
34 }
35 }