1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20250909083125 extends Migration {
4 override async up(): Promise<void> {
5 this.addSql(
6 `alter table if exists "promotion_campaign_budget_usage" drop constraint if exists "promotion_campaign_budget_usage_attribute_value_budget_id_unique";`
7 )
8 this.addSql(
9 `create table if not exists "promotion_campaign_budget_usage" ("id" text not null, "attribute_value" text not null, "used" numeric not null default 0, "budget_id" text not null, "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_usage_pkey" primary key ("id"));`
10 )
11 this.addSql(
12 `CREATE INDEX IF NOT EXISTS "IDX_promotion_campaign_budget_usage_budget_id" ON "promotion_campaign_budget_usage" (budget_id) WHERE deleted_at IS NULL;`
13 )
14 this.addSql(
15 `CREATE INDEX IF NOT EXISTS "IDX_promotion_campaign_budget_usage_deleted_at" ON "promotion_campaign_budget_usage" (deleted_at) WHERE deleted_at IS NULL;`
16 )
17 this.addSql(
18 `CREATE UNIQUE INDEX IF NOT EXISTS "IDX_promotion_campaign_budget_usage_attribute_value_budget_id_unique" ON "promotion_campaign_budget_usage" (attribute_value, budget_id) WHERE deleted_at IS NULL;`
19 )
20
21 this.addSql(
22 `alter table if exists "promotion_campaign_budget_usage" add constraint "promotion_campaign_budget_usage_budget_id_foreign" foreign key ("budget_id") references "promotion_campaign_budget" ("id") on update cascade on delete cascade;`
23 )
24
25 this.addSql(
26 `alter table if exists "promotion_campaign_budget" drop constraint if exists "promotion_campaign_budget_type_check";`
27 )
28
29 this.addSql(
30 `alter table if exists "promotion_campaign_budget" add column if not exists "attribute" text null;`
31 )
32 this.addSql(
33 `alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_type_check" check("type" in ('spend', 'usage', 'use_by_attribute', 'spend_by_attribute'));`
34 )
35 }
36
37 override async down(): Promise<void> {
38 this.addSql(
39 `drop table if exists "promotion_campaign_budget_usage" cascade;`
40 )
41
42 this.addSql(
43 `alter table if exists "promotion_campaign_budget" drop constraint if exists "promotion_campaign_budget_type_check";`
44 )
45
46 this.addSql(
47 `alter table if exists "promotion_campaign_budget" drop column if exists "attribute";`
48 )
49
50 this.addSql(
51 `alter table if exists "promotion_campaign_budget" add constraint "promotion_campaign_budget_type_check" check("type" in ('spend', 'usage'));`
52 )
53 }
54 }