From aca2de91a82376a5c4fc7438b257e803ce78c6d1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 24 Nov 2013 20:53:14 -0800 Subject: [PATCH] CRM-13822 - Ensure mysql phone function exists --- CRM/Core/BAO/Phone.php | 10 ++++++++++ CRM/Core/DAO.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CRM/Core/BAO/Phone.php b/CRM/Core/BAO/Phone.php index 14572de4e2..e12d84c435 100644 --- a/CRM/Core/BAO/Phone.php +++ b/CRM/Core/BAO/Phone.php @@ -45,6 +45,9 @@ class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone { * @param array $params input parameters */ static function create($params) { + // Ensure mysql phone function exists + CRM_Core_DAO::checkSqlFunctionsExist(); + if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || // if id is set & is_primary isn't we can assume no change empty($params['id']) @@ -66,6 +69,9 @@ class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone { * @static */ static function add(&$params) { + // Ensure mysql phone function exists + CRM_Core_DAO::checkSqlFunctionsExist(); + $hook = empty($params['id']) ? 'create' : 'edit'; CRM_Utils_Hook::pre($hook, 'Phone', CRM_Utils_Array::value('id', $params), $params); @@ -229,6 +235,8 @@ ORDER BY ph.is_primary DESC, phone_id ASC "; if (!$optionId) { return; } + // Ensure mysql phone function exists + CRM_Core_DAO::checkSqlFunctionsExist(); $tables = array( 'civicrm_phone', @@ -252,6 +260,8 @@ ORDER BY ph.is_primary DESC, phone_id ASC "; * Call common delete function */ static function del($id) { + // Ensure mysql phone function exists + CRM_Core_DAO::checkSqlFunctionsExist(); return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id); } } diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index c03f87f9ee..214a937846 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -67,6 +67,8 @@ class CRM_Core_DAO extends DB_DataObject { */ static $_factory = NULL; + static $_checkedSqlFunctionsExist = FALSE; + /** * Class constructor * @@ -1558,6 +1560,23 @@ SELECT contact_id self::createTriggers($info); } + /** + * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests + * @see http://issues.civicrm.org/jira/browse/CRM-13822 + * TODO: Alternative solutions might be + * * Stop using functions and find another way to strip numeric characters from phones + * * Give better error messages (currently a missing fn fatals with "unknown error") + */ + static function checkSqlFunctionsExist() { + if (!self::$_checkedSqlFunctionsExist) { + self::$_checkedSqlFunctionsExist = TRUE; + $dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'"); + if (!$dao->fetch()) { + self::triggerRebuild(); + } + } + } + /** * Wrapper function to drop triggers * -- 2.25.1