1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class InitialSetup20240315083440 extends Migration {
4 async up(): Promise<void> {
5 const productTables = await this.execute(
6 "select * from information_schema.tables where table_name = 'product' and table_schema = 'public'"
7 )
8
9 if (productTables.length > 0) {
10 // This is so we can still run the api tests, remove completely once that is not needed
11 this.addSql(
12 `alter table "product_option_value" alter column "variant_id" drop not null;`
13 )
14 this.addSql(
15 `alter table "product_variant" drop column "inventory_quantity";`
16 )
17 this.addSql(
18 `alter table "product_category" add column "deleted_at" timestamptz null;`
19 )
20 }
21
22 /* --- ENTITY TABLES AND INDICES --- */
23 this.addSql(
24 'create table if not exists "product" ("id" text not null, "title" text not null, "handle" text not null, "subtitle" text null, "description" text null, "is_giftcard" boolean not null default false, "status" text check ("status" in (\'draft\', \'proposed\', \'published\', \'rejected\')) not null, "thumbnail" text null, "weight" text null, "length" text null, "height" text null, "width" text null, "origin_country" text null, "hs_code" text null, "mid_code" text null, "material" text null, "collection_id" text null, "type_id" text null, "discountable" boolean not null default true, "external_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "metadata" jsonb null, constraint "product_pkey" primary key ("id"));'
25 )
26 this.addSql(
27 'create unique index if not exists "IDX_product_handle_unique" on "product" (handle) where deleted_at is null;'
28 )
29 this.addSql(
30 'create index if not exists "IDX_product_type_id" on "product" ("type_id") where deleted_at is null;'
31 )
32 this.addSql(
33 'create index if not exists "IDX_product_collection_id" on "product" ("collection_id") where deleted_at is null;'
34 )
35 this.addSql(
36 'create index if not exists "IDX_product_deleted_at" on "product" ("deleted_at");'
37 )
38
39 this.addSql(
40 'create table if not exists "product_variant" ("id" text not null, "title" text not null, "sku" text null, "barcode" text null, "ean" text null, "upc" text null, "allow_backorder" boolean not null default false, "manage_inventory" boolean not null default true, "hs_code" text null, "origin_country" text null, "mid_code" text null, "material" text null, "weight" numeric null, "length" numeric null, "height" numeric null, "width" numeric null, "metadata" jsonb null, "variant_rank" numeric null default 0, "product_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_variant_pkey" primary key ("id"));'
41 )
42 this.addSql(
43 'create unique index if not exists "IDX_product_variant_ean_unique" on "product_variant" (ean) where deleted_at is null;'
44 )
45 this.addSql(
46 'create unique index if not exists "IDX_product_variant_upc_unique" on "product_variant" (upc) where deleted_at is null;'
47 )
48 this.addSql(
49 'create unique index if not exists "IDX_product_variant_sku_unique" on "product_variant" (sku) where deleted_at is null;'
50 )
51 this.addSql(
52 'create unique index if not exists "IDX_product_variant_barcode_unique" on "product_variant" (barcode) where deleted_at is null;'
53 )
54 this.addSql(
55 'create index if not exists "IDX_product_variant_product_id" on "product_variant" ("product_id") where deleted_at is null;'
56 )
57 this.addSql(
58 'create index if not exists "IDX_product_variant_deleted_at" on "product_variant" ("deleted_at");'
59 )
60
61 this.addSql(
62 'create table if not exists "product_option" ("id" text not null, "title" text not null, "product_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_option_pkey" primary key ("id"));'
63 )
64 this.addSql(
65 'create unique index if not exists "IDX_option_product_id_title_unique" on "product_option" (product_id, title) where deleted_at is null;'
66 )
67 this.addSql(
68 'create index if not exists "IDX_product_option_deleted_at" on "product_option" ("deleted_at");'
69 )
70
71 this.addSql(
72 'create table if not exists "product_option_value" ("id" text not null, "value" text not null, "option_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_option_value_pkey" primary key ("id"));'
73 )
74 this.addSql(
75 'create unique index if not exists "IDX_option_value_option_id_unique" on "product_option_value" (option_id, value) where deleted_at is null;'
76 )
77 this.addSql(
78 'create index if not exists "IDX_product_option_value_deleted_at" on "product_option_value" ("deleted_at");'
79 )
80
81 this.addSql(
82 'create table if not exists "image" ("id" text not null, "url" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "image_pkey" primary key ("id"));'
83 )
84 this.addSql(
85 'create index if not exists "IDX_product_image_url" on "image" ("url") where deleted_at is null;'
86 )
87 this.addSql(
88 'create index if not exists "IDX_product_image_deleted_at" on "image" ("deleted_at");'
89 )
90
91 this.addSql(
92 'create table if not exists "product_tag" ("id" text not null, "value" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_tag_pkey" primary key ("id"));'
93 )
94
95 this.addSql(
96 'create unique index if not exists "IDX_tag_value_unique" on "product_tag" (value) where deleted_at is null;'
97 )
98 this.addSql(
99 'create index if not exists "IDX_product_tag_deleted_at" on "product_tag" ("deleted_at");'
100 )
101
102 this.addSql(
103 'create table if not exists "product_type" ("id" text not null, "value" text not null, "metadata" json null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_type_pkey" primary key ("id"));'
104 )
105 this.addSql(
106 'create unique index if not exists "IDX_type_value_unique" on "product_type" (value) where deleted_at is null;'
107 )
108 this.addSql(
109 'create index if not exists "IDX_product_type_deleted_at" on "product_type" ("deleted_at");'
110 )
111
112 this.addSql(
113 'create table if not exists "product_collection" ("id" text not null, "title" text not null, "handle" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_collection_pkey" primary key ("id"));'
114 )
115 this.addSql(
116 'create unique index if not exists "IDX_collection_handle_unique" on "product_collection" (handle) where deleted_at is null;'
117 )
118 this.addSql(
119 'create index if not exists "IDX_product_collection_deleted_at" on "product_collection" ("deleted_at");'
120 )
121
122 this.addSql(
123 'create table if not exists "product_category" ("id" text not null, "name" text not null, "description" text not null default \'\', "handle" text not null, "mpath" text not null, "is_active" boolean not null default false, "is_internal" boolean not null default false, "rank" numeric not null default 0, "parent_category_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "product_category_pkey" primary key ("id"));'
124 )
125 this.addSql(
126 'create unique index if not exists "IDX_category_handle_unique" on "product_category" (handle) where deleted_at is null;'
127 )
128 this.addSql(
129 'create index if not exists "IDX_product_category_path" on "product_category" ("mpath") where deleted_at is null;'
130 )
131 this.addSql(
132 'create index if not exists "IDX_product_category_deleted_at" on "product_collection" ("deleted_at");'
133 )
134 // TODO: Batch updating composite unique index in MikroORM is faulty. Should be added back when issue has been resolved.
135 this.addSql(`drop index if exists "UniqProductCategoryParentIdRank";`)
136
137 /* --- PIVOT TABLES --- */
138 this.addSql(
139 'create table if not exists "product_tags" ("product_id" text not null, "product_tag_id" text not null, constraint "product_tags_pkey" primary key ("product_id", "product_tag_id"));'
140 )
141 this.addSql(
142 'create table if not exists "product_images" ("product_id" text not null, "image_id" text not null, constraint "product_images_pkey" primary key ("product_id", "image_id"));'
143 )
144 this.addSql(
145 'create table if not exists "product_category_product" ("product_id" text not null, "product_category_id" text not null, constraint "product_category_product_pkey" primary key ("product_id", "product_category_id"));'
146 )
147 this.addSql(
148 'create table if not exists "product_variant_option" ("variant_id" text not null, "option_value_id" text not null, constraint "product_variant_option_pkey" primary key ("variant_id", "option_value_id"));'
149 )
150
151 /* --- FOREIGN KEYS AND CONSTRAINTS --- */
152 this.addSql(
153 'alter table if exists "product" add constraint "product_collection_id_foreign" foreign key ("collection_id") references "product_collection" ("id") on update cascade on delete set null;'
154 )
155 this.addSql(
156 'alter table if exists "product" add constraint "product_type_id_foreign" foreign key ("type_id") references "product_type" ("id") on update cascade on delete set null;'
157 )
158
159 this.addSql(
160 'alter table if exists "product_variant" add constraint "product_variant_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
161 )
162
163 this.addSql(
164 'alter table if exists "product_option" add constraint "product_option_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
165 )
166
167 this.addSql(
168 'alter table if exists "product_option_value" add constraint "product_option_value_option_id_foreign" foreign key ("option_id") references "product_option" ("id") on update cascade on delete cascade;'
169 )
170
171 this.addSql(
172 'alter table if exists "product_variant_option" add constraint "product_variant_option_variant_id_foreign" foreign key ("variant_id") references "product_variant" ("id") on update cascade on delete cascade;'
173 )
174 this.addSql(
175 'alter table if exists "product_variant_option" add constraint "product_variant_option_option_value_id_foreign" foreign key ("option_value_id") references "product_option_value" ("id") on update cascade on delete cascade;'
176 )
177
178 this.addSql(
179 'alter table if exists "product_images" add constraint "product_images_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
180 )
181 this.addSql(
182 'alter table if exists "product_images" add constraint "product_images_image_id_foreign" foreign key ("image_id") references "image" ("id") on update cascade on delete cascade;'
183 )
184
185 this.addSql(
186 'alter table if exists "product_tags" add constraint "product_tags_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
187 )
188 this.addSql(
189 'alter table if exists "product_tags" add constraint "product_tags_product_tag_id_foreign" foreign key ("product_tag_id") references "product_tag" ("id") on update cascade on delete cascade;'
190 )
191
192 this.addSql(
193 'alter table if exists "product_category_product" add constraint "product_category_product_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;'
194 )
195 this.addSql(
196 'alter table if exists "product_category_product" add constraint "product_category_product_product_category_id_foreign" foreign key ("product_category_id") references "product_category" ("id") on update cascade on delete cascade;'
197 )
198
199 this.addSql(
200 'alter table if exists "product_category" add constraint "product_category_parent_category_id_foreign" foreign key ("parent_category_id") references "product_category" ("id") on update cascade on delete cascade;'
201 )
202 }
203 }