ensure that the indexed column is not an FK before deleting the index
authorMichael McAndrew <michaelmcandrew@thirdsectordesign.org>
Thu, 31 May 2018 20:39:17 +0000 (21:39 +0100)
committerMichael McAndrew <michaelmcandrew@thirdsectordesign.org>
Mon, 4 Jun 2018 10:20:03 +0000 (11:20 +0100)
CRM/Core/BAO/SchemaHandler.php
tests/phpunit/api/v3/CustomFieldTest.php

index ef28021a7c9ceb89f79cf880a0ac4ec293d306be..ab58a8aa4b25f6efa0d77af505661b3d9e16430a 100644 (file)
@@ -189,14 +189,15 @@ class CRM_Core_BAO_SchemaHandler {
 
     //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']}";
index abb5c19f1eb2729495f6401259dcd1d79c7e91c4..a84dcf480667e5c6e62f42bc26145330fc3c2a6b 100644 (file)
@@ -569,11 +569,28 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase {
     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',