|
1
|
import { PlannerActionLinkDescriptor } from "@medusajs/framework/types" |
|
2
|
import { MikroORM } from "@medusajs/framework/mikro-orm/core" |
|
3
|
import { PostgreSqlDriver } from "@medusajs/framework/mikro-orm/postgresql" |
|
4
|
import { MigrationsExecutionPlanner } from "../index" |
|
5
|
|
|
6
|
class TestablePlanner extends MigrationsExecutionPlanner { |
|
7
|
async testRenameOldTable( |
|
8
|
orm: MikroORM<PostgreSqlDriver>, |
|
9
|
oldName: string, |
|
10
|
newName: string, |
|
11
|
descriptor: PlannerActionLinkDescriptor |
|
12
|
) { |
|
13
|
return this.renameOldTable(orm, oldName, newName, descriptor) |
|
14
|
} |
|
15
|
} |
|
16
|
|
|
17
|
describe("MigrationsExecutionPlanner.renameOldTable", () => { |
|
18
|
let planner: TestablePlanner |
|
19
|
let capturedSQL: string |
|
20
|
|
|
21
|
beforeEach(() => { |
|
22
|
capturedSQL = "" |
|
23
|
planner = new TestablePlanner([], { |
|
24
|
database: { clientUrl: "postgres://localhost:5432/test_db" }, |
|
25
|
}) |
|
26
|
}) |
|
27
|
|
|
28
|
function makeMockOrm(): MikroORM<PostgreSqlDriver> { |
|
29
|
return { |
|
30
|
em: { |
|
31
|
getDriver: () => ({ |
|
32
|
getConnection: () => ({ |
|
33
|
execute: async (sql: string) => { |
|
34
|
capturedSQL = sql |
|
35
|
}, |
|
36
|
}), |
|
37
|
}), |
|
38
|
}, |
|
39
|
} as unknown as MikroORM<PostgreSqlDriver> |
|
40
|
} |
|
41
|
|
|
42
|
const descriptor: PlannerActionLinkDescriptor = { |
|
43
|
fromModule: "order", |
|
44
|
toModule: "payout", |
|
45
|
fromModel: "order", |
|
46
|
toModel: "payout", |
|
47
|
} |
|
48
|
|
|
49
|
it("should use an unqualified identifier as the RENAME TO target", async () => { |
|
50
|
await planner.testRenameOldTable( |
|
51
|
makeMockOrm(), |
|
52
|
"order_payout", |
|
53
|
"order_order_payout_payout", |
|
54
|
descriptor |
|
55
|
) |
|
56
|
|
|
57
|
// PostgreSQL grammar requires an unqualified name after RENAME TO. |
|
58
|
// A schema-qualified target ("public"."new_name") causes ERROR 42601. |
|
59
|
expect(capturedSQL).toMatch(/RENAME TO "order_order_payout_payout"/) |
|
60
|
expect(capturedSQL).not.toMatch( |
|
61
|
/RENAME TO "public"\."order_order_payout_payout"/ |
|
62
|
) |
|
63
|
}) |
|
64
|
|
|
65
|
it("should schema-qualify the source table in ALTER TABLE", async () => { |
|
66
|
await planner.testRenameOldTable( |
|
67
|
makeMockOrm(), |
|
68
|
"order_payout", |
|
69
|
"order_order_payout_payout", |
|
70
|
descriptor |
|
71
|
) |
|
72
|
|
|
73
|
expect(capturedSQL).toMatch(/ALTER TABLE "public"\."order_payout"/) |
|
74
|
}) |
|
75
|
|
|
76
|
it("should update the tracking table entry to the new name", async () => { |
|
77
|
await planner.testRenameOldTable( |
|
78
|
makeMockOrm(), |
|
79
|
"order_payout", |
|
80
|
"order_order_payout_payout", |
|
81
|
descriptor |
|
82
|
) |
|
83
|
|
|
84
|
expect(capturedSQL).toMatch(/SET table_name = 'order_order_payout_payout'/) |
|
85
|
expect(capturedSQL).toMatch(/WHERE table_name = 'order_payout'/) |
|
86
|
}) |
|
87
|
}) |