Merge pull request #15927 from eileenmcnaughton/event_form
[civicrm-core.git] / CRM / Logging / Schema.php
index 64d5537c716f51b264a75252493492ebb5a293a8..176020b7056ff0d01bce3036170544d78b1056e7 100644 (file)
@@ -430,6 +430,17 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
       $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) {
@@ -451,9 +462,21 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
       }
     }
 
+    $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.
    *