From e59cdae3365981339d55ec01224f09995250e67d Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 12 Nov 2025 15:56:50 +0100 Subject: [PATCH] fix(): Proper schema usage when running migrations (#14036) * fix(): Proper schema usage when running migrations * Create thick-pugs-dance.md --- .changeset/thick-pugs-dance.md | 6 +++ .../src/dal/mikro-orm/custom-db-migrator.ts | 20 ++++++++- .../link-modules/src/migration/index.ts | 44 ++++++++++++++----- 3 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 .changeset/thick-pugs-dance.md diff --git a/.changeset/thick-pugs-dance.md b/.changeset/thick-pugs-dance.md new file mode 100644 index 0000000000..d3f56d3989 --- /dev/null +++ b/.changeset/thick-pugs-dance.md @@ -0,0 +1,6 @@ +--- +"@medusajs/link-modules": patch +"@medusajs/utils": patch +--- + +fix(): Proper schema usage when running migrations diff --git a/packages/core/utils/src/dal/mikro-orm/custom-db-migrator.ts b/packages/core/utils/src/dal/mikro-orm/custom-db-migrator.ts index bb20ad1901..aac0c7f278 100644 --- a/packages/core/utils/src/dal/mikro-orm/custom-db-migrator.ts +++ b/packages/core/utils/src/dal/mikro-orm/custom-db-migrator.ts @@ -32,7 +32,25 @@ export class CustomDBMigrator extends BaseMigrator { const MigrationClass = Object.values( migration )[0] as Constructor - const instance = new MigrationClass($this.driver, $this.config) + const instance = new MigrationClass( + $this.driver, + $this.config + ) as Migration + + const customSchema = $this.config.options.schema + if (customSchema) { + const up = instance.up + const down = instance.down + instance.up = async function (...args) { + this.driver.execute(`SET LOCAL search_path TO ${customSchema}`) + return up.bind(this)(...args) + } + instance.down = async function (...args) { + this.driver.execute(`SET LOCAL search_path TO ${customSchema}`) + return down.bind(this)(...args) + } + } + await $this.runner.run(instance, method) } diff --git a/packages/modules/link-modules/src/migration/index.ts b/packages/modules/link-modules/src/migration/index.ts index 41c7d9b04f..ce96564f06 100644 --- a/packages/modules/link-modules/src/migration/index.ts +++ b/packages/modules/link-modules/src/migration/index.ts @@ -31,6 +31,8 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { */ #dbConfig: ReturnType + #schema: string = "public" + /** * The set of commands that are unsafe to execute automatically when * performing "alter table" @@ -56,6 +58,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { options?: ModuleServiceInitializeOptions ) { this.#dbConfig = ModulesSdkUtils.loadDatabaseConfig("link_modules", options) + this.#schema = options?.database?.schema ?? "public" this.#linksEntities = joinerConfig .map((config) => { if (config.isReadOnlyLink) { @@ -96,7 +99,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { orm: MikroORM ): Promise { await orm.em.getDriver().getConnection().execute(` - CREATE TABLE IF NOT EXISTS "${this.tableName}" ( + CREATE TABLE IF NOT EXISTS "${this.#schema}"."${this.tableName}" ( id SERIAL PRIMARY KEY, table_name VARCHAR(255) NOT NULL UNIQUE, link_descriptor JSONB NOT NULL DEFAULT '{}'::jsonb, @@ -125,7 +128,8 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { >( ` SELECT table_name - FROM information_schema.tables; + FROM information_schema.tables + WHERE table_schema = '${this.#schema}'; ` ) ) @@ -155,7 +159,9 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { .getConnection() .execute( ` - INSERT INTO ${this.tableName} (table_name, link_descriptor) VALUES ${positionalArgs} ON CONFLICT DO NOTHING; + INSERT INTO "${this.#schema}"."${ + this.tableName + }" (table_name, link_descriptor) VALUES ${positionalArgs} ON CONFLICT DO NOTHING; `, existingTables.flatMap((tableName, index) => [ tableName, @@ -185,7 +191,12 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { .getConnection() .execute( ` - INSERT INTO "${this.tableName}" (table_name, link_descriptor) VALUES (?, ?); + SET LOCAL search_path TO "${this.#schema}"; + + INSERT INTO "${ + this.tableName + }" (table_name, link_descriptor) VALUES (?, ?); + ${sql} `, [tableName, linkDescriptor] @@ -201,8 +212,10 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { tableName: string ) { await orm.em.getDriver().getConnection().execute(` - DROP TABLE IF EXISTS "${tableName}"; - DELETE FROM "${this.tableName}" WHERE table_name = '${tableName}'; + DROP TABLE IF EXISTS "${this.#schema}"."${tableName}"; + DELETE FROM "${this.#schema}"."${ + this.tableName + }" WHERE table_name = '${tableName}'; `) } @@ -227,7 +240,9 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { link_descriptor: PlannerActionLinkDescriptor }[] >(` - SELECT table_name, link_descriptor from "${this.tableName}" + SELECT table_name, link_descriptor from "${this.#schema}"."${ + this.tableName + }" `) return results.map((tuple) => ({ @@ -410,10 +425,12 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { descriptor: PlannerActionLinkDescriptor ) { await orm.em.getDriver().getConnection().execute(` - ALTER TABLE "${oldName}" RENAME TO "${newName}"; - UPDATE "${ - this.tableName - }" SET table_name = '${newName}', link_descriptor = '${JSON.stringify( + ALTER TABLE "${this.#schema}"."${oldName}" RENAME TO "${ + this.#schema + }"."${newName}"; + UPDATE "${this.#schema}"."${ + this.tableName + }" SET table_name = '${newName}', link_descriptor = '${JSON.stringify( descriptor )}' WHERE table_name = '${oldName}'; `) @@ -504,7 +521,10 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner { case "create": return await this.createLinkTable(orm, action) case "update": - return await orm.em.getDriver().getConnection().execute(action.sql) + const sql = `SET LOCAL search_path TO "${this.#schema}"; \n\n${ + action.sql + }` + return await orm.em.getDriver().getConnection().execute(sql) default: return }