@@ -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