From c3eff32d0eeac52dfb5fafcccd98313b27c7f3fa Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 16 Nov 2020 17:36:15 -0500 Subject: [PATCH] don't assume tablespec exists --- CRM/Logging/Schema.php | 11 +---- tests/phpunit/CRM/Logging/SchemaTest.php | 58 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 48ff217fe3..ab254d86a3 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -805,18 +805,11 @@ COLS; // - drop non-column rows of the query (keys, constraints, etc.) // - set the ENGINE to the specified engine (default is INNODB) // - add log-specific columns (at the end of the table) - $mysqlEngines = []; - $engines = CRM_Core_DAO::executeQuery("SHOW ENGINES"); - while ($engines->fetch()) { - if ($engines->Support === 'YES' || $engines->Support === 'DEFAULT') { - $mysqlEngines[] = $engines->Engine; - } - } $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); $query = preg_replace("/^ [^`].*$/m", '', $query); - $engine = strtoupper(CRM_Utils_Array::value('engine', $this->logTableSpec[$table], self::ENGINE)); - $engine .= " " . CRM_Utils_Array::value('engine_config', $this->logTableSpec[$table]); + $engine = strtoupper(empty($this->logTableSpec[$table]['engine']) ? self::ENGINE : $this->logTableSpec[$table]['engine']); + $engine .= " " . ($this->logTableSpec[$table]['engine_config'] ?? ''); if (strpos($engine, 'ROW_FORMAT') !== FALSE) { $query = preg_replace("/ROW_FORMAT=\w+/m", '', $query); } diff --git a/tests/phpunit/CRM/Logging/SchemaTest.php b/tests/phpunit/CRM/Logging/SchemaTest.php index 02bf5598ec..5356c14e6d 100644 --- a/tests/phpunit/CRM/Logging/SchemaTest.php +++ b/tests/phpunit/CRM/Logging/SchemaTest.php @@ -375,6 +375,64 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase { $this->assertStringContainsString("`{$custom_field['column_name']}` varchar(768)", $dao->Create_Table); } + /** + * Test creating a table with SchemaHandler::createTable when logging + * is enabled. + */ + public function testCreateTableWithLogging() { + $schema = new CRM_Logging_Schema(); + $schema->enableLogging(); + + CRM_Core_BAO_SchemaHandler::createTable([ + 'name' => 'civicrm_test_table', + 'is_multiple' => FALSE, + 'attributes' => 'ENGINE=InnoDB', + 'fields' => [ + [ + 'name' => 'id', + 'type' => 'int unsigned', + 'primary' => TRUE, + 'required' => TRUE, + 'attributes' => 'AUTO_INCREMENT', + 'comment' => 'Default MySQL primary key', + ], + [ + 'name' => 'activity_id', + 'type' => 'int unsigned', + 'required' => TRUE, + 'comment' => 'FK to civicrm_activity', + 'fk_table_name' => 'civicrm_activity', + 'fk_field_name' => 'id', + 'fk_attributes' => 'ON DELETE CASCADE', + ], + [ + 'name' => 'texty', + 'type' => 'varchar(255)', + 'required' => FALSE, + ], + ], + ]); + $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE civicrm_test_table"); + $dao->fetch(); + // using regex since not sure it's always int(10), so accept int(10), int(11), integer, etc... + $this->assertRegExp('/`id` int(.+) unsigned NOT NULL AUTO_INCREMENT/', $dao->Create_Table); + $this->assertRegExp('/`activity_id` int(.+) unsigned NOT NULL/', $dao->Create_Table); + $this->assertStringContainsString('`texty` varchar(255)', $dao->Create_Table); + $this->assertStringContainsString('ENGINE=InnoDB', $dao->Create_Table); + $this->assertStringContainsString('FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE', $dao->Create_Table); + + // Check log table. + $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_test_table"); + $dao->fetch(); + $this->assertStringNotContainsString('AUTO_INCREMENT', $dao->Create_Table); + // This seems debatable whether `id` should lose its NOT NULL status + $this->assertRegExp('/`id` int(.+) unsigned DEFAULT NULL/', $dao->Create_Table); + $this->assertRegExp('/`activity_id` int(.+) unsigned DEFAULT NULL/', $dao->Create_Table); + $this->assertStringContainsString('`texty` varchar(255)', $dao->Create_Table); + $this->assertStringContainsString('ENGINE=InnoDB', $dao->Create_Table); + $this->assertStringNotContainsString('FOREIGN KEY', $dao->Create_Table); + } + /** * Determine if we are running on MySQL 8 version 8.0.19 or later. * -- 2.25.1