Skip to content

Commit

Permalink
Merge pull request civicrm#16145 from eileenmcnaughton/index_api
Browse files Browse the repository at this point in the history
Add api to check for missing indices
  • Loading branch information
eileenmcnaughton authored Dec 23, 2019
2 parents 1ca18cc + a5e6535 commit d1dd266
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CRM/Utils/Check/Component/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions api/v3/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 7 additions & 2 deletions tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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' => [
[
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit d1dd266

Please sign in to comment.