dev/core#1093: Add Custom Fields to Logging Tables
authorCamilo Rodríguez <camilo@compucorp.co.uk>
Thu, 24 Oct 2019 18:34:54 +0000 (13:34 -0500)
committerCamilo Rodriguez <camilo@compucorp.co.uk>
Mon, 28 Oct 2019 12:47:26 +0000 (12:47 +0000)
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

index e5c013ddf7fe1fcfbf66e4378ca42fb0be7ccdb0..f0687c82e7b6c9be98927877a91c07c40bc80e78 100644 (file)
@@ -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();