From a599e68c8812d04811ca35abeec0436b27ac8274 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 8 Mar 2018 11:20:01 +1100 Subject: [PATCH] CRM-21835 Gracefully switch Log Table Engines if Archive is not avaliable --- CRM/Logging/Schema.php | 12 ++++++++++-- tests/phpunit/CRM/Logging/SchemaTest.php | 11 +++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index de3f13bccc..3ecdfc01d8 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -703,12 +703,20 @@ COLS; // - prepend the name with log_ // - drop AUTO_INCREMENT columns // - drop non-column rows of the query (keys, constraints, etc.) - // - set the ENGINE to the specified engine (default is archive) + // - set the ENGINE to the specified engine (default is archive or if archive is disabled or nor installed 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; + } + } + $logEngine = in_array('ARCHIVE', $mysqlEngines) ? 'ARCHIVE' : 'INNODB'; $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], 'ARCHIVE')); + $engine = strtoupper(CRM_Utils_Array::value('engine', $this->logTableSpec[$table], $logEngine)); $engine .= " " . CRM_Utils_Array::value('engine_config', $this->logTableSpec[$table]); $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=' . $engine . ' ', $query); diff --git a/tests/phpunit/CRM/Logging/SchemaTest.php b/tests/phpunit/CRM/Logging/SchemaTest.php index 889cdaf3d0..27f72364b9 100644 --- a/tests/phpunit/CRM/Logging/SchemaTest.php +++ b/tests/phpunit/CRM/Logging/SchemaTest.php @@ -30,4 +30,15 @@ class CRM_Logging_SchemaTest extends CiviUnitTestCase { $this->assertEquals($expectedQuery, CRM_Logging_Schema::fixTimeStampAndNotNullSQL($query)); } + public function testLogEngine() { + $schema = new CRM_Logging_Schema(); + $schema->enableLogging(); + $log_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE log_civicrm_acl"); + while ($log_table->fetch()) { + $this->assertRegexp('/ENGINE=ARCHIVE/', $log_table->Create_Table); + } + $schema->disableLogging(); + $schema->dropAllLogTables(); + } + } -- 2.25.1