|
1
|
import { Migration } from "@medusajs/framework/mikro-orm/migrations" |
|
2
|
|
|
3
|
export class Migration20240124154000 extends Migration { |
|
4
|
async up(): Promise<void> { |
|
5
|
// Customer table modifications |
|
6
|
this.addSql( |
|
7
|
'create table if not exists "customer" ("id" text not null, "company_name" text null, "first_name" text null, "last_name" text null, "email" text null, "phone" text null, "has_account" boolean not null default false, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "created_by" text null, constraint "customer_pkey" primary key ("id"));' |
|
8
|
) |
|
9
|
this.addSql('alter table "customer" alter column "email" drop not null;') |
|
10
|
this.addSql( |
|
11
|
'alter table "customer" add column if not exists "company_name" text null;' |
|
12
|
) |
|
13
|
this.addSql( |
|
14
|
'alter table "customer" add column if not exists "created_by" text null;' |
|
15
|
) |
|
16
|
this.addSql('drop index if exists "IDX_8abe81b9aac151ae60bf507ad1";') |
|
17
|
|
|
18
|
// Customer Address table |
|
19
|
this.addSql( |
|
20
|
'create table if not exists "customer_address" ("id" text not null, "customer_id" text not null, "address_name" text null, "is_default_shipping" boolean not null default false, "is_default_billing" boolean not null default false, "company" text null, "first_name" text null, "last_name" text null, "address_1" text null, "address_2" text null, "city" text null, "country_code" text null, "province" text null, "postal_code" text null, "phone" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), constraint "customer_address_pkey" primary key ("id"));' |
|
21
|
) |
|
22
|
this.addSql( |
|
23
|
'create index if not exists "IDX_customer_address_customer_id" on "customer_address" ("customer_id");' |
|
24
|
) |
|
25
|
this.addSql( |
|
26
|
'create unique index "IDX_customer_address_unique_customer_billing" on "customer_address" ("customer_id") where "is_default_billing" = true;' |
|
27
|
) |
|
28
|
this.addSql( |
|
29
|
'create unique index "IDX_customer_address_unique_customer_shipping" on "customer_address" ("customer_id") where "is_default_shipping" = true;' |
|
30
|
) |
|
31
|
|
|
32
|
// Customer Group table modifications |
|
33
|
this.addSql( |
|
34
|
'create table if not exists "customer_group" ("id" text not null, "name" text null, "metadata" jsonb null, "created_by" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "customer_group_pkey" primary key ("id"));' |
|
35
|
) |
|
36
|
this.addSql( |
|
37
|
'alter table "customer_group" add column if not exists "created_by" text null;' |
|
38
|
) |
|
39
|
this.addSql('drop index if exists "IDX_c4c3a5225a7a1f0af782c40abc";') |
|
40
|
this.addSql( |
|
41
|
'create unique index if not exists "IDX_customer_group_name" on "customer_group" ("name") where "deleted_at" is null;' |
|
42
|
) |
|
43
|
|
|
44
|
// Customer Group Customer table |
|
45
|
this.addSql( |
|
46
|
'create table if not exists "customer_group_customer" ("id" text not null, "customer_id" text not null, "customer_group_id" text not null, "metadata" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "created_by" text null, constraint "customer_group_customer_pkey" primary key ("id"));' |
|
47
|
) |
|
48
|
this.addSql( |
|
49
|
'create index if not exists "IDX_customer_group_customer_group_id" on "customer_group_customer" ("customer_group_id");' |
|
50
|
) |
|
51
|
this.addSql( |
|
52
|
'create index if not exists "IDX_customer_group_customer_customer_id" on "customer_group_customer" ("customer_id");' |
|
53
|
) |
|
54
|
|
|
55
|
// Foreign key constraints |
|
56
|
this.addSql( |
|
57
|
'alter table "customer" drop constraint if exists "FK_8abe81b9aac151ae60bf507ad15";' |
|
58
|
) |
|
59
|
this.addSql( |
|
60
|
'alter table "customer_address" add constraint "customer_address_customer_id_foreign" foreign key ("customer_id") references "customer" ("id") on update cascade on delete cascade;' |
|
61
|
) |
|
62
|
this.addSql( |
|
63
|
'alter table "customer_group_customer" add constraint "customer_group_customer_customer_group_id_foreign" foreign key ("customer_group_id") references "customer_group" ("id") on delete cascade;' |
|
64
|
) |
|
65
|
this.addSql( |
|
66
|
'alter table "customer_group_customer" add constraint "customer_group_customer_customer_id_foreign" foreign key ("customer_id") references "customer" ("id") on delete cascade;' |
|
67
|
) |
|
68
|
} |
|
69
|
} |