|
1
|
import { Migration } from "@medusajs/framework/mikro-orm/migrations" |
|
2
|
|
|
3
|
export class Migration20251016182939 extends Migration { |
|
4
|
override async up(): Promise<void> { |
|
5
|
// Step 1: Add the column |
|
6
|
this.addSql(` |
|
7
|
alter table if exists "order_credit_line" |
|
8
|
add column if not exists "version" integer default null; |
|
9
|
`) |
|
10
|
|
|
11
|
// Step 2: Populate the version column from the related order table |
|
12
|
this.addSql(` |
|
13
|
update "order_credit_line" ocl |
|
14
|
set "version" = o."version" |
|
15
|
from "order" o |
|
16
|
where ocl."order_id" = o."id"; |
|
17
|
`) |
|
18
|
|
|
19
|
// Step 3: Set NOT NULL and default AFTER backfilling |
|
20
|
this.addSql(` |
|
21
|
alter table if exists "order_credit_line" |
|
22
|
alter column "version" set not null, |
|
23
|
alter column "version" set default 1; |
|
24
|
`) |
|
25
|
|
|
26
|
// Step 4: Add index for performance |
|
27
|
this.addSql(` |
|
28
|
create index if not exists "IDX_order_credit_line_order_id_version" |
|
29
|
on "order_credit_line" (order_id, version) |
|
30
|
where deleted_at is null; |
|
31
|
`) |
|
32
|
} |
|
33
|
|
|
34
|
override async down(): Promise<void> { |
|
35
|
this.addSql( |
|
36
|
`drop index if exists "IDX_order_credit_line_order_id_version";` |
|
37
|
) |
|
38
|
this.addSql( |
|
39
|
`alter table if exists "order_credit_line" drop column if exists "version";` |
|
40
|
) |
|
41
|
} |
|
42
|
} |