From 363c450b7b3c490603565b691f0f8673cdaa5951 Mon Sep 17 00:00:00 2001 From: Camilo Rodriguez Date: Mon, 11 Nov 2019 16:00:30 +0000 Subject: [PATCH] dev/core#1383: Fix Re-Installation of Extensions With Logging Enabled When uninstalling an extension, logging tables associated to custom groups and fields will not be deleted. On re-installation, addition of custom fields will cause DB errors to be thrown, as columns existing on logging tables are tried to be created again (they already exist on logging tables). Fixed by checking if the column exists on log table before trying to create it, treating it as a modification of the schema if it exists. --- CRM/Logging/Schema.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index ea6da26505b..647edcfbf49 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -446,6 +446,17 @@ public function fixSchemaDifferencesFor($table, $cols = []) { $cols = $this->columnsWithDiffSpecs($table, "log_$table"); } + // If a column that already exists on logging table is being added, we + // should treat it as a modification. + $this->resetSchemaCacheForTable("log_$table"); + $logTableSchema = $this->columnSpecsOf("log_$table"); + foreach ($cols['ADD'] as $colKey => $col) { + if (array_key_exists($col, $logTableSchema)) { + $cols['MODIFY'][] = $col; + unset($cols['ADD'][$colKey]); + } + } + // use the relevant lines from CREATE TABLE to add colums to the log table $create = $this->_getCreateQuery($table); foreach ((['ADD', 'MODIFY']) as $alterType) { @@ -467,9 +478,21 @@ public function fixSchemaDifferencesFor($table, $cols = []) { } } + $this->resetSchemaCacheForTable("log_$table"); + return TRUE; } + /** + * Resets schema cache for the given table. + * + * @param string $table + * Name of the table. + */ + private function resetSchemaCacheForTable($table) { + unset(\Civi::$statics[__CLASS__]['columnSpecs'][$table]); + } + /** * Get query table. *