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 CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule,
11 longNameJoinerConfig,
12 User,
13 userJoinerConfig,
14 UserModule,
15 } from "../__fixtures__/migrations"
16
17 jest.setTimeout(30000)
18
19 MedusaModule.setJoinerConfig(userJoinerConfig.serviceName, userJoinerConfig)
20 MedusaModule.setJoinerConfig(carJoinerConfig.serviceName, carJoinerConfig)
21 MedusaModule.setJoinerConfig(
22 longNameJoinerConfig.serviceName,
23 longNameJoinerConfig
24 )
25
26 moduleIntegrationTestRunner<ILinkModule>({
27 moduleName: Modules.LINK,
28 moduleModels: [User, Car],
29 testSuite: ({ dbConfig }) => {
30 describe("MigrationsExecutionPlanner", () => {
31 test("should generate an execution plan", async () => {
32 defineLink(UserModule.linkable.user, CarModule.linkable.car)
33 defineLink(
34 UserModule.linkable.user,
35 CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule
36 .linkable.veryLongTableNameOfCustomModule
37 )
38
39 MedusaModule.getCustomLinks().forEach((linkDefinition: any) => {
40 MedusaModule.setCustomLink(
41 linkDefinition(MedusaModule.getAllJoinerConfigs())
42 )
43 })
44
45 /**
46 * Expect a create plan
47 */
48
49 let joinerConfigs = MedusaModule.getCustomLinks().filter(
50 (link): link is ModuleJoinerConfig => isObject(link)
51 )
52
53 let planner = new MigrationsExecutionPlanner(joinerConfigs, {
54 database: dbConfig,
55 })
56
57 let actionPlan = await planner.createPlan()
58
59 await planner.executePlan(actionPlan)
60
61 expect(actionPlan).toHaveLength(2)
62
63 expect(actionPlan[0]).toEqual({
64 action: "create",
65 linkDescriptor: {
66 fromModule: "user",
67 toModule: "car",
68 fromModel: "user",
69 toModel: "car",
70 },
71 tableName: "user_user_car_car",
72 sql:
73 "set names 'utf8';\n" +
74 "\n" +
75 'create table if not exists "user_user_car_car" ("user_id" varchar(255) not null, "car_id" varchar(255) not null, "id" varchar(255) not null, "created_at" timestamptz not null default CURRENT_TIMESTAMP, "updated_at" timestamptz not null default CURRENT_TIMESTAMP, "deleted_at" timestamptz null, constraint "user_user_car_car_pkey" primary key ("user_id", "car_id"));\n' +
76 'create index if not exists "IDX_id_-92128f74" on "user_user_car_car" ("id");\n' +
77 'CREATE INDEX IF NOT EXISTS "IDX_user_id_-92128f74" ON "user_user_car_car" ("user_id") WHERE deleted_at IS NULL;\n' +
78 'CREATE INDEX IF NOT EXISTS "IDX_car_id_-92128f74" ON "user_user_car_car" ("car_id") WHERE deleted_at IS NULL;\n' +
79 'create index if not exists "IDX_deleted_at_-92128f74" on "user_user_car_car" ("deleted_at");\n' +
80 "\n",
81 })
82
83 expect(actionPlan[1]).toEqual({
84 action: "create",
85 linkDescriptor: {
86 fromModule: "user",
87 toModule:
88 "CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATable",
89 fromModel: "user",
90 toModel: "very_long_table_name_of_custom_module",
91 },
92 tableName: "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de",
93 sql:
94 "set names 'utf8';\n" +
95 "\n" +
96 'create table if not exists "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("user_id" varchar(255) not null, "very_long_table_name_of_custom_module_id" varchar(255) not null, "id" varchar(255) not null, "created_at" timestamptz not null default CURRENT_TIMESTAMP, "updated_at" timestamptz not null default CURRENT_TIMESTAMP, "deleted_at" timestamptz null, constraint "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de_pkey" primary key ("user_id", "very_long_table_name_of_custom_module_id"));\n' +
97 'create index if not exists "IDX_id_1776e67de" on "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("id");\n' +
98 'CREATE INDEX IF NOT EXISTS "IDX_user_id_1776e67de" ON "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("user_id") WHERE deleted_at IS NULL;\n' +
99 'CREATE INDEX IF NOT EXISTS "IDX_very_long_table_name_of_custom_module_id_1776e67de" ON "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("very_long_table_name_of_custom_module_id") WHERE deleted_at IS NULL;\n' +
100 'create index if not exists "IDX_deleted_at_1776e67de" on "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("deleted_at");\n' +
101 "\n",
102 })
103
104 /**
105 * Expect an update plan
106 */
107 // @ts-ignore
108 MedusaModule.customLinks_.length = 0
109
110 defineLink(UserModule.linkable.user, CarModule.linkable.car, {
111 database: {
112 extraColumns: {
113 data: {
114 type: "json",
115 },
116 },
117 },
118 })
119
120 MedusaModule.getCustomLinks().forEach((linkDefinition: any) => {
121 MedusaModule.setCustomLink(
122 linkDefinition(MedusaModule.getAllJoinerConfigs())
123 )
124 })
125
126 joinerConfigs = MedusaModule.getCustomLinks().filter(
127 (link): link is ModuleJoinerConfig => isObject(link)
128 )
129
130 planner = new MigrationsExecutionPlanner(joinerConfigs, {
131 database: dbConfig,
132 })
133
134 actionPlan = await planner.createPlan()
135 await planner.executePlan(actionPlan)
136
137 expect(actionPlan).toHaveLength(2)
138 expect(actionPlan[0]).toEqual({
139 action: "update",
140 linkDescriptor: {
141 fromModule: "user",
142 toModule: "car",
143 fromModel: "user",
144 toModel: "car",
145 },
146 tableName: "user_user_car_car",
147 sql: 'alter table if exists "user_user_car_car" add column if not exists "data" jsonb not null;',
148 })
149
150 /**
151 * Expect a noop plan
152 */
153
154 actionPlan = await planner.createPlan()
155 await planner.executePlan(actionPlan)
156
157 expect(actionPlan).toHaveLength(1)
158 expect(actionPlan[0]).toEqual({
159 action: "noop",
160 linkDescriptor: {
161 fromModule: "user",
162 toModule: "car",
163 fromModel: "user",
164 toModel: "car",
165 },
166 tableName: "user_user_car_car",
167 })
168
169 /**
170 * Expect a delete plan
171 */
172
173 joinerConfigs = []
174
175 planner = new MigrationsExecutionPlanner(joinerConfigs, {
176 database: dbConfig,
177 })
178
179 actionPlan = await planner.createPlan()
180
181 expect(actionPlan).toHaveLength(1)
182 expect(actionPlan[0]).toEqual({
183 action: "delete",
184 tableName: "user_user_car_car",
185 linkDescriptor: {
186 toModel: "car",
187 toModule: "car",
188 fromModel: "user",
189 fromModule: "user",
190 },
191 })
192 })
193 })
194 },
195 })