mirror of
https://github.com/moodle/moodle.git
synced 2025-03-23 09:00:30 +01:00
MDL-67886 tool_xmldb: checks for extra indexes
This commit is contained in:
parent
47f3fa131a
commit
fad610045b
@ -44,11 +44,13 @@ class check_indexes extends XMLDBCheckAction {
|
||||
|
||||
// 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',
|
||||
));
|
||||
}
|
||||
@ -58,6 +60,7 @@ class check_indexes extends XMLDBCheckAction {
|
||||
$dbman = $DB->get_manager();
|
||||
|
||||
$o = '';
|
||||
$dbindexes = $DB->get_indexes($xmldb_table->getName());
|
||||
$missing_indexes = array();
|
||||
|
||||
// Keys
|
||||
@ -88,6 +91,7 @@ class check_indexes extends XMLDBCheckAction {
|
||||
// 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
|
||||
@ -109,6 +113,7 @@ class check_indexes extends XMLDBCheckAction {
|
||||
// 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
|
||||
@ -122,6 +127,14 @@ class check_indexes extends XMLDBCheckAction {
|
||||
$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);
|
||||
}
|
||||
|
||||
@ -129,33 +142,56 @@ class check_indexes extends XMLDBCheckAction {
|
||||
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">';
|
||||
@ -166,4 +202,18 @@ class check_indexes extends XMLDBCheckAction {
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,7 @@ $string['edit_xml_file'] = 'Edit XML file';
|
||||
$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';
|
||||
@ -157,7 +158,7 @@ $string['newtablefrommysql'] = 'New table from MySQL';
|
||||
$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';
|
||||
@ -215,6 +216,7 @@ $string['wronglengthforenum'] = 'Incorrect length for enum field';
|
||||
$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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user