Skip to content

Commit

Permalink
Deprecated alterFieldSQL
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eileenmcnaughton committed Jul 5, 2019
1 parent f4bb3e4 commit 7aa6ebb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
40 changes: 39 additions & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1650,9 +1650,47 @@ public static function defaultCustomTableSchema($params) {
* @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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/SchemaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public static function buildForeignKeySQL($params, $separator, $prefix, $tableNa
* @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;

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function testDropColumn() {
];

// 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()) {
Expand All @@ -328,7 +328,7 @@ public function testDropColumn() {

// 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()) {
Expand Down

0 comments on commit 7aa6ebb

Please sign in to comment.