1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20240227120221 extends Migration {
4 async up(): Promise<void> {
5 this.addSql(
6 'create table if not exists "promotion_campaign" ("id" text not null, "name" text not null, "description" text null, "campaign_identifier" text not null, "starts_at" timestamptz null, "ends_at" timestamptz null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_campaign_pkey" primary key ("id"));'
7 )
8 this.addSql(
9 'alter table if exists "promotion_campaign" add constraint "IDX_campaign_identifier_unique" unique ("campaign_identifier");'
10 )
11
12 this.addSql(
13 'create table if not exists "promotion_campaign_budget" ("id" text not null, "type" text check ("type" in (\'spend\', \'usage\')) not null, "campaign_id" text not null, "limit" numeric null, "raw_limit" jsonb null, "used" numeric not null default 0, "raw_used" jsonb not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_campaign_budget_pkey" primary key ("id"));'
14 )
15 this.addSql(
16 'create index if not exists "IDX_campaign_budget_type" on "promotion_campaign_budget" ("type");'
17 )
18 this.addSql(
19 'alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_campaign_id_unique" unique ("campaign_id");'
20 )
21
22 this.addSql(
23 'create table if not exists "promotion" ("id" text not null, "code" text not null, "campaign_id" text null, "is_automatic" boolean not null default false, "type" text check ("type" in (\'standard\', \'buyget\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_pkey" primary key ("id"));'
24 )
25 this.addSql(
26 'create index if not exists "IDX_promotion_code" on "promotion" ("code");'
27 )
28 this.addSql(
29 'create index if not exists "IDX_promotion_type" on "promotion" ("type");'
30 )
31 this.addSql(
32 'alter table if exists "promotion" add constraint "IDX_promotion_code_unique" unique ("code");'
33 )
34
35 this.addSql(
36 'create table if not exists "promotion_application_method" ("id" text not null, "value" numeric null, "raw_value" jsonb null, "max_quantity" numeric null, "apply_to_quantity" numeric null, "buy_rules_min_quantity" numeric null, "type" text check ("type" in (\'fixed\', \'percentage\')) not null, "target_type" text check ("target_type" in (\'order\', \'shipping_methods\', \'items\')) not null, "allocation" text check ("allocation" in (\'each\', \'across\')) null, "promotion_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_application_method_pkey" primary key ("id"));'
37 )
38 this.addSql(
39 'create index if not exists "IDX_application_method_type" on "promotion_application_method" ("type");'
40 )
41 this.addSql(
42 'create index if not exists "IDX_application_method_target_type" on "promotion_application_method" ("target_type");'
43 )
44 this.addSql(
45 'create index if not exists "IDX_application_method_allocation" on "promotion_application_method" ("allocation");'
46 )
47 this.addSql(
48 'alter table if exists "promotion_application_method" add constraint "promotion_application_method_promotion_id_unique" unique ("promotion_id");'
49 )
50
51 this.addSql(
52 'create table if not exists "promotion_rule" ("id" text not null, "description" text null, "attribute" text not null, "operator" text check ("operator" in (\'gte\', \'lte\', \'gt\', \'lt\', \'eq\', \'ne\', \'in\')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_pkey" primary key ("id"));'
53 )
54 this.addSql(
55 'create index if not exists "IDX_promotion_rule_attribute" on "promotion_rule" ("attribute");'
56 )
57 this.addSql(
58 'create index if not exists "IDX_promotion_rule_operator" on "promotion_rule" ("operator");'
59 )
60
61 this.addSql(
62 'create table if not exists "promotion_promotion_rule" ("promotion_id" text not null, "promotion_rule_id" text not null, constraint "promotion_promotion_rule_pkey" primary key ("promotion_id", "promotion_rule_id"));'
63 )
64
65 this.addSql(
66 'create table if not exists "application_method_target_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_target_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));'
67 )
68
69 this.addSql(
70 'create table if not exists "application_method_buy_rules" ("application_method_id" text not null, "promotion_rule_id" text not null, constraint "application_method_buy_rules_pkey" primary key ("application_method_id", "promotion_rule_id"));'
71 )
72
73 this.addSql(
74 'create table if not exists "promotion_rule_value" ("id" text not null, "promotion_rule_id" text not null, "value" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_value_pkey" primary key ("id"));'
75 )
76 this.addSql(
77 'create index if not exists "IDX_promotion_rule_promotion_rule_value_id" on "promotion_rule_value" ("promotion_rule_id");'
78 )
79
80 this.addSql(
81 'alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on update cascade;'
82 )
83
84 this.addSql(
85 'alter table if exists "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on delete set null;'
86 )
87
88 this.addSql(
89 'alter table if exists "promotion_application_method" add constraint "promotion_application_method_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;'
90 )
91
92 this.addSql(
93 'alter table if exists "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;'
94 )
95 this.addSql(
96 'alter table if exists "promotion_promotion_rule" add constraint "promotion_promotion_rule_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;'
97 )
98
99 this.addSql(
100 'alter table if exists "application_method_target_rules" add constraint "application_method_target_rules_application_method_id_foreign" foreign key ("application_method_id") references "promotion_application_method" ("id") on update cascade on delete cascade;'
101 )
102 this.addSql(
103 'alter table if exists "application_method_target_rules" add constraint "application_method_target_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;'
104 )
105
106 this.addSql(
107 'alter table if exists "application_method_buy_rules" add constraint "application_method_buy_rules_application_method_id_foreign" foreign key ("application_method_id") references "promotion_application_method" ("id") on update cascade on delete cascade;'
108 )
109 this.addSql(
110 'alter table if exists "application_method_buy_rules" add constraint "application_method_buy_rules_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;'
111 )
112
113 this.addSql(
114 'alter table if exists "promotion_rule_value" add constraint "promotion_rule_value_promotion_rule_id_foreign" foreign key ("promotion_rule_id") references "promotion_rule" ("id") on update cascade on delete cascade;'
115 )
116
117 this.addSql(
118 'alter table if exists "promotion" drop constraint if exists "promotion_campaign_id_foreign";'
119 )
120
121 this.addSql(
122 'alter table if exists "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "promotion_campaign" ("id") on update cascade on delete set null;'
123 )
124
125 this.addSql(
126 'alter table if exists "promotion_application_method" add column if not exists "currency_code" text not null;'
127 )
128
129 this.addSql(
130 'CREATE INDEX IF NOT EXISTS "IDX_promotion_application_method_currency_code" ON "promotion_application_method" (currency_code) WHERE deleted_at IS NOT NULL;'
131 )
132
133 this.addSql(
134 'alter table "promotion_application_method" alter column "value" type numeric using ("value"::numeric);'
135 )
136
137 this.addSql(
138 'alter table "promotion_application_method" alter column "raw_value" type jsonb using ("raw_value"::jsonb);'
139 )
140
141 this.addSql(
142 'alter table "promotion_application_method" alter column "raw_value" set not null;'
143 )
144
145 this.addSql(
146 'alter table if exists "promotion_campaign_budget" add column if not exists "currency_code" text null;'
147 )
148 }
149 }