committed by
GitHub
parent
cb79a5dbff
commit
d6ff526820
@@ -6,15 +6,15 @@ import {
|
||||
PlannerActionLinkDescriptor,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { generateEntity } from "../utils"
|
||||
import { EntitySchema, MikroORM } from "@mikro-orm/core"
|
||||
import { DatabaseSchema, PostgreSqlDriver } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
arrayDifference,
|
||||
DALUtils,
|
||||
ModulesSdkUtils,
|
||||
arrayDifference,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { EntitySchema, MikroORM } from "@mikro-orm/core"
|
||||
import { DatabaseSchema, PostgreSqlDriver } from "@mikro-orm/postgresql"
|
||||
import { generateEntity } from "../utils"
|
||||
|
||||
/**
|
||||
* The migrations execution planner creates a plan of SQL queries
|
||||
@@ -125,7 +125,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
|
||||
`
|
||||
)
|
||||
)
|
||||
.map(({ table_name }) => table_name)
|
||||
.map(({ table_name }) => table_name.toLowerCase())
|
||||
.filter((tableName) =>
|
||||
this.#linksEntities.some(
|
||||
({ entity }) => entity.meta.collection === tableName
|
||||
@@ -145,6 +145,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
|
||||
const positionalArgs = new Array(existingTables.length)
|
||||
.fill("(?, ?)")
|
||||
.join(", ")
|
||||
|
||||
await orm.em
|
||||
.getDriver()
|
||||
.getConnection()
|
||||
@@ -223,7 +224,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
|
||||
`)
|
||||
|
||||
return results.map((tuple) => ({
|
||||
table_name: tuple.table_name,
|
||||
table_name: tuple.table_name.toLowerCase(),
|
||||
link_descriptor: tuple.link_descriptor,
|
||||
}))
|
||||
}
|
||||
@@ -236,7 +237,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
|
||||
entity: EntitySchema,
|
||||
trackedLinksTables: string[]
|
||||
): Promise<LinkMigrationsPlannerAction> {
|
||||
const tableName = entity.meta.collection
|
||||
const tableName = entity.meta.collection.toLowerCase()
|
||||
const orm = await this.createORM([entity])
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { compressName } from "../compress-name"
|
||||
|
||||
describe("compressName", () => {
|
||||
it("should remove consecutive duplicate names in sequence if it exceds 58 chars", () => {
|
||||
const name =
|
||||
"product_product_variant_id_order_order_id_long_long_long_long_name"
|
||||
const result = compressName(name)
|
||||
expect(result).toBe("product_variant_id_order_id_long_name33bb7b344")
|
||||
})
|
||||
|
||||
it("should remove duplicate names and truncate name to append a hash", () => {
|
||||
const name = "product_product_variant_id_order_order_id"
|
||||
const result = compressName(name, 30)
|
||||
expect(result).toBe("product_variant_id_ord91d0cda8")
|
||||
})
|
||||
|
||||
it("handles very long names with truncation and appends hash when necessary", () => {
|
||||
const name =
|
||||
"CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule"
|
||||
const result = compressName(name)
|
||||
expect(result).toHaveLength(58)
|
||||
expect(result).toBe(
|
||||
"cust_modu_impl_cont_area_big_name_that_exce_posg_1f1cc72da"
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { camelToSnakeCase, simpleHash } from "@medusajs/utils"
|
||||
|
||||
export function compressName(name: string, limit = 58) {
|
||||
if (name.length <= limit) {
|
||||
return name.toLowerCase()
|
||||
}
|
||||
|
||||
const hash = simpleHash(name).toLowerCase()
|
||||
const hashLength = hash.length
|
||||
|
||||
// Remove consecutive duplicates
|
||||
const parts = name.toLowerCase().split("_")
|
||||
const deduplicatedParts: string[] = []
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
if (i === 0 || parts[i] !== parts[i - 1]) {
|
||||
deduplicatedParts.push(parts[i])
|
||||
}
|
||||
}
|
||||
|
||||
let result = deduplicatedParts.join("_")
|
||||
|
||||
// If still greater than the limit, truncate each part to 4 characters
|
||||
if (result.length > limit) {
|
||||
const allParts = camelToSnakeCase(name).split("_")
|
||||
result = allParts.map((part) => part.substring(0, 4)).join("_")
|
||||
}
|
||||
|
||||
name = result
|
||||
const nameLength = name.length
|
||||
const diff = nameLength + hashLength - limit
|
||||
|
||||
if (diff > 0) {
|
||||
return name.slice(0, nameLength - diff) + hash
|
||||
}
|
||||
|
||||
return (name + hash).toLowerCase()
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { compressName } from "./compress-name"
|
||||
|
||||
function getClass(...properties) {
|
||||
return class LinkModel {
|
||||
@@ -62,7 +63,7 @@ export function generateEntity(
|
||||
class: getClass(
|
||||
...fieldNames.concat("created_at", "updated_at", "deleted_at")
|
||||
) as any,
|
||||
tableName,
|
||||
tableName: compressName(tableName),
|
||||
properties: {
|
||||
id: {
|
||||
type: "string",
|
||||
|
||||
Reference in New Issue
Block a user