1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20240719123015 extends Migration {
4 async up(): Promise<void> {
5 this.addSql(
6 `
7 ALTER TABLE "reservation_item" ALTER COLUMN "quantity" TYPE numeric;
8 ALTER TABLE "reservation_item" ADD COLUMN IF NOT EXISTS "raw_quantity" JSONB NULL;
9
10 ALTER TABLE "inventory_level" ALTER COLUMN "stocked_quantity" TYPE numeric;
11 ALTER TABLE "inventory_level" ADD COLUMN IF NOT EXISTS "raw_stocked_quantity" JSONB NULL;
12
13 ALTER TABLE "inventory_level" ALTER COLUMN "reserved_quantity" TYPE numeric;
14 ALTER TABLE "inventory_level" ADD COLUMN IF NOT EXISTS "raw_reserved_quantity" JSONB NULL;
15
16 ALTER TABLE "inventory_level" ALTER COLUMN "incoming_quantity" TYPE numeric;
17 ALTER TABLE "inventory_level" ADD COLUMN IF NOT EXISTS "raw_incoming_quantity" JSONB NULL;
18
19
20 DROP INDEX IF EXISTS "IDX_inventory_item_sku_unique";
21 DROP INDEX IF EXISTS "IDX_inventory_level_inventory_item_id";
22 DROP INDEX IF EXISTS "IDX_inventory_level_location_id";
23 DROP INDEX IF EXISTS "IDX_reservation_item_line_item_id";
24 DROP INDEX IF EXISTS "IDX_reservation_item_location_id";
25 DROP INDEX IF EXISTS "IDX_reservation_item_inventory_item_id";
26
27 CREATE UNIQUE INDEX IF NOT EXISTS "IDX_inventory_item_sku_unique" ON "inventory_item" (sku) WHERE deleted_at IS NULL;
28 CREATE INDEX IF NOT EXISTS "IDX_inventory_level_inventory_item_id" ON "inventory_level" (inventory_item_id) WHERE deleted_at IS NULL;
29 CREATE INDEX IF NOT EXISTS "IDX_inventory_level_location_id" ON "inventory_level" (location_id) WHERE deleted_at IS NULL;
30 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_line_item_id" ON "reservation_item" (line_item_id) WHERE deleted_at IS NULL;
31 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_location_id" ON "reservation_item" (location_id) WHERE deleted_at IS NULL;
32 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_inventory_item_id" ON "reservation_item" (inventory_item_id) WHERE deleted_at IS NULL;
33
34 CREATE UNIQUE INDEX "IDX_inventory_level_item_location" ON "inventory_level" (inventory_item_id, location_id) WHERE deleted_at IS NULL;
35 `
36 )
37 }
38
39 async down(): Promise<void> {
40 this.addSql(
41 `
42 ALTER TABLE "reservation_item" ALTER COLUMN "quantity" TYPE integer;
43 ALTER TABLE "reservation_item" DROP COLUMN IF EXISTS "raw_quantity";
44
45 ALTER TABLE "inventory_level" ALTER COLUMN "stocked_quantity" TYPE integer;
46 ALTER TABLE "inventory_level" DROP COLUMN IF EXISTS "raw_stocked_quantity";
47
48 ALTER TABLE "inventory_level" ALTER COLUMN "reserved_quantity" TYPE integer;
49 ALTER TABLE "inventory_level" DROP COLUMN IF EXISTS "raw_reserved_quantity";
50
51 ALTER TABLE "inventory_level" ALTER COLUMN "incoming_quantity" TYPE integer;
52 ALTER TABLE "inventory_level" DROP COLUMN IF EXISTS "raw_incoming_quantity";
53
54
55 DROP INDEX IF EXISTS "IDX_inventory_item_sku_unique";
56 DROP INDEX IF EXISTS "IDX_inventory_level_inventory_item_id";
57 DROP INDEX IF EXISTS "IDX_inventory_level_location_id";
58 DROP INDEX IF EXISTS "IDX_reservation_item_line_item_id";
59 DROP INDEX IF EXISTS "IDX_reservation_item_location_id";
60 DROP INDEX IF EXISTS "IDX_reservation_item_inventory_item_id";
61
62 CREATE UNIQUE INDEX IF NOT EXISTS "IDX_inventory_item_sku_unique" ON "inventory_item" (sku);
63 CREATE INDEX IF NOT EXISTS "IDX_inventory_level_inventory_item_id" ON "inventory_level" (inventory_item_id);
64 CREATE INDEX IF NOT EXISTS "IDX_inventory_level_location_id" ON "inventory_level" (location_id);
65 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_line_item_id" ON "reservation_item" (line_item_id);
66 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_location_id" ON "reservation_item" (location_id);
67 CREATE INDEX IF NOT EXISTS "IDX_reservation_item_inventory_item_id" ON "reservation_item" (inventory_item_id);
68
69 DROP INDEX IF EXISTS "IDX_inventory_level_item_location"
70 `
71 )
72 }
73 }