From 47e86ed376addc381a8f90b9cdaa41df42f614e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Camilo=20Rodr=C3=ADguez?= Date: Thu, 24 Oct 2019 13:34:54 -0500 Subject: [PATCH] dev/core#1093: Add Custom Fields to Logging Tables BulkSave method was adding fields to custom group table, but not to custom group logging table. If these tables were updated on installation, before all the schema is synced bewtween main and logging tables, fatal errors would be thrown, as triggers tried to insert info into log tables missing required fields. Fixed by building log table after all fields are added to main table, and before rebuilding triggers. --- CRM/Core/BAO/CustomField.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index e5c013ddf7..f0687c82e7 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -176,7 +176,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * Default parameters to be be merged into each of the params. */ public static function bulkSave($bulkParams, $defaults = []) { - $sql = $tables = $customFields = []; + $addedColumns = $sql = $tables = $customFields = []; foreach ($bulkParams as $index => $fieldParams) { $params = array_merge($defaults, $fieldParams); $customField = self::createCustomFieldRecord($params); @@ -194,11 +194,19 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $params['table_name'] = $tables[$params['custom_group_id']]; } $sql[$params['table_name']][] = $fieldSQL; + $addedColumns[$params['table_name']][] = $customField->name; $customFields[$index] = $customField; } + foreach ($sql as $tableName => $statements) { // CRM-7007: do not i18n-rewrite this query CRM_Core_DAO::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE); + + if (CRM_Core_Config::singleton()->logging) { + $logging = new CRM_Logging_Schema(); + $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]); + } + Civi::service('sql_triggers')->rebuild($params['table_name'], TRUE); } CRM_Utils_System::flushCache(); -- 2.25.1