refactor: use module name as the snapshot name (#11802)

Fixes: FRMW-2930

This PR updates the MikroORM config to use the module name as the snapshot name when generating migration files. Otherwise, MikroORM defaults to the database name and every time you update the database name, it will create a new snapshot. 

Also, we migrate existing snapshot files to be same the new file name to avoid breaking changes.
This commit is contained in:
Harminder Virk
2025-03-12 12:32:19 +05:30
committed by GitHub
parent 267af9f3f6
commit 375c4a5ab1
7 changed files with 94 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ describe("defineMikroOrmCliConfig", () => {
dbName: "medusa-fulfillment",
migrations: {
generator: expect.any(Function),
snapshotName: ".snapshot-medusa-my-test",
},
})
})
@@ -44,6 +45,7 @@ describe("defineMikroOrmCliConfig", () => {
password: "",
migrations: {
generator: expect.any(Function),
snapshotName: ".snapshot-medusa-my-test",
},
})
})

View File

@@ -3,6 +3,7 @@ import { mikroOrmCreateConnection } from "../../dal"
import { loadDatabaseConfig } from "../load-module-database-config"
import { Migrations } from "../../migrations"
import { toMikroOrmEntities } from "../../dml"
import { kebabCase } from "../../common/to-kebab-case"
const TERMINAL_SIZE = process.stdout.columns
@@ -42,7 +43,12 @@ export function buildGenerateMigrationScript({
const normalizedModels = toMikroOrmEntities(models)
const orm = await mikroOrmCreateConnection(
dbData,
{
...dbData,
snapshotName: `.snapshot-${kebabCase(
moduleName.replace("Service", "")
)}`,
},
normalizedModels,
pathToMigrations
)

View File

@@ -61,6 +61,7 @@ export function defineMikroOrmCliConfig(
...(options as any),
entities: entities.filter(Boolean),
migrations: {
snapshotName: `.snapshot-${databaseName}`,
generator: CustomTsMigrationGenerator,
...options.migrations,
},