Skip to content

Commit

Permalink
ensure that the indexed column is not an FK before deleting the index
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmcandrew committed Jun 4, 2018
1 parent 0d4655e commit 7ab8180
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CRM/Core/BAO/SchemaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ public static function buildSearchIndexSQL(&$params, $separator, $prefix, $index

//create index only for searchable fields during ADD,
//create index only if field is become searchable during MODIFY,
//drop index only if field is no more searchable and index was exist.
//drop index only if field is no longer searchable and it does not reference
//a forgein key (and indexExist is true)
if (!empty($params['searchable']) && !$indexExist) {
$sql .= $separator;
$sql .= str_repeat(' ', 8);
$sql .= $prefix;
$sql .= "INDEX_{$params['name']} ( {$params['name']} )";
}
elseif (empty($params['searchable']) && $indexExist) {
elseif (empty($params['searchable']) && empty($params['fk_table_name']) && $indexExist) {
$sql .= $separator;
$sql .= str_repeat(' ', 8);
$sql .= "DROP INDEX INDEX_{$params['name']}";
Expand Down
27 changes: 22 additions & 5 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,28 @@ public function getCustomFieldKeys($getFieldsResult) {
return $r;
}

/**
* This test is designed to ensure that when a custom field is updated via the
* API, params that are not supplied do not revert to the defaults. This was
* happening with, for example, is_searchable
*/
public function testMakeSearchableContactReferenceFieldUnsearchable() {
$customGroup = $this->customGroupCreate(array(
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
));
$params = array(
'name' => 'testCustomField',
'label' => 'testCustomField',
'custom_group_id' => 'testCustomGroup',
'data_type' => 'ContactReference',
'html_type' => 'Autocomplete-Select',
'is_searchable' => '1',
);
$result = $this->callAPISuccess('CustomField', 'create', $params);
$params = [
'id' => $result['id'],
'is_searchable' => 0,
];
$result = $this->callAPISuccess('CustomField', 'create', $params);
}

public function testDisableSearchableContactReferenceField() {
$customGroup = $this->customGroupCreate(array(
'name' => 'testCustomGroup',
Expand Down

0 comments on commit 7ab8180

Please sign in to comment.