|
1
|
import { Migration } from "@mikro-orm/migrations" |
|
2
|
|
|
3
|
export class Migration20251022153442 extends Migration { |
|
4
|
override async up(): Promise<void> { |
|
5
|
this.addSql(` |
|
6
|
create table if not exists "product_product_option" ( |
|
7
|
"id" text not null, |
|
8
|
"product_id" text not null, |
|
9
|
"product_option_id" text not null, |
|
10
|
"created_at" timestamptz not null default now(), |
|
11
|
"updated_at" timestamptz not null default now(), |
|
12
|
"deleted_at" timestamptz null, |
|
13
|
constraint "product_product_option_pkey" primary key ("id") |
|
14
|
); |
|
15
|
`) |
|
16
|
|
|
17
|
this.addSql(` |
|
18
|
alter table if exists "product_product_option" |
|
19
|
add constraint "product_product_option_product_id_foreign" |
|
20
|
foreign key ("product_id") references "product" ("id") |
|
21
|
on update cascade on delete cascade; |
|
22
|
`) |
|
23
|
|
|
24
|
this.addSql(` |
|
25
|
alter table if exists "product_product_option" |
|
26
|
add constraint "product_product_option_product_option_id_foreign" |
|
27
|
foreign key ("product_option_id") references "product_option" ("id") |
|
28
|
on update cascade on delete cascade; |
|
29
|
`) |
|
30
|
|
|
31
|
this.addSql(` |
|
32
|
CREATE INDEX IF NOT EXISTS "IDX_product_product_option_product_id" |
|
33
|
ON "product_product_option" (product_id) WHERE deleted_at IS NULL; |
|
34
|
`) |
|
35
|
|
|
36
|
this.addSql(` |
|
37
|
CREATE INDEX IF NOT EXISTS "IDX_product_product_option_product_option_id" |
|
38
|
ON "product_product_option" (product_option_id) WHERE deleted_at IS NULL; |
|
39
|
`) |
|
40
|
|
|
41
|
this.addSql(` |
|
42
|
CREATE INDEX IF NOT EXISTS "IDX_product_product_option_deleted_at" |
|
43
|
ON "product_product_option" (deleted_at) WHERE deleted_at IS NULL; |
|
44
|
`) |
|
45
|
|
|
46
|
this.addSql(` |
|
47
|
alter table if exists "product_option" |
|
48
|
add column if not exists "is_exclusive" boolean not null default false; |
|
49
|
`) |
|
50
|
|
|
51
|
this.addSql(` |
|
52
|
insert into "product_product_option" ("id", "product_id", "product_option_id") |
|
53
|
select gen_random_uuid(), "product_id", "id" |
|
54
|
from "product_option" |
|
55
|
where "product_id" is not null; |
|
56
|
`) |
|
57
|
|
|
58
|
this.addSql(` |
|
59
|
update "product_option" set "is_exclusive" = true |
|
60
|
where "product_id" is not null; |
|
61
|
`) |
|
62
|
|
|
63
|
this.addSql( |
|
64
|
`alter table if exists "product_option" drop constraint if exists "product_option_product_id_foreign";` |
|
65
|
) |
|
66
|
this.addSql(`drop index if exists "IDX_product_option_product_id";`) |
|
67
|
this.addSql(`drop index if exists "IDX_option_product_id_title_unique";`) |
|
68
|
this.addSql( |
|
69
|
`alter table if exists "product_option" drop column if exists "product_id";` |
|
70
|
) |
|
71
|
} |
|
72
|
|
|
73
|
override async down(): Promise<void> { |
|
74
|
// Recreate product_id column before removing the pivot |
|
75
|
this.addSql(` |
|
76
|
alter table if exists "product_option" |
|
77
|
add column if not exists "product_id" text; |
|
78
|
`) |
|
79
|
|
|
80
|
// Migrate data back from join table |
|
81
|
this.addSql(` |
|
82
|
update "product_option" po |
|
83
|
set "product_id" = ppo."product_id" |
|
84
|
from "product_product_option" ppo |
|
85
|
where po."id" = ppo."product_option_id" |
|
86
|
and ppo."deleted_at" is null; |
|
87
|
`) |
|
88
|
|
|
89
|
// Make product_id NOT NULL |
|
90
|
this.addSql(` |
|
91
|
alter table if exists "product_option" |
|
92
|
alter column "product_id" set not null; |
|
93
|
`) |
|
94
|
|
|
95
|
// Re-add foreign key and indexes |
|
96
|
this.addSql(` |
|
97
|
alter table if exists "product_option" |
|
98
|
add constraint "product_option_product_id_foreign" |
|
99
|
foreign key ("product_id") references "product" ("id") |
|
100
|
on update cascade on delete cascade; |
|
101
|
`) |
|
102
|
|
|
103
|
this.addSql(` |
|
104
|
CREATE INDEX IF NOT EXISTS "IDX_product_option_product_id" |
|
105
|
ON "product_option" (product_id) WHERE deleted_at IS NULL; |
|
106
|
`) |
|
107
|
|
|
108
|
this.addSql(` |
|
109
|
CREATE UNIQUE INDEX IF NOT EXISTS "IDX_option_product_id_title_unique" |
|
110
|
ON "product_option" (product_id, title) WHERE deleted_at IS NULL; |
|
111
|
`) |
|
112
|
|
|
113
|
// Drop the join table |
|
114
|
this.addSql(`drop table if exists "product_product_option" cascade;`) |
|
115
|
|
|
116
|
// Drop is_exclusive column |
|
117
|
this.addSql(` |
|
118
|
alter table if exists "product_option" drop column if exists "is_exclusive"; |
|
119
|
`) |
|
120
|
} |
|
121
|
} |