Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api to check for missing indices #16145

Merged
merged 1 commit into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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