From 138b4c4cab7314eadf7344326b82b4952ab6c1dc Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 23 Dec 2019 12:44:43 +1300 Subject: [PATCH] Add filter 'tables' on getMissingIndices api call --- CRM/Core/BAO/SchemaHandler.php | 7 ++- api/v3/System.php | 20 ++++++++- .../CRM/Core/BAO/SchemaHandlerTest.php | 43 +++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CRM/Core/BAO/SchemaHandler.php b/CRM/Core/BAO/SchemaHandler.php index 1998806aff..c81ed88043 100644 --- a/CRM/Core/BAO/SchemaHandler.php +++ b/CRM/Core/BAO/SchemaHandler.php @@ -654,15 +654,20 @@ MODIFY {$columnName} varchar( $length ) * @param bool $dropFalseIndices * If set - this function deletes false indices present in the DB which mismatches the expected * values of xml file so that civi re-creates them with correct values using createMissingIndices() function. + * @param array|FALSE $tables + * An optional array of tables - if provided the results will be restricted to these tables. * * @return array * index specifications */ - public static function getMissingIndices($dropFalseIndices = FALSE) { + public static function getMissingIndices($dropFalseIndices = FALSE, $tables = FALSE) { $requiredSigs = $existingSigs = []; // Get the indices defined (originally) in the xml files $requiredIndices = CRM_Core_DAO_AllCoreTables::indices(); $reqSigs = []; + if ($tables !== FALSE) { + $requiredIndices = array_intersect_key($requiredIndices, array_fill_keys($tables, TRUE)); + } foreach ($requiredIndices as $table => $indices) { $reqSigs[] = CRM_Utils_Array::collect('sig', $indices); } diff --git a/api/v3/System.php b/api/v3/System.php index c91583fc0f..7bf1d3f401 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -420,13 +420,29 @@ function civicrm_api3_system_updateindexes() { /** * Get an array of indices that should be defined but are not. * + * @param array $params + * * @return array */ -function civicrm_api3_system_getmissingindices() { - $indices = CRM_Core_BAO_SchemaHandler::getMissingIndices(FALSE); +function civicrm_api3_system_getmissingindices($params) { + $tables = empty($params['tables']) ? FALSE : (array) $params['tables']; + $indices = CRM_Core_BAO_SchemaHandler::getMissingIndices(FALSE, $tables); return civicrm_api3_create_success($indices); } +/** + * Declare metadata for api System.getmissingindices + * + * @param array $params + */ +function _civicrm_api3_system_getmissingindices_spec(&$params) { + $params['tables'] = [ + 'type' => CRM_Utils_Type::T_STRING, + 'api.default' => FALSE, + 'title' => ts('Optional tables filter'), + ]; +} + /** * Creates missing log tables. * diff --git a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php index b39ca18ed7..811e8c4c8d 100644 --- a/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php +++ b/tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php @@ -14,10 +14,21 @@ * * These tests create and drop indexes on the civicrm_uf_join table. The indexes * being added and dropped we assume will never exist. + * * @group headless */ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase { + /** + * Ensure any removed indices are put back. + * + * @throws \CRM_Core_Exception + */ + public function tearDown() { + parent::tearDown(); + $this->callAPISuccess('System', 'updateindexes', []); + } + /** * Test creating an index. * @@ -236,6 +247,38 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase { $this->assertEmpty($missingIndices); } + /** + * Check there are no missing indices + * + * @throws \CRM_Core_Exception + */ + public function testGetMissingIndicesWithTableFilter() { + CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contact DROP INDEX index_sort_name'); + CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution DROP INDEX index_total_amount_receive_date'); + $missingIndices = $this->callAPISuccess('System', 'getmissingindices', [])['values']; + $expected = [ + 'civicrm_contact' => [ + [ + 'name' => 'index_sort_name', + 'field' => ['sort_name'], + 'localizable' => FALSE, + 'sig' => 'civicrm_contact::0::sort_name', + ], + ], + 'civicrm_contribution' => [ + [ + 'name' => 'index_total_amount_receive_date', + 'field' => ['total_amount', 'receive_date'], + 'localizable' => FALSE, + 'sig' => 'civicrm_contribution::0::total_amount::receive_date', + ], + ], + ]; + $this->assertEquals($expected, $missingIndices); + $missingIndices = $this->callAPISuccess('System', 'getmissingindices', ['tables' => ['civicrm_contact']])['values']; + $this->assertEquals(['civicrm_contact' => $expected['civicrm_contact']], $missingIndices); + } + /** * Check for partial indices */ -- 2.25.1