From dd05020d4ca94f107703d9f410a993e65456e66e Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Sun, 26 May 2019 20:14:59 +0100 Subject: [PATCH] Allow updating ENGINE_CONFIG for System.updatelogtables --- CRM/Logging/Schema.php | 30 ++++++++++++++++++++++++++++-- api/v3/System.php | 22 +++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 149d405516..9ce4883b18 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -304,13 +304,23 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) * Note changing engine & adding hook-defined indexes, but not changing back * to ARCHIVE if engine has not been deliberately set (by hook) and not dropping * indexes. Sysadmin will need to manually intervene to revert to defaults. + * + * @param array $params + * 'updateChangedEngineConfig' - update if the engine config changes, default FALSE + * + * @return int $updateTablesCount */ - public function updateLogTableSchema() { + public function updateLogTableSchema($params = []) { + isset($params['updateChangedEngineConfig']) ? NULL : $params['updateChangedEngineConfig'] = FALSE; + $updateLogConn = FALSE; + $updatedTablesCount = 0; foreach ($this->logs as $mainTable => $logTable) { $alterSql = []; $tableSpec = $this->logTableSpec[$mainTable]; - if (isset($tableSpec['engine']) && strtoupper($tableSpec['engine']) != $this->getEngineForLogTable($logTable)) { + $engineChanged = isset($tableSpec['engine']) && (strtoupper($tableSpec['engine']) != $this->getEngineForLogTable($logTable)); + $engineConfigChanged = isset($tableSpec['engine_config']) && (strtoupper($tableSpec['engine_config']) != $this->getEngineConfigForLogTable($logTable)); + if ($engineChanged || ($engineConfigChanged && $params['updateChangedEngineConfig'])) { $alterSql[] = "ENGINE=" . $tableSpec['engine'] . " " . CRM_Utils_Array::value('engine_config', $tableSpec); } if (!empty($tableSpec['indexes'])) { @@ -334,11 +344,13 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) } if (!empty($alterSql)) { CRM_Core_DAO::executeQuery("ALTER TABLE {$this->db}.{$logTable} " . implode(', ', $alterSql), [], TRUE, NULL, FALSE, FALSE); + $updatedTablesCount++; } } if ($updateLogConn) { civicrm_api3('Setting', 'create', ['logging_uniqueid_date' => date('Y-m-d H:i:s')]); } + return $updatedTablesCount; } /** @@ -355,6 +367,20 @@ AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) ", [1 => [$table, 'String'], 2 => [$this->db, 'String']])); } + /** + * Get the engine config for the given table. + * + * @param string $table + * + * @return string + */ + public function getEngineConfigForLogTable($table) { + return strtoupper(CRM_Core_DAO::singleValueQuery(" + SELECT CREATE_OPTIONS FROM information_schema.tables WHERE TABLE_NAME = %1 + AND table_schema = %2 + ", [1 => [$table, 'String'], 2 => [$this->db, 'String']])); + } + /** * Get all the indexes in the table. * diff --git a/api/v3/System.php b/api/v3/System.php index b15a471bbd..3523877682 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -394,10 +394,26 @@ function _civicrm_api3_system_get_whitelist($whitelistFile) { * This updates the engine type if defined in the hook and changes the field type * for log_conn_id to reflect CRM-18193. */ -function civicrm_api3_system_updatelogtables() { +function civicrm_api3_system_updatelogtables($params) { $schema = new CRM_Logging_Schema(); - $schema->updateLogTableSchema(); - return civicrm_api3_create_success(1); + $updatedTablesCount = $schema->updateLogTableSchema($params); + return civicrm_api3_create_success($updatedTablesCount); +} + +/** + * Adjust Metadata for Flush action. + * + * The metadata is used for setting defaults, documentation & validation. + * + * @param array $params + * Array of parameters determined by getfields. + */ +function _civicrm_api3_system_updatelogtables_spec(&$params) { + $params['updateChangedEngineConfig'] = [ + 'title' => 'Update Engine Config if changed?', + 'description' => 'By default, we only update if the ENGINE has changed, set this to TRUE to update if the ENGINE_CONFIG has changed.', + 'type' => CRM_Utils_Type::T_BOOLEAN, + ]; } /** -- 2.25.1