From c19a9ac545ce278b0223c48f7e81429b8f19af3d Mon Sep 17 00:00:00 2001 From: Benn Oshrin Date: Thu, 23 Jun 2022 10:34:30 -0400 Subject: [PATCH] Additional commit for Groups (CFM-38), changes for MySQL --- app/config/app.php | 8 +++++ app/resources/locales/en_US/menu.po | 3 ++ app/src/Command/DatabaseCommand.php | 7 ++++- app/src/Command/TransmogrifyCommand.php | 35 ++++++++++++++++++--- app/src/Model/Behavior/TimezoneBehavior.php | 7 ----- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/app/config/app.php b/app/config/app.php index 3692e7ccd..9ea7fcc5e 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -377,6 +377,14 @@ 'file' => 'trace', 'url' => env('LOG_TRACE_URL', null), 'scopes' => ['trace'], + ], + // We define a rules level to record application rule execution + 'rule' => [ + 'className' => 'Cake\Log\Engine\FileLog', + 'path' => LOGS, + 'file' => 'rule', + 'url' => env('LOG_TRACE_URL', null), + 'scopes' => ['rule'], ] ], diff --git a/app/resources/locales/en_US/menu.po b/app/resources/locales/en_US/menu.po index 216e033d6..d8ba9d422 100644 --- a/app/resources/locales/en_US/menu.po +++ b/app/resources/locales/en_US/menu.po @@ -27,6 +27,9 @@ msgid "co.configuration" msgstr "Configuration" +msgid "co.groups" +msgstr "Groups" + msgid "co.people" msgstr "People" diff --git a/app/src/Command/DatabaseCommand.php b/app/src/Command/DatabaseCommand.php index 76c7f6b1f..32ad6ea73 100644 --- a/app/src/Command/DatabaseCommand.php +++ b/app/src/Command/DatabaseCommand.php @@ -114,9 +114,14 @@ public function execute(Arguments $args, ConsoleIo $io) { 'user' => $cfg['username'], 'password' => $cfg['password'], 'host' => $cfg['host'], - 'driver' => ($cfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "pdo_mysql") + 'driver' => ($cfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "mysqli") ]; + // For MySQL SSL + if(!empty($cfg['ssl_ca'])) { + $cfargs['ssl_ca'] = $cfg['ssl_ca']; + } + $conn = DriverManager::getConnection($cfargs, $config); $schema = new Schema(); diff --git a/app/src/Command/TransmogrifyCommand.php b/app/src/Command/TransmogrifyCommand.php index 9af1f8f70..940b91894 100644 --- a/app/src/Command/TransmogrifyCommand.php +++ b/app/src/Command/TransmogrifyCommand.php @@ -320,6 +320,8 @@ class TransmogrifyCommand extends Command { // Make some objects more easily accessible protected $inconn = null; protected $outconn = null; + // Cache the driver for ease of workarounds + protected $outdriver = null; /** * Build an Option Parser. @@ -439,9 +441,15 @@ public function execute(Arguments $args, ConsoleIo $io) { 'user' => $incfg['username'], 'password' => $incfg['password'], 'host' => $incfg['host'], - 'driver' => ($incfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "pdo_mysql") + 'driver' => ($incfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "mysqli") ]; + // For MySQL SSL + if(!empty($incfg['ssl_ca'])) { + // mysqli supports SSL configuration + $cargs['ssl_ca'] = $incfg['ssl_ca']; + } + $this->inconn = DriverManager::getConnection($cargs, $inconfig); $outconfig = new \Doctrine\DBAL\Configuration(); @@ -451,10 +459,17 @@ public function execute(Arguments $args, ConsoleIo $io) { 'user' => $outcfg['username'], 'password' => $outcfg['password'], 'host' => $outcfg['host'], - 'driver' => ($outcfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "pdo_mysql") + 'driver' => ($outcfg['driver'] == 'Cake\Database\Driver\Postgres' ? "pdo_pgsql" : "mysqli") ]; + // For MySQL SSL + if(!empty($outcfg['ssl_ca'])) { + // mysqli supports SSL configuration + $cargs['ssl_ca'] = $outcfg['ssl_ca']; + } + $this->outconn = DriverManager::getConnection($cargs, $outconfig); + $this->outdriver = $cargs['driver']; // We accept a list of table names, mostly for testing purposes $atables = $args->getArguments(); @@ -516,7 +531,9 @@ public function execute(Arguments $args, ConsoleIo $io) { $this->mapFields($t, $row); - $this->outconn->insert($t, $row); + // We prefix the database to the table to avoid having to quote + // table names that match (MySQL) reserved keywords (in particular "groups") + $this->outconn->insert($outcfg['database'] . '.' . $t, $row); $this->cacheResults($t, $row); @@ -566,7 +583,11 @@ public function execute(Arguments $args, ConsoleIo $io) { // Strictly speaking we should use prepared statements, but we control the // data here, and also we're executing a maintenance operation (so query // optimization is less important) - $outsql = "ALTER SEQUENCE " . $t . "_id_seq RESTART WITH " . $max; + if($this->outdriver == 'mysqli') { + $outsql = "ALTER TABLE `" . $t . "` AUTO_INCREMENT = " . $max; + } else { + $outsql = "ALTER SEQUENCE " . $t . "_id_seq RESTART WITH " . $max; + } $this->outconn->executeQuery($outsql); // Run any post processing functions for the table. @@ -636,7 +657,11 @@ protected function fixBooleans(string $table, array &$row) { // this issue: https://github.com/doctrine/dbal/issues/1847 // We need to (more generically than this hack) convert from boolean to char // to avoid errors on insert - $row[$a] = ($row[$a] ? 't' : 'f'); + if($this->outdriver == 'mysqli') { + $row[$a] = ($row[$a] ? '1' : '0'); + } else { + $row[$a] = ($row[$a] ? 't' : 'f'); + } } } } diff --git a/app/src/Model/Behavior/TimezoneBehavior.php b/app/src/Model/Behavior/TimezoneBehavior.php index 734e5ce54..17f9306ad 100644 --- a/app/src/Model/Behavior/TimezoneBehavior.php +++ b/app/src/Model/Behavior/TimezoneBehavior.php @@ -72,13 +72,6 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti } } } -// XXX -// - adjust back after find -// - afterfind isn't a thing anymore, perhaps use a mutator? -// - note that's on the entity, not the table, so we can't just use the Behavior, maybe need a trait -// - maybe move this behavior to a trait on the entity and do this entirely with accessors and mutators? -// - or on retrieve we should assert values are UTC then convert in the view -// https://stackoverflow.com/questions/49280448/how-to-handle-users-timezone-and-utc-sync-between-application-and-database-in-c } /**