1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20240307132720 extends Migration {
4 async up(): Promise<void> {
5 this.addSql(
6 'create table if not exists "inventory_item" ("id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "sku" text null, "origin_country" text null, "hs_code" text null, "mid_code" text null, "material" text null, "weight" int null, "length" int null, "height" int null, "width" int null, "requires_shipping" boolean not null default true, "description" text null, "title" text null, "thumbnail" text null, "metadata" jsonb null, constraint "inventory_item_pkey" primary key ("id"));'
7 )
8 this.addSql(
9 'CREATE INDEX IF NOT EXISTS "IDX_inventory_item_deleted_at" ON "inventory_item" (deleted_at) WHERE deleted_at IS NOT NULL;'
10 )
11 this.addSql(
12 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_inventory_item_sku_unique" ON "inventory_item" (sku);'
13 )
14
15 this.addSql(
16 'create table if not exists "inventory_level" ("id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "inventory_item_id" text not null, "location_id" text not null, "stocked_quantity" int not null default 0, "reserved_quantity" int not null default 0, "incoming_quantity" int not null default 0, "metadata" jsonb null, constraint "inventory_level_pkey" primary key ("id"));'
17 )
18 this.addSql(
19 'CREATE INDEX IF NOT EXISTS "IDX_inventory_level_deleted_at" ON "inventory_level" (deleted_at) WHERE deleted_at IS NOT NULL;'
20 )
21 this.addSql(
22 'CREATE INDEX IF NOT EXISTS "IDX_inventory_level_inventory_item_id" ON "inventory_level" (inventory_item_id);'
23 )
24 this.addSql(
25 'CREATE INDEX IF NOT EXISTS "IDX_inventory_level_location_id" ON "inventory_level" (location_id);'
26 )
27
28 this.addSql(
29 'create table if not exists "reservation_item" ("id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "line_item_id" text null, "location_id" text not null, "quantity" integer not null, "external_id" text null, "description" text null, "created_by" text null, "metadata" jsonb null, "inventory_item_id" text not null, constraint "reservation_item_pkey" primary key ("id"));'
30 )
31
32 this.addSql(
33 `ALTER TABLE "reservation_item" ADD COLUMN IF NOT EXISTS "allow_backorder" boolean DEFAULT false;`
34 )
35
36 this.addSql(
37 'CREATE INDEX IF NOT EXISTS "IDX_reservation_item_deleted_at" ON "reservation_item" (deleted_at) WHERE deleted_at IS NOT NULL;'
38 )
39 this.addSql(
40 'CREATE INDEX IF NOT EXISTS "IDX_reservation_item_line_item_id" ON "reservation_item" (line_item_id);'
41 )
42 this.addSql(
43 'CREATE INDEX IF NOT EXISTS "IDX_reservation_item_location_id" ON "reservation_item" (location_id);'
44 )
45 this.addSql(
46 'CREATE INDEX IF NOT EXISTS "IDX_reservation_item_inventory_item_id" ON "reservation_item" (inventory_item_id);'
47 )
48
49 this.addSql(
50 'alter table if exists "inventory_level" add constraint "inventory_level_inventory_item_id_foreign" foreign key ("inventory_item_id") references "inventory_item" ("id") on update cascade on delete cascade;'
51 )
52
53 this.addSql(
54 'alter table if exists "reservation_item" add constraint "reservation_item_inventory_item_id_foreign" foreign key ("inventory_item_id") references "inventory_item" ("id") on update cascade on delete cascade;'
55 )
56 }
57
58 async down(): Promise<void> {
59 this.addSql(
60 'alter table if exists "inventory_level" drop constraint if exists "inventory_level_inventory_item_id_foreign";'
61 )
62
63 this.addSql(
64 'alter table if exists "reservation_item" drop constraint if exists "reservation_item_inventory_item_id_foreign";'
65 )
66
67 this.addSql('drop table if exists "inventory_item" cascade;')
68
69 this.addSql('drop table if exists "inventory_level" cascade;')
70
71 this.addSql('drop table if exists "reservation_item" cascade;')
72 }
73 }