diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index fe67d9b944bb..8486a18bc826 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -17,7 +17,10 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { /** + * Check defined indices exist. + * * @return array + * @throws \CiviCRM_API3_Exception */ public function checkIndices() { $messages = []; @@ -26,7 +29,7 @@ public function checkIndices() { // unreliable. Bypass this check until CRM-20817 and CRM-20533 are resolved. return $messages; - $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices(); + $missingIndices = civicrm_api3('System', 'getmissingindices', [])['values']; if ($missingIndices) { $html = ''; foreach ($missingIndices as $tableName => $indices) { diff --git a/api/v3/System.php b/api/v3/System.php index 6b593546a661..c91583fc0f7e 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -417,6 +417,16 @@ function civicrm_api3_system_updateindexes() { return civicrm_api3_create_success(1); } +/** + * Get an array of indices that should be defined but are not. + * + * @return array + */ +function civicrm_api3_system_getmissingindices() { + $indices = CRM_Core_BAO_SchemaHandler::getMissingIndices(FALSE); + return civicrm_api3_create_success($indices); +} + /** * Creates missing log tables. * diff --git a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php index 79782ea450bb..d8fb893cf596 100644 --- a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php +++ b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php @@ -169,7 +169,7 @@ public function testSafeDropForeignKey($tableName, $key) { */ public function testGetMissingIndices() { $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices(); - $this->assertTrue(empty($missingIndices)); + $this->assertEmpty($missingIndices); } /** @@ -211,10 +211,15 @@ public function testCreateMissingIndices() { /** * Check there are no missing indices + * + * @throws \CRM_Core_Exception */ public function testReconcileMissingIndices() { CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contact DROP INDEX index_sort_name'); $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices(); + // Check the api also retrieves them. + $missingIndicesAPI = $this->callAPISuccess('System', 'getmissingindices', [])['values']; + $this->assertEquals($missingIndices, $missingIndicesAPI); $this->assertEquals([ 'civicrm_contact' => [ [ @@ -227,7 +232,7 @@ public function testReconcileMissingIndices() { ], $missingIndices); $this->callAPISuccess('System', 'updateindexes', []); $missingIndices = CRM_Core_BAO_SchemaHandler::getMissingIndices(); - $this->assertTrue(empty($missingIndices)); + $this->assertEmpty($missingIndices); } /**