|
1
|
import { MedusaModule } from "@medusajs/framework/modules-sdk" |
|
2
|
import { ILinkModule, ModuleJoinerConfig } from "@medusajs/framework/types" |
|
3
|
import { defineLink, isObject, Modules } from "@medusajs/framework/utils" |
|
4
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
5
|
import { MigrationsExecutionPlanner } from "../../src" |
|
6
|
import { |
|
7
|
Car, |
|
8
|
carJoinerConfig, |
|
9
|
CarModule, |
|
10
|
User, |
|
11
|
userJoinerConfig, |
|
12
|
UserModule, |
|
13
|
} from "../__fixtures__/migrations" |
|
14
|
|
|
15
|
jest.setTimeout(30000) |
|
16
|
|
|
17
|
MedusaModule.setJoinerConfig(userJoinerConfig.serviceName, userJoinerConfig) |
|
18
|
MedusaModule.setJoinerConfig(carJoinerConfig.serviceName, carJoinerConfig) |
|
19
|
|
|
20
|
moduleIntegrationTestRunner<ILinkModule>({ |
|
21
|
moduleName: Modules.LINK, |
|
22
|
moduleModels: [User, Car], |
|
23
|
testSuite: ({ dbConfig }) => { |
|
24
|
describe("MigrationsExecutionPlanner - table rename", () => { |
|
25
|
test("should rename a stale tracked table to its current computed name without throwing a PostgreSQL syntax error", async () => { |
|
26
|
defineLink(UserModule.linkable.user, CarModule.linkable.car) |
|
27
|
|
|
28
|
MedusaModule.getCustomLinks().forEach((linkDefinition: any) => { |
|
29
|
MedusaModule.setCustomLink( |
|
30
|
linkDefinition(MedusaModule.getAllJoinerConfigs()) |
|
31
|
) |
|
32
|
}) |
|
33
|
|
|
34
|
const joinerConfigs = MedusaModule.getCustomLinks().filter( |
|
35
|
(link): link is ModuleJoinerConfig => isObject(link) |
|
36
|
) |
|
37
|
|
|
38
|
const schema = (dbConfig as any).schema ?? "public" |
|
39
|
// Simulates a table name that was produced by a previous naming convention |
|
40
|
// before the 4-segment convention (e.g. "order_payout" instead of |
|
41
|
// "order_order_payout_payout"). link_module_migrations tracks this stale name. |
|
42
|
const oldTableName = "order_payout" |
|
43
|
|
|
44
|
// Seed a pre-upgrade DB state: physical table exists under the old name |
|
45
|
// and the tracking table records that old name. |
|
46
|
const seedPlanner = new MigrationsExecutionPlanner([], { |
|
47
|
database: dbConfig, |
|
48
|
}) |
|
49
|
const seedOrm = await (seedPlanner as any).createORM() |
|
50
|
try { |
|
51
|
const conn = seedOrm.em.getDriver().getConnection() |
|
52
|
await conn.execute(` |
|
53
|
CREATE TABLE IF NOT EXISTS "${schema}"."link_module_migrations" ( |
|
54
|
id SERIAL PRIMARY KEY, |
|
55
|
table_name VARCHAR(255) NOT NULL UNIQUE, |
|
56
|
link_descriptor JSONB NOT NULL DEFAULT '{}'::jsonb, |
|
57
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
|
58
|
); |
|
59
|
|
|
60
|
CREATE TABLE "${schema}"."${oldTableName}" ( |
|
61
|
"user_id" varchar(255) not null, |
|
62
|
"car_id" varchar(255) not null, |
|
63
|
"id" varchar(255) not null, |
|
64
|
"created_at" timestamptz not null default CURRENT_TIMESTAMP, |
|
65
|
"updated_at" timestamptz not null default CURRENT_TIMESTAMP, |
|
66
|
"deleted_at" timestamptz null, |
|
67
|
constraint "${oldTableName}_pkey" primary key ("user_id", "car_id") |
|
68
|
); |
|
69
|
|
|
70
|
INSERT INTO "${schema}"."link_module_migrations" (table_name, link_descriptor) |
|
71
|
VALUES ( |
|
72
|
'${oldTableName}', |
|
73
|
'{"fromModule":"user","toModule":"car","fromModel":"user","toModel":"car"}' |
|
74
|
); |
|
75
|
`) |
|
76
|
} finally { |
|
77
|
await seedOrm.close(true) |
|
78
|
} |
|
79
|
|
|
80
|
// Before the fix, renameOldTable generated: |
|
81
|
// ALTER TABLE "public"."order_payout" RENAME TO "public"."user_user_car_car" |
|
82
|
// which PostgreSQL rejects with ERROR 42601: syntax error at or near "." |
|
83
|
const planner = new MigrationsExecutionPlanner(joinerConfigs, { |
|
84
|
database: dbConfig, |
|
85
|
}) |
|
86
|
const actionPlan = await planner.createPlan() |
|
87
|
|
|
88
|
expect(actionPlan).toHaveLength(1) |
|
89
|
expect(actionPlan[0].tableName).toBe("user_user_car_car") |
|
90
|
// "create" would mean the rename silently failed and the planner treated |
|
91
|
// the table as a brand-new untracked entity. |
|
92
|
expect(actionPlan[0].action).not.toBe("create") |
|
93
|
}) |
|
94
|
}) |
|
95
|
}, |
|
96
|
}) |