Skip to content

Commit

Permalink
MDL-67886 tool_xmldb: checks for extra indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 22, 2020
1 parent 47f3fa1 commit fad6100
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
86 changes: 68 additions & 18 deletions admin/tool/xmldb/actions/check_indexes/check_indexes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ function init() {

// Get needed strings
$this->loadStrings(array(
'extraindexesfound' => 'tool_xmldb',
'missing' => 'tool_xmldb',
'key' => 'tool_xmldb',
'index' => 'tool_xmldb',
'missingindexes' => 'tool_xmldb',
'nomissingindexesfound' => 'tool_xmldb',
'nomissingorextraindexesfound' => 'tool_xmldb',
'yesextraindexesfound' => 'tool_xmldb',
'yesmissingindexesfound' => 'tool_xmldb',
));
}
Expand All @@ -58,6 +60,7 @@ protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
$dbman = $DB->get_manager();

$o = '';
$dbindexes = $DB->get_indexes($xmldb_table->getName());
$missing_indexes = array();

// Keys
Expand Down Expand Up @@ -88,6 +91,7 @@ protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
$this->remove_index_from_dbindex($dbindexes, $xmldb_index);
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
// Add the missing index to the list
Expand All @@ -109,6 +113,7 @@ protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
$this->remove_index_from_dbindex($dbindexes, $xmldb_index);
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
// Add the missing index to the list
Expand All @@ -122,40 +127,71 @@ protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
$o.=' </ul>';
}

// Hack - skip for table 'search_simpledb_index' as this plugin adds indexes dynamically on install
// which are not included in install.xml. See search/engine/simpledb/db/install.php.
if ($xmldb_table->getName() != 'search_simpledb_index') {
foreach ($dbindexes as $indexname => $index) {
$missing_indexes[] = $indexname;
}
}

return array($o, $missing_indexes);
}

protected function display_results(array $missing_indexes) {
global $DB;
$dbman = $DB->get_manager();

$missingindexes = [];
$extraindexes = [];

foreach ($missing_indexes as $missingindex) {
if (is_object($missingindex)) {
$missingindexes[] = $missingindex;
} else {
$extraindexes[] = $missingindex;
}
}

$s = '';
$r = '<table class="generaltable boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>';
$r .= ' <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missingindexes) . '</p>';
$r .= ' <p class="centerpara">' . $this->str['extraindexesfound'] . ': ' . count($extraindexes) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';

// If we have found missing indexes inform about them
if (count($missing_indexes)) {
$r.= ' <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
$r.= ' <ul>';
foreach ($missing_indexes as $obj) {
$xmldb_table = $obj->table;
$xmldb_index = $obj->index;
$sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
// If we have found missing indexes or extra indexes inform the user about them.
if (!empty($missingindexes) || !empty($extraindexes)) {
if ($missingindexes) {
$r.= ' <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
$r.= ' <ul>';
foreach ($missingindexes as $obj) {
$xmldb_table = $obj->table;
$xmldb_index = $obj->index;
$sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';

}
$r.= ' </ul>';
// Add the SQL statements (all together)
$r.= '<hr />' . $s;
}
if ($extraindexes) {
$r .= '<p class="centerpara">' . $this->str['yesextraindexesfound'] . '</p>';
$r .= '<ul>';
foreach ($extraindexes as $ei) {
$r .= '<li>' . $ei . '</li>';
}
$r .= '</ul>';
$r .= '<hr />';
}
$r.= ' </ul>';
// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
$r.= ' <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>';
$r .= '<p class="centerpara">' . $this->str['nomissingorextraindexesfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
Expand All @@ -166,4 +202,18 @@ protected function display_results(array $missing_indexes) {

return $r;
}

/**
* Removes an index from the array $dbindexes if it is found.
*
* @param array $dbindexes
* @param xmldb_index $index
*/
private function remove_index_from_dbindex(array &$dbindexes, xmldb_index $index) {
foreach ($dbindexes as $key => $dbindex) {
if ($dbindex['columns'] == $index->getFields()) {
unset($dbindexes[$key]);
}
}
}
}
4 changes: 3 additions & 1 deletion admin/tool/xmldb/lang/en/tool_xmldb.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
$string['enumvaluesincorrect'] = 'Incorrect values for enum field';
$string['expected'] = 'Expected';
$string['extensionrequired'] = 'Sorry - the PHP extension \'{$a}\' is required for this action. Please install the extension if you want to use this feature.';
$string['extraindexesfound'] = 'Extra indexes found';
$string['field'] = 'Field';
$string['fieldnameempty'] = 'Name field empty';
$string['fields'] = 'Fields';
Expand Down Expand Up @@ -157,7 +158,7 @@
$string['new_table_from_mysql'] = 'New table from MySQL';
$string['nofieldsspecified'] = 'No fields specified';
$string['nomasterprimaryuniquefound'] = 'The column(s) that your foreign key references must be included in a primary or unique KEY in the referenced table. Note that the column being in a UNIQUE INDEX is not good enough.';
$string['nomissingindexesfound'] = 'No missing indexes have been found, your DB doesn\'t need further actions.';
$string['nomissingorextraindexesfound'] = 'No missing or extra indexes have been found, your DB doesn\'t need further actions.';
$string['noreffieldsspecified'] = 'No reference fields specified';
$string['noreftablespecified'] = 'Specified reference table not found';
$string['noviolatedforeignkeysfound'] = 'No violated foreign keys found';
Expand Down Expand Up @@ -215,6 +216,7 @@
$string['wrongnumberofreffields'] = 'Wrong number of reference fields';
$string['wrongreservedwords'] = 'Currently used reserved words<br />(note that table names aren\'t important if using $CFG->prefix)';
$string['wrongoraclesemantics'] = 'Wrong Oracle BYTE semantics found';
$string['yesextraindexesfound'] = 'The following additional indexes were found.';
$string['yesmissingindexesfound'] = '<p>Some missing indexes have been found in your DB. Here are their details and the needed SQL statements to be executed with your favourite SQL interface to create all of them. Remember to backup your data first!</p>
<p>After doing that, it\'s highly recommended to execute this utility again to check that no more missing indexes are found.</p>';
$string['yeswrongdefaultsfound'] = '<p>Some inconsistent defaults have been found in your DB. Here are their details and the needed SQL statements to be executed with your favourite SQL interface to fix them all. Remember to backup your data first!</p>
Expand Down

0 comments on commit fad6100

Please sign in to comment.