Ejegg has submitted this change and it was merged. Change subject: CRM-18104 Hook for defining log tables. ......................................................................
CRM-18104 Hook for defining log tables. Allows defining ENGINE and any indexes Submitted for Core in https://github.com/civicrm/civicrm-core/pull/7988 Bug: T130161 Change-Id: I29ae8c8d5f8062e46dc4d06a4433684ba8bf1cec --- M CRM/Logging/Schema.php M CRM/Utils/Hook.php 2 files changed, 45 insertions(+), 2 deletions(-) Approvals: Ejegg: Looks good to me, approved jenkins-bot: Verified diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index 4169dd8..bee5eca 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -54,6 +54,18 @@ /** + * Specifications of all log table including + * - engine (default is archive, if not set.) + * - indexes (default is none and they cannot be added unless engine is innodb. If they are added and + * engine is not set to innodb an exception will be thrown since quiet acquiescence is easier to miss). + * - exceptions (by default those stored in $this->exceptions are included). These are + * excluded from the triggers. + * + * @var array + */ + private $logTableSpec = array(); + + /** * Setting Callback - Validate. * * @param mixed $value @@ -129,6 +141,12 @@ // do not log civicrm_mailing_recipients table, CRM-16193 $this->tables = array_diff($this->tables, array('civicrm_mailing_recipients')); + $this->logTableSpec = array_fill_keys($this->tables, array()); + foreach ($this->exceptions as $tableName => $fields) { + $this->logTableSpec[$tableName]['exceptions'] = $fields; + } + CRM_Utils_Hook::logTableSpec($this->logTableSpec); + $this->tables = array_keys($this->logTableSpec); if (defined('CIVICRM_LOGGING_DSN')) { $dsn = DB::parseDSN(CIVICRM_LOGGING_DSN); @@ -549,6 +567,15 @@ log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') COLS; + if (!empty($this->logTableSpec[$table]['indexes'])) { + foreach ($this->logTableSpec[$table]['indexes'] as $indexName => $indexSpec) { + if (is_array($indexSpec)) { + $indexSpec = implode(" , ", $indexSpec); + } + $cols .= ", INDEX {$indexName}($indexSpec)"; + } + } + // - prepend the name with log_ // - drop AUTO_INCREMENT columns // - drop non-column rows of the query (keys, constraints, etc.) @@ -557,7 +584,8 @@ $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); - $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query); + $engine = strtoupper(CRM_Utils_Array::value('engine', $this->logTableSpec[$table], 'ARCHIVE')); + $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=' . $engine . ' ', $query); // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, // so there's no need for a default timestamp and therefore we remove such default timestamps @@ -650,8 +678,9 @@ // only do the change if any data has changed $cond = array(); foreach ($columns as $column) { + $tableExceptions = array_key_exists('exceptions', $this->logTableSpec[$table]) ? $this->logTableSpec[$table]['exceptions'] : array(); // ignore modified_date changes - if ($column != 'modified_date' && !in_array($column, CRM_Utils_Array::value($table, $this->exceptions, array()))) { + if ($column != 'modified_date' && !in_array($column, $tableExceptions)) { $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; } } diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 2d7dca8..9cd1a93 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -1426,6 +1426,20 @@ 'civicrm_triggerInfo' ); } + /** + * This hook allows changes to the spec of which tables to log. + * + * @param array $logTableSpec + * + * @return mixed + */ + public static function logTableSpec(&$logTableSpec) { + return self::singleton()->invoke(1, $logTableSpec, $_nullObject, + self::$_nullObject, self::$_nullObject, self::$_nullObject, + self::$_nullObject, + 'civicrm_logTableSpec' + ); + } /** * This hook is called when a module-extension is installed. -- To view, visit https://gerrit.wikimedia.org/r/277911 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I29ae8c8d5f8062e46dc4d06a4433684ba8bf1cec Gerrit-PatchSet: 3 Gerrit-Project: wikimedia/fundraising/crm/civicrm Gerrit-Branch: master Gerrit-Owner: Eileen <[email protected]> Gerrit-Reviewer: Awight <[email protected]> Gerrit-Reviewer: Ejegg <[email protected]> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
