From 22edc48baa31d853478c7ff05d7237c0775acf9e Mon Sep 17 00:00:00 2001 From: Scott Koranda Date: Fri, 1 Dec 2023 14:38:36 +1000 Subject: [PATCH] Support PostgreSQL schema other than public (CFM-356) --- app/src/Command/TransmogrifyCommand.php | 13 +++++++++++++ app/src/Lib/Util/SchemaManager.php | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/app/src/Command/TransmogrifyCommand.php b/app/src/Command/TransmogrifyCommand.php index 43651d127..0d373b079 100644 --- a/app/src/Command/TransmogrifyCommand.php +++ b/app/src/Command/TransmogrifyCommand.php @@ -554,6 +554,12 @@ public function execute(Arguments $args, ConsoleIo $io) { } $this->inconn = DriverManager::getConnection($cargs, $inconfig); + + // If a PostgreSQL schema is configured force DBAL to respect it + // by setting the SEARCH_PATH. + if(!empty($incfg['schema']) && ($incfg['driver'] == 'Cake\Database\Driver\Postgres')) { + $this->inconn->exec("SET SEARCH_PATH TO " . $incfg['schema'] . ";"); + } $outconfig = new \Doctrine\DBAL\Configuration(); @@ -572,6 +578,13 @@ public function execute(Arguments $args, ConsoleIo $io) { } $this->outconn = DriverManager::getConnection($cargs, $outconfig); + + // If a PostgreSQL schema is configured force DBAL to respect it + // by setting the SEARCH_PATH. + if(!empty($outcfg['schema']) && ($outcfg['driver'] == 'Cake\Database\Driver\Postgres')) { + $this->outconn->exec("SET SEARCH_PATH TO " . $outcfg['schema'] . ";"); + } + $this->outdriver = $cargs['driver']; // We accept a list of table names, mostly for testing purposes diff --git a/app/src/Lib/Util/SchemaManager.php b/app/src/Lib/Util/SchemaManager.php index 1819ecf86..96e4cf732 100644 --- a/app/src/Lib/Util/SchemaManager.php +++ b/app/src/Lib/Util/SchemaManager.php @@ -94,6 +94,13 @@ public function __construct(?ConsoleIo $io=null, string $connection='default') { } $this->conn = DriverManager::getConnection($cfargs, $config); + + // If a PostgreSQL schema is configured force DBAL to respect it + // by setting the SEARCH_PATH. + if(!empty($cfg['schema']) && ($cfg['driver'] == 'Cake\Database\Driver\Postgres')) { + $this->conn->exec("SET SEARCH_PATH TO " . $cfg['schema'] . ";"); + } + $this->driver = $cfg['driver']; }