CRM-16717 consider length of fields when comparing schema when turning logging on
authorEileen McNaughton <eileen@fuzion.co.nz>
Thu, 18 Jun 2015 00:33:28 +0000 (12:33 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Thu, 9 Jul 2015 00:31:48 +0000 (12:31 +1200)
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
CRM/Logging/Schema.php

index a7e04ba442038e6fb9a9de25f8527bbdf3e972f9..e52b3e2bc0dcaa97067ddaa974db29e4f5f82898 100644 (file)
@@ -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'],
index f5446dfe6531d521928bdd2e039f754a99102143..9bc148351452b7ff286df8ef48d40d37064a82b2 100644 (file)
@@ -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;
         }