1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20240626133555 extends Migration {
4 async up(): Promise<void> {
5 this.addSql(
6 'alter table if exists "price_list_rule" add column if not exists "value" jsonb;'
7 )
8
9 // TODO: Added on 28.06.2024, Drop defaults after a while.
10 this.addSql(
11 'alter table if exists "price_list_rule" add column if not exists "attribute" text not null DEFAULT \'\';'
12 )
13
14 this.addSql(
15 'alter table if exists "price_rule" add column if not exists "attribute" text not null DEFAULT \'\';'
16 )
17
18 /* DATA MIGRATION */
19 this.addSql(
20 "update price_rule set attribute = (SELECT rule_attribute FROM rule_type WHERE rule_type.id = price_rule.rule_type_id);"
21 )
22 this.addSql(
23 "update price_list_rule set value = (SELECT array_to_json(ARRAY(SELECT value FROM price_list_rule_value WHERE price_list_rule_value.price_list_rule_id = price_list_rule.id))::jsonb);"
24 )
25 this.addSql(
26 "update price_list_rule set attribute = (SELECT rule_attribute FROM rule_type WHERE rule_type.id = price_list_rule.rule_type_id);"
27 )
28 /* DATA MIGRATION END */
29
30 this.addSql(
31 'alter table if exists "price_set_rule_type" drop constraint if exists "price_set_rule_type_rule_type_id_foreign";'
32 )
33
34 this.addSql(
35 'alter table if exists "price_rule" drop constraint if exists "price_rule_rule_type_id_foreign";'
36 )
37
38 this.addSql(
39 'alter table if exists "price_list_rule" drop constraint if exists "price_list_rule_rule_type_id_foreign";'
40 )
41
42 this.addSql(
43 'alter table if exists "price_rule" drop constraint if exists "price_rule_price_set_id_foreign";'
44 )
45
46 this.addSql(
47 'drop index if exists "IDX_price_list_rule_rule_type_id_unique";'
48 )
49 this.addSql('drop index if exists "IDX_price_rule_price_set_id";')
50 this.addSql('drop index if exists "IDX_price_rule_rule_type_id";')
51 this.addSql('drop index if exists "IDX_price_rule_price_id_unique";')
52 this.addSql(
53 'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_price_rule_price_id_attribute_unique" ON "price_rule" (price_id, attribute) WHERE deleted_at IS NULL;'
54 )
55
56 this.addSql(
57 'alter table if exists "price_rule" drop column if exists "price_set_id";'
58 )
59 this.addSql(
60 'alter table if exists "price_rule" drop column if exists "rule_type_id";'
61 )
62 this.addSql(
63 'alter table if exists "price_list_rule" drop column if exists "rule_type_id";'
64 )
65
66 this.addSql('drop table if exists "rule_type" cascade;')
67
68 this.addSql('drop table if exists "price_set_rule_type" cascade;')
69
70 this.addSql('drop table if exists "price_list_rule_value" cascade;')
71 }
72 }