From 3111965c28b3fae2acfe6f0b2193fc7641df1ec5 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 4 Jul 2019 12:08:14 +1200 Subject: [PATCH] Deprecated alterFieldSQL This function is only used by one place and feels like the wrong separation of concerns - ie we have a single create action & a multiple create action. They both need to share the generation of the sql to run but can handle the batching of that & rebuilding of triggers etc separately --- CRM/Core/BAO/CustomField.php | 40 ++++++++++++++++++- CRM/Core/BAO/SchemaHandler.php | 2 +- .../CRM/Core/BAO/SchemaHandlerTest.php | 4 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 1dc707ecd6..b893873e96 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1650,9 +1650,47 @@ SELECT $columnName * @param bool $triggerRebuild */ public static function createField($field, $operation, $indexExist = FALSE, $triggerRebuild = TRUE) { + $params = [ + 'table_name' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name'), + ]; + $sql = str_repeat(' ', 8); + $sql .= "ALTER TABLE {$params['table_name']}"; + $sql .= self::getAlterFieldSQL($field, $operation, $params, $indexExist); + + // CRM-7007: do not i18n-rewrite this query + CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE); + + $config = CRM_Core_Config::singleton(); + if ($config->logging) { + // CRM-16717 not sure why this was originally limited to add. + // For example custom tables can have field length changes - which need to flow through to logging. + // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?) + if ($operation == 'add' || $operation == 'modify') { + $logging = new CRM_Logging_Schema(); + $logging->fixSchemaDifferencesFor($params['table_name'], [trim(strtoupper($operation)) => [$params['name']]]); + } + } + + if ($triggerRebuild) { + Civi::service('sql_triggers')->rebuild($params['table_name'], TRUE); + } + + } + + /** + * @param CRM_Core_DAO_CustomField $field + * @param string $operation + * @param array $params + * @param bool $indexExist + * + * @return bool + */ + public static function getAlterFieldSQL($field, $operation, &$params, $indexExist = FALSE) { $params = self::prepareCreateParams($field, $operation); + // lets suppress the required flag, since that can cause sql issue + $params['required'] = FALSE; - CRM_Core_BAO_SchemaHandler::alterFieldSQL($params, $indexExist, $triggerRebuild); + return CRM_Core_BAO_SchemaHandler::getFieldAlterSQL($params, $indexExist); } /** diff --git a/CRM/Core/BAO/SchemaHandler.php b/CRM/Core/BAO/SchemaHandler.php index c67f3744e8..a8bc7c3da7 100644 --- a/CRM/Core/BAO/SchemaHandler.php +++ b/CRM/Core/BAO/SchemaHandler.php @@ -295,7 +295,7 @@ ALTER TABLE {$tableName} * @return bool */ public static function alterFieldSQL($params, $indexExist = FALSE, $triggerRebuild = TRUE) { - + CRM_Core_Error::deprecatedFunctionWarning('function no longer in use / supported'); // lets suppress the required flag, since that can cause sql issue $params['required'] = FALSE; diff --git a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php index f65fc65430..a5b2d6634a 100644 --- a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php +++ b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php @@ -318,7 +318,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase { ]; // drop col1 - CRM_Core_BAO_SchemaHandler::alterFieldSQL($alterParams, FALSE, TRUE); + CRM_Core_DAO::executeQuery(CRM_Core_BAO_SchemaHandler::buildFieldChangeSql($alterParams, FALSE)); $create_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE civicrm_test_drop_column"); while ($create_table->fetch()) { @@ -328,7 +328,7 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase { // drop col2 $alterParams['name'] = 'col2'; - CRM_Core_BAO_SchemaHandler::alterFieldSQL($alterParams, FALSE, TRUE); + CRM_Core_DAO::executeQuery(CRM_Core_BAO_SchemaHandler::buildFieldChangeSql($alterParams, FALSE)); $create_table = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE civicrm_test_drop_column"); while ($create_table->fetch()) { -- 2.25.1