fix: Link migration descriptor case changes and hash computation (#9560)

**What**
The module service name case has changed and the hash generation was performed on the non lower cased version of it while after the hash generation everything is based on the lower case version of the generated table name form the service names leading to different hash computation. This pr update the link migration table to adjust the to/from module value with its new value as well as generating the hash from the lower case version of the computed table name

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-10-14 16:32:50 +02:00
committed by GitHub
parent 170b3a08b7
commit cea4cdc8d7
6 changed files with 84 additions and 62 deletions

View File

@@ -1,40 +1,15 @@
import { ModuleServiceInitializeOptions } from "@medusajs/types"
import { Filter as MikroORMFilter } from "@mikro-orm/core"
import { TSMigrationGenerator } from "@mikro-orm/migrations"
import { ModuleServiceInitializeOptions } from "@medusajs/types"
import { isString } from "../../common"
import { normalizeMigrationSQL } from "../utils"
type FilterDef = Parameters<typeof MikroORMFilter>[0]
export class CustomTsMigrationGenerator extends TSMigrationGenerator {
createStatement(sql: string, padLeft: number): string {
if (isString(sql)) {
sql = sql.replace(
/create table (?!if not exists)/g,
"create table if not exists "
)
sql = sql.replace(/alter table (?!if exists)/g, "alter table if exists ")
sql = sql.replace(
/create index (?!if not exists)/g,
"create index if not exists "
)
sql = sql.replace(/drop index (?!if exists)/g, "drop index if exists ")
sql = sql.replace(
/create unique index (?!if not exists)/g,
"create unique index if not exists "
)
sql = sql.replace(
/drop unique index (?!if exists)/g,
"drop unique index if exists "
)
sql = sql.replace(
/add column (?!if not exists)/g,
"add column if not exists "
)
sql = sql.replace(/drop column (?!if exists)/g, "drop column if exists ")
sql = sql.replace(
/drop constraint (?!if exists)/g,
"drop constraint if exists "
)
sql = normalizeMigrationSQL(sql)
}
return super.createStatement(sql, padLeft)

View File

@@ -83,3 +83,36 @@ export function getSoftDeletedCascadedEntitiesIdsMappedBy({
return Object.fromEntries(deletedEntitiesMap)
}
export function normalizeMigrationSQL(sql: string) {
sql = sql.replace(
/create table (?!if not exists)/g,
"create table if not exists "
)
sql = sql.replace(/alter table (?!if exists)/g, "alter table if exists ")
sql = sql.replace(
/create index (?!if not exists)/g,
"create index if not exists "
)
sql = sql.replace(/alter index (?!if exists)/g, "alter index if exists ")
sql = sql.replace(/drop index (?!if exists)/g, "drop index if exists ")
sql = sql.replace(
/create unique index (?!if not exists)/g,
"create unique index if not exists "
)
sql = sql.replace(
/drop unique index (?!if exists)/g,
"drop unique index if exists "
)
sql = sql.replace(
/add column (?!if not exists)/g,
"add column if not exists "
)
sql = sql.replace(/drop column (?!if exists)/g, "drop column if exists ")
sql = sql.replace(
/drop constraint (?!if exists)/g,
"drop constraint if exists "
)
return sql
}