diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 5103e0ba784..5247e49429e 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -30,6 +30,51 @@ defined('MOODLE_INTERNAL') || die(); +/** + * Returns all non-view and non-temp tables with sane names. + * Prints list of non-supported tables using $OUTPUT->notification() + * + * @return array + */ +function upgrade_mysql_get_supported_tables() { + global $OUTPUT, $DB; + + $tables = array(); + $patprefix = str_replace('_', '\\_', $DB->get_prefix()); + $pregprefix = preg_quote($DB->get_prefix(), '/'); + + $sql = "SHOW FULL TABLES LIKE '$patprefix%'"; + $rs = $DB->get_recordset_sql($sql); + foreach ($rs as $record) { + $record = array_change_key_case((array)$record, CASE_LOWER); + $type = $record['table_type']; + unset($record['table_type']); + $fullname = array_shift($record); + + if ($pregprefix === '') { + $name = $fullname; + } else { + $count = null; + $name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count); + if ($count !== 1) { + continue; + } + } + + if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) { + echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem'); + continue; + } + if ($type === 'VIEW') { + echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem'); + continue; + } + $tables[$name] = $name; + } + $rs->close(); + + return $tables; +} /** * Remove all signed numbers from current database - mysql only. @@ -50,7 +95,7 @@ function upgrade_mysql_fix_unsigned_columns() { $pbar = new progress_bar('mysqlconvertunsigned', 500, true); $prefix = $DB->get_prefix(); - $tables = $DB->get_tables(); + $tables = upgrade_mysql_get_supported_tables(); $tablecount = count($tables); $i = 0; @@ -115,7 +160,7 @@ function upgrade_mysql_fix_lob_columns() { $pbar = new progress_bar('mysqlconvertlobs', 500, true); $prefix = $DB->get_prefix(); - $tables = $DB->get_tables(); + $tables = upgrade_mysql_get_supported_tables(); asort($tables); $tablecount = count($tables);