chore(): Generate migrations with correct mikro orm import path (#13893)
* chore(): Generate migrations with correct mikro orm import path * Create shy-melons-jog.md
This commit is contained in:
committed by
GitHub
parent
0f79f22ebc
commit
6d7ba778f5
@@ -14,6 +14,22 @@ import { dirname, join } from "path"
|
||||
import { readDir } from "../common"
|
||||
import { CustomDBMigrator } from "../dal/mikro-orm/custom-db-migrator"
|
||||
|
||||
// Define the replacement mappings
|
||||
const replacements = [
|
||||
// MikroORM imports - replace mikro-orm/{subpath} with @medusajs/framework/mikro-orm/{subpath}
|
||||
{
|
||||
pattern: /from\s+['"]@?mikro-orm\/([^'"]+)['"]/g,
|
||||
// eslint-disable-next-line quotes
|
||||
replacement: 'from "@medusajs/framework/mikro-orm/$1"',
|
||||
},
|
||||
// PG imports - replace pg with @medusajs/framework/pg
|
||||
{
|
||||
pattern: /from\s+['"]pg['"]/g,
|
||||
// eslint-disable-next-line quotes
|
||||
replacement: 'from "@medusajs/framework/pg"',
|
||||
},
|
||||
]
|
||||
|
||||
/**
|
||||
* Events emitted by the migrations class
|
||||
*/
|
||||
@@ -70,7 +86,33 @@ export class Migrations extends EventEmitter<MigrationsEvents> {
|
||||
try {
|
||||
await this.migrateSnapshotFile(migrator["snapshotPath"])
|
||||
await this.ensureSnapshot(migrator["snapshotPath"])
|
||||
return await migrator.createMigration()
|
||||
const migrationResult = await migrator.createMigration()
|
||||
const code = migrationResult.code
|
||||
if (code) {
|
||||
let modifiedContent = code
|
||||
let wasModified = false
|
||||
|
||||
replacements.forEach(({ pattern, replacement }) => {
|
||||
const newContent = modifiedContent.replace(pattern, replacement)
|
||||
if (newContent !== modifiedContent) {
|
||||
wasModified = true
|
||||
modifiedContent = newContent
|
||||
}
|
||||
})
|
||||
|
||||
if (wasModified) {
|
||||
await writeFile(
|
||||
join(
|
||||
connection.config.getDriver().config.get("migrations").path ?? "",
|
||||
migrationResult.fileName
|
||||
),
|
||||
modifiedContent,
|
||||
"utf-8"
|
||||
)
|
||||
}
|
||||
migrationResult.code = modifiedContent
|
||||
}
|
||||
return migrationResult
|
||||
} finally {
|
||||
await connection.close(true)
|
||||
}
|
||||
|
||||
@@ -26,13 +26,12 @@ export function buildGenerateMigrationScript({
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
return async function ({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
return async function (
|
||||
{ options, logger } = {} as Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
>
|
||||
) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
logger.info(new Array(TERMINAL_SIZE).join("-"))
|
||||
|
||||
Reference in New Issue
Block a user