Files
medusa-store/packages/modules/link-modules/integration-tests/__tests__/migrations.spec.ts
Carlos R. L. Rodrigues cc73802ab3 chore(order): dml (#10292)
* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* order dml

* cleanup

* re add foerign key indexes

* wip

* chore: remove unused types

* wip

* changes

* rm raw

* autoincrement

* wip

* rel

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* number searchable

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* types

* wip

* WIP type improvements

* update order models

* partially fix types temporarely

* rel

* fix: recursive type issue

* improve type inference breaks

* improve type inference breaks

* update models

* rm nullable

* default value

* repository

* update default value handling

* fix unit tests

* WIP

* toMikroORM

* fix relations

* cascades

* fix

* experimental dml hooks

* rm migration

* serial

* nullable autoincrement

* fix model

* model changes

* fix one to one DML

* order test

* fix addresses

* fix unit tests

* Re align dml entity name inference

* update model table name config

* update model table name config

* revert

* update return relation

* WIP

* hasOne

* models

* fix

* model

* initial commit

* cart service

* order module

* utils unit test

* index engine

* changeset

* merge

* fix hasOne with fk

* update

* free text filter per entity

* tests

* prod category

* property string many to many

* fix big number

* link modules migration set names

* merge

* shipping option rules

* serializer

* unit test

* fix test mikro orm init

* fix test mikro orm init

* Maintain merge object properties

* fix test mikro orm init

* prevent unit test from connecting to db

* wip

* fix test

* fix test

* link test

* schema

* models

* auto increment

* hook

* model hooks

* order

* wip

* orm version

* request return field

* fix return configuration on order model

* workflows

* core flows

* unit test

* test

* base repo

* test

* base repo

* test fix

* inventory move

* locking inventory

* test

* free text fix

* rm timeout mock

* migrate fulfillment values

* v6.4.3

* cleanup

* link-modules update sql

* revert test

* remove fake timers

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2025-01-21 08:04:47 -05:00

196 lines
7.1 KiB
TypeScript

import { MedusaModule } from "@medusajs/framework/modules-sdk"
import { ILinkModule, ModuleJoinerConfig } from "@medusajs/framework/types"
import { defineLink, isObject, Modules } from "@medusajs/framework/utils"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { MigrationsExecutionPlanner } from "../../src"
import {
Car,
carJoinerConfig,
CarModule,
CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule,
longNameJoinerConfig,
User,
userJoinerConfig,
UserModule,
} from "../__fixtures__/migrations"
jest.setTimeout(30000)
MedusaModule.setJoinerConfig(userJoinerConfig.serviceName, userJoinerConfig)
MedusaModule.setJoinerConfig(carJoinerConfig.serviceName, carJoinerConfig)
MedusaModule.setJoinerConfig(
longNameJoinerConfig.serviceName,
longNameJoinerConfig
)
moduleIntegrationTestRunner<ILinkModule>({
moduleName: Modules.LINK,
moduleModels: [User, Car],
testSuite: ({ dbConfig }) => {
describe("MigrationsExecutionPlanner", () => {
test("should generate an execution plan", async () => {
defineLink(UserModule.linkable.user, CarModule.linkable.car)
defineLink(
UserModule.linkable.user,
CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule
.linkable.veryLongTableNameOfCustomModule
)
MedusaModule.getCustomLinks().forEach((linkDefinition: any) => {
MedusaModule.setCustomLink(
linkDefinition(MedusaModule.getAllJoinerConfigs())
)
})
/**
* Expect a create plan
*/
let joinerConfigs = MedusaModule.getCustomLinks().filter(
(link): link is ModuleJoinerConfig => isObject(link)
)
let planner = new MigrationsExecutionPlanner(joinerConfigs, {
database: dbConfig,
})
let actionPlan = await planner.createPlan()
await planner.executePlan(actionPlan)
expect(actionPlan).toHaveLength(2)
expect(actionPlan[0]).toEqual({
action: "create",
linkDescriptor: {
fromModule: "user",
toModule: "car",
fromModel: "user",
toModel: "car",
},
tableName: "user_user_car_car",
sql:
"set names 'utf8';\n" +
"\n" +
'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' +
'create index if not exists "IDX_car_id_-92128f74" on "user_user_car_car" ("car_id");\n' +
'create index if not exists "IDX_id_-92128f74" on "user_user_car_car" ("id");\n' +
'create index if not exists "IDX_user_id_-92128f74" on "user_user_car_car" ("user_id");\n' +
'create index if not exists "IDX_deleted_at_-92128f74" on "user_user_car_car" ("deleted_at");\n' +
"\n",
})
expect(actionPlan[1]).toEqual({
action: "create",
linkDescriptor: {
fromModule: "user",
toModule:
"CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATable",
fromModel: "user",
toModel: "very_long_table_name_of_custom_module",
},
tableName: "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de",
sql:
"set names 'utf8';\n" +
"\n" +
'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' +
'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");\n' +
'create index if not exists "IDX_id_1776e67de" on "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("id");\n' +
'create index if not exists "IDX_user_id_1776e67de" on "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("user_id");\n' +
'create index if not exists "IDX_deleted_at_1776e67de" on "user_user_cust_very_long_tabl_name_of_cust_modu1776e67de" ("deleted_at");\n' +
"\n",
})
/**
* Expect an update plan
*/
// @ts-ignore
MedusaModule.customLinks_.length = 0
defineLink(UserModule.linkable.user, CarModule.linkable.car, {
database: {
extraColumns: {
data: {
type: "json",
},
},
},
})
MedusaModule.getCustomLinks().forEach((linkDefinition: any) => {
MedusaModule.setCustomLink(
linkDefinition(MedusaModule.getAllJoinerConfigs())
)
})
joinerConfigs = MedusaModule.getCustomLinks().filter(
(link): link is ModuleJoinerConfig => isObject(link)
)
planner = new MigrationsExecutionPlanner(joinerConfigs, {
database: dbConfig,
})
actionPlan = await planner.createPlan()
await planner.executePlan(actionPlan)
expect(actionPlan).toHaveLength(2)
expect(actionPlan[0]).toEqual({
action: "update",
linkDescriptor: {
fromModule: "user",
toModule: "car",
fromModel: "user",
toModel: "car",
},
tableName: "user_user_car_car",
sql: 'alter table if exists "user_user_car_car" add column if not exists "data" jsonb not null;',
})
/**
* Expect a noop plan
*/
actionPlan = await planner.createPlan()
await planner.executePlan(actionPlan)
expect(actionPlan).toHaveLength(1)
expect(actionPlan[0]).toEqual({
action: "noop",
linkDescriptor: {
fromModule: "user",
toModule: "car",
fromModel: "user",
toModel: "car",
},
tableName: "user_user_car_car",
})
/**
* Expect a delete plan
*/
joinerConfigs = []
planner = new MigrationsExecutionPlanner(joinerConfigs, {
database: dbConfig,
})
actionPlan = await planner.createPlan()
expect(actionPlan).toHaveLength(1)
expect(actionPlan[0]).toEqual({
action: "delete",
tableName: "user_user_car_car",
linkDescriptor: {
toModel: "car",
toModule: "car",
fromModel: "user",
fromModule: "user",
},
})
})
})
},
})