From cf0bd1e1dd7561c67f72881acfffebb91a20ae26 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 18 Jun 2015 12:33:28 +1200 Subject: [PATCH] CRM-16717 consider length of fields when comparing schema when turning logging on If a field length has increased we need to ensure the field length of the log table is also increased --- CRM/Core/BAO/CustomField.php | 4 ++-- CRM/Logging/Schema.php | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index a7e04ba442..e52b3e2bc0 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -251,8 +251,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { self::fixOptionGroups($params['id'], $params['option_group_id']); } - // if we dont have a default value - // retrive it from one of the other custom fields which use this option group + // if we do not have a default value + // retrieve it from one of the other custom fields which use this option group if (empty($params['default_value'])) { //don't insert only value separator as default value, CRM-4579 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php index f5446dfe65..9bc1483514 100644 --- a/CRM/Logging/Schema.php +++ b/CRM/Logging/Schema.php @@ -396,11 +396,11 @@ AND TABLE_NAME LIKE 'log_civicrm_%' $dao = new CRM_Contact_DAO_Contact(); $civiDB = $dao->_database; } - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); + CRM_Core_TemporaryErrorScope::ignoreException(); // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster // than firing query for every given table. $query = " -SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT +SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; $dao = CRM_Core_DAO::executeQuery($query); @@ -417,6 +417,9 @@ WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; 'IS_NULLABLE' => $dao->IS_NULLABLE, 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT, ); + if (($first = strpos($dao->COLUMN_TYPE, '(')) != 0) { + $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME]['LENGTH'] = substr($dao->COLUMN_TYPE, $first, strpos($dao->COLUMN_TYPE, ')')); + } } } return $columnSpecs[$table]; @@ -448,7 +451,10 @@ WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]); if (!empty($specDiff) && $col != 'id' && !array_key_exists($col, $diff['ADD'])) { // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors - if ($civiTableSpecs[$col]['DATA_TYPE'] != CRM_Utils_Array::value('DATA_TYPE', $logTableSpecs[$col])) { + if ($civiTableSpecs[$col]['DATA_TYPE'] != CRM_Utils_Array::value('DATA_TYPE', $logTableSpecs[$col]) + // We won't alter the log if the length is decreased in case some of the existing data won't fit. + || CRM_Utils_Array::value('LENGTH', $civiTableSpecs[$col]) > CRM_Utils_Array::value('LENGTH', $logTableSpecs[$col]) + ) { // if data-type is different, surely consider the column $diff['MODIFY'][] = $col; } -- 2.25.1