Skip to content

Commit e59cdae

Browse files
authored
fix(): Proper schema usage when running migrations (#14036)
* fix(): Proper schema usage when running migrations * Create thick-pugs-dance.md
1 parent da4c011 commit e59cdae

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

.changeset/thick-pugs-dance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/link-modules": patch
3+
"@medusajs/utils": patch
4+
---
5+
6+
fix(): Proper schema usage when running migrations

packages/core/utils/src/dal/mikro-orm/custom-db-migrator.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ export class CustomDBMigrator extends BaseMigrator {
3232
const MigrationClass = Object.values(
3333
migration
3434
)[0] as Constructor<Migration>
35-
const instance = new MigrationClass($this.driver, $this.config)
35+
const instance = new MigrationClass(
36+
$this.driver,
37+
$this.config
38+
) as Migration
39+
40+
const customSchema = $this.config.options.schema
41+
if (customSchema) {
42+
const up = instance.up
43+
const down = instance.down
44+
instance.up = async function (...args) {
45+
this.driver.execute(`SET LOCAL search_path TO ${customSchema}`)
46+
return up.bind(this)(...args)
47+
}
48+
instance.down = async function (...args) {
49+
this.driver.execute(`SET LOCAL search_path TO ${customSchema}`)
50+
return down.bind(this)(...args)
51+
}
52+
}
53+
3654
await $this.runner.run(instance, method)
3755
}
3856

packages/modules/link-modules/src/migration/index.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
3131
*/
3232
#dbConfig: ReturnType<typeof ModulesSdkUtils.loadDatabaseConfig>
3333

34+
#schema: string = "public"
35+
3436
/**
3537
* The set of commands that are unsafe to execute automatically when
3638
* performing "alter table"
@@ -56,6 +58,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
5658
options?: ModuleServiceInitializeOptions
5759
) {
5860
this.#dbConfig = ModulesSdkUtils.loadDatabaseConfig("link_modules", options)
61+
this.#schema = options?.database?.schema ?? "public"
5962
this.#linksEntities = joinerConfig
6063
.map((config) => {
6164
if (config.isReadOnlyLink) {
@@ -96,7 +99,7 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
9699
orm: MikroORM<PostgreSqlDriver>
97100
): Promise<void> {
98101
await orm.em.getDriver().getConnection().execute(`
99-
CREATE TABLE IF NOT EXISTS "${this.tableName}" (
102+
CREATE TABLE IF NOT EXISTS "${this.#schema}"."${this.tableName}" (
100103
id SERIAL PRIMARY KEY,
101104
table_name VARCHAR(255) NOT NULL UNIQUE,
102105
link_descriptor JSONB NOT NULL DEFAULT '{}'::jsonb,
@@ -125,7 +128,8 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
125128
>(
126129
`
127130
SELECT table_name
128-
FROM information_schema.tables;
131+
FROM information_schema.tables
132+
WHERE table_schema = '${this.#schema}';
129133
`
130134
)
131135
)
@@ -155,7 +159,9 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
155159
.getConnection()
156160
.execute(
157161
`
158-
INSERT INTO ${this.tableName} (table_name, link_descriptor) VALUES ${positionalArgs} ON CONFLICT DO NOTHING;
162+
INSERT INTO "${this.#schema}"."${
163+
this.tableName
164+
}" (table_name, link_descriptor) VALUES ${positionalArgs} ON CONFLICT DO NOTHING;
159165
`,
160166
existingTables.flatMap((tableName, index) => [
161167
tableName,
@@ -185,7 +191,12 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
185191
.getConnection()
186192
.execute(
187193
`
188-
INSERT INTO "${this.tableName}" (table_name, link_descriptor) VALUES (?, ?);
194+
SET LOCAL search_path TO "${this.#schema}";
195+
196+
INSERT INTO "${
197+
this.tableName
198+
}" (table_name, link_descriptor) VALUES (?, ?);
199+
189200
${sql}
190201
`,
191202
[tableName, linkDescriptor]
@@ -201,8 +212,10 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
201212
tableName: string
202213
) {
203214
await orm.em.getDriver().getConnection().execute(`
204-
DROP TABLE IF EXISTS "${tableName}";
205-
DELETE FROM "${this.tableName}" WHERE table_name = '${tableName}';
215+
DROP TABLE IF EXISTS "${this.#schema}"."${tableName}";
216+
DELETE FROM "${this.#schema}"."${
217+
this.tableName
218+
}" WHERE table_name = '${tableName}';
206219
`)
207220
}
208221

@@ -227,7 +240,9 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
227240
link_descriptor: PlannerActionLinkDescriptor
228241
}[]
229242
>(`
230-
SELECT table_name, link_descriptor from "${this.tableName}"
243+
SELECT table_name, link_descriptor from "${this.#schema}"."${
244+
this.tableName
245+
}"
231246
`)
232247

233248
return results.map((tuple) => ({
@@ -410,10 +425,12 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
410425
descriptor: PlannerActionLinkDescriptor
411426
) {
412427
await orm.em.getDriver().getConnection().execute(`
413-
ALTER TABLE "${oldName}" RENAME TO "${newName}";
414-
UPDATE "${
415-
this.tableName
416-
}" SET table_name = '${newName}', link_descriptor = '${JSON.stringify(
428+
ALTER TABLE "${this.#schema}"."${oldName}" RENAME TO "${
429+
this.#schema
430+
}"."${newName}";
431+
UPDATE "${this.#schema}"."${
432+
this.tableName
433+
}" SET table_name = '${newName}', link_descriptor = '${JSON.stringify(
417434
descriptor
418435
)}' WHERE table_name = '${oldName}';
419436
`)
@@ -504,7 +521,10 @@ export class MigrationsExecutionPlanner implements ILinkMigrationsPlanner {
504521
case "create":
505522
return await this.createLinkTable(orm, action)
506523
case "update":
507-
return await orm.em.getDriver().getConnection().execute(action.sql)
524+
const sql = `SET LOCAL search_path TO "${this.#schema}"; \n\n${
525+
action.sql
526+
}`
527+
return await orm.em.getDriver().getConnection().execute(sql)
508528
default:
509529
return
510530
}

0 commit comments

Comments
 (0)