-- fix for CRM-13417
authorRavish Nair <ravish.nair@webaccess.co.in>
Fri, 27 Sep 2013 11:36:25 +0000 (17:06 +0530)
committerRavish Nair <ravish.nair@webaccess.co.in>
Fri, 27 Sep 2013 11:36:25 +0000 (17:06 +0530)
----------------------------------------
* CRM-13417: Dedupe Rule Creation error trapping fails on length selections for substrings of numeric fields or out of range numbers for text fields
  http://issues.civicrm.org/jira/browse/CRM-13417

CRM/Contact/Form/DedupeRules.php

index dd4c322465359d73effccd892da23da72c28e3a9..ea8154b811692cfe7aa56da3624c7b1a080c6cd1 100644 (file)
@@ -265,6 +265,25 @@ UPDATE civicrm_dedupe_rule_group
         if (!isset($substrLenghts[$table])) {
           $substrLenghts[$table] = array();
         }
+
+        //CRM-13417 to avoid fatal error "Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys, 1089"
+        $daoObj = new CRM_Core_DAO();
+        $database = $daoObj->database();
+        $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS
+          WHERE TABLE_SCHEMA = '{$database}' AND
+          TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';";
+        $dao = CRM_Core_DAO::executeQuery($schemaQuery);
+
+        while ($dao->fetch()) {
+          // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes.
+          if ($dao->COLUMN_NAME == $field && !in_array($dao->DATA_TYPE, array('char', 'varchar', 'binary', 'varbinary', 'text', 'blob'))) {
+            $length = NULL;
+          }
+          elseif ($dao->COLUMN_NAME == $field && !empty($dao->CHARACTER_MAXIMUM_LENGTH) && ($length > $dao->CHARACTER_MAXIMUM_LENGTH)) {
+            //set the length to CHARACTER_MAXIMUM_LENGTH in case the length provided by the user is greater than the limit
+            $length = $dao->CHARACTER_MAXIMUM_LENGTH;
+          }
+        }
         $substrLenghts[$table][$field] = $length;
       }
     }