1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20241125090957 extends Migration {
4 async up(): Promise<void> {
5 this.addSql(
6 'alter table if exists "product" drop constraint if exists "product_status_check";'
7 )
8
9 this.addSql(
10 'CREATE INDEX IF NOT EXISTS "IDX_product_category_parent_category_id" ON "product_category" (parent_category_id) WHERE deleted_at IS NULL;'
11 )
12 this.addSql(
13 'CREATE INDEX IF NOT EXISTS "IDX_product_category_path" ON "product_category" (mpath) WHERE deleted_at IS NULL;'
14 )
15 this.addSql(
16 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_category_handle_unique" ON "product_category" (handle) WHERE deleted_at IS NULL;'
17 )
18
19 this.addSql(
20 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_collection_handle_unique" ON "product_collection" (handle) WHERE deleted_at IS NULL;'
21 )
22
23 this.addSql('drop index if exists "IDX_product_image_deleted_at";')
24 this.addSql(
25 'CREATE INDEX IF NOT EXISTS "IDX_image_deleted_at" ON "image" (deleted_at) WHERE deleted_at IS NULL;'
26 )
27 this.addSql(
28 'CREATE INDEX IF NOT EXISTS "IDX_product_image_url" ON "image" (url) WHERE deleted_at IS NULL;'
29 )
30
31 this.addSql(
32 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_tag_value_unique" ON "product_tag" (value) WHERE deleted_at IS NULL;'
33 )
34
35 this.addSql(
36 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_type_value_unique" ON "product_type" (value) WHERE deleted_at IS NULL;'
37 )
38
39 this.addSql(
40 'alter table if exists "product" alter column "status" type text using ("status"::text);'
41 )
42 this.addSql(
43 "alter table if exists \"product\" add constraint \"product_status_check\" check (\"status\" in ('draft', 'proposed', 'published', 'rejected'));"
44 )
45 this.addSql(
46 'alter table if exists "product" alter column "status" set default \'draft\';'
47 )
48 this.addSql(
49 'CREATE INDEX IF NOT EXISTS "IDX_product_type_id" ON "product" (type_id) WHERE deleted_at IS NULL;'
50 )
51 this.addSql(
52 'CREATE INDEX IF NOT EXISTS "IDX_product_collection_id" ON "product" (collection_id) WHERE deleted_at IS NULL;'
53 )
54 this.addSql(
55 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_product_handle_unique" ON "product" (handle) WHERE deleted_at IS NULL;'
56 )
57
58 this.addSql(
59 'alter table if exists "product_option" alter column "product_id" type text using ("product_id"::text);'
60 )
61 this.addSql(
62 'alter table if exists "product_option" alter column "product_id" set not null;'
63 )
64 this.addSql(
65 'CREATE INDEX IF NOT EXISTS "IDX_product_option_product_id" ON "product_option" (product_id) WHERE deleted_at IS NULL;'
66 )
67 this.addSql(
68 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_option_product_id_title_unique" ON "product_option" (product_id, title) WHERE deleted_at IS NULL;'
69 )
70
71 this.addSql(
72 'CREATE INDEX IF NOT EXISTS "IDX_product_option_value_option_id" ON "product_option_value" (option_id) WHERE deleted_at IS NULL;'
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
78 this.addSql(
79 'alter table if exists "product_variant" alter column "weight" type integer using ("weight"::integer);'
80 )
81 this.addSql(
82 'alter table if exists "product_variant" alter column "length" type integer using ("length"::integer);'
83 )
84 this.addSql(
85 'alter table if exists "product_variant" alter column "height" type integer using ("height"::integer);'
86 )
87 this.addSql(
88 'alter table if exists "product_variant" alter column "width" type integer using ("width"::integer);'
89 )
90 this.addSql(
91 'CREATE INDEX IF NOT EXISTS "IDX_product_variant_product_id" ON "product_variant" (product_id) WHERE deleted_at IS NULL;'
92 )
93 this.addSql(
94 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_product_variant_sku_unique" ON "product_variant" (sku) WHERE deleted_at IS NULL;'
95 )
96 this.addSql(
97 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_product_variant_barcode_unique" ON "product_variant" (barcode) WHERE deleted_at IS NULL;'
98 )
99 this.addSql(
100 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_product_variant_ean_unique" ON "product_variant" (ean) WHERE deleted_at IS NULL;'
101 )
102 this.addSql(
103 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_product_variant_upc_unique" ON "product_variant" (upc) WHERE deleted_at IS NULL;'
104 )
105 }
106
107 async down(): Promise<void> {
108 this.addSql(
109 'alter table if exists "product" drop constraint if exists "product_status_check";'
110 )
111
112 this.addSql(
113 'drop index if exists "IDX_product_category_parent_category_id";'
114 )
115 this.addSql('drop index if exists "IDX_product_category_path";')
116 this.addSql('drop index if exists "IDX_category_handle_unique";')
117
118 this.addSql('drop index if exists "IDX_collection_handle_unique";')
119
120 this.addSql('drop index if exists "IDX_image_deleted_at";')
121 this.addSql('drop index if exists "IDX_product_image_url";')
122 this.addSql(
123 'create index if not exists "IDX_product_image_deleted_at" on "image" ("deleted_at");'
124 )
125
126 this.addSql('drop index if exists "IDX_tag_value_unique";')
127
128 this.addSql('drop index if exists "IDX_type_value_unique";')
129
130 this.addSql(
131 'alter table if exists "product" alter column "status" drop default;'
132 )
133 this.addSql(
134 'alter table if exists "product" alter column "status" type text using ("status"::text);'
135 )
136 this.addSql(
137 "alter table if exists \"product\" add constraint \"product_status_check\" check (\"status\" in ('draft', 'proposed', 'published', 'rejected'));"
138 )
139 this.addSql('drop index if exists "IDX_product_type_id";')
140 this.addSql('drop index if exists "IDX_product_collection_id";')
141 this.addSql('drop index if exists "IDX_product_handle_unique";')
142
143 this.addSql(
144 'alter table if exists "product_option" alter column "product_id" type text using ("product_id"::text);'
145 )
146 this.addSql(
147 'alter table if exists "product_option" alter column "product_id" drop not null;'
148 )
149 this.addSql('drop index if exists "IDX_product_option_product_id";')
150 this.addSql('drop index if exists "IDX_option_product_id_title_unique";')
151
152 this.addSql('drop index if exists "IDX_product_option_value_option_id";')
153 this.addSql('drop index if exists "IDX_option_value_option_id_unique";')
154
155 this.addSql(
156 'alter table if exists "product_variant" alter column "weight" type numeric using ("weight"::numeric);'
157 )
158 this.addSql(
159 'alter table if exists "product_variant" alter column "length" type numeric using ("length"::numeric);'
160 )
161 this.addSql(
162 'alter table if exists "product_variant" alter column "height" type numeric using ("height"::numeric);'
163 )
164 this.addSql(
165 'alter table if exists "product_variant" alter column "width" type numeric using ("width"::numeric);'
166 )
167 this.addSql('drop index if exists "IDX_product_variant_product_id";')
168 this.addSql('drop index if exists "IDX_product_variant_sku_unique";')
169 this.addSql('drop index if exists "IDX_product_variant_barcode_unique";')
170 this.addSql('drop index if exists "IDX_product_variant_ean_unique";')
171 this.addSql('drop index if exists "IDX_product_variant_upc_unique";')
172 }
173 }