2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Upgrade helper functions
20 * This file is used for special upgrade functions - for example groups and gradebook.
21 * These functions must use SQL and database related functions only- no other Moodle API,
22 * because it might depend on db structures that are not yet present during upgrade.
23 * (Do not use functions from accesslib.php, grades classes or group functions at all!)
25 * @package core_install
27 * @copyright 2007 Petr Skoda (http://skodak.org)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 defined('MOODLE_INTERNAL') || die();
34 * Returns all non-view and non-temp tables with sane names.
35 * Prints list of non-supported tables using $OUTPUT->notification()
39 function upgrade_mysql_get_supported_tables() {
43 $patprefix = str_replace('_', '\\_', $DB->get_prefix());
44 $pregprefix = preg_quote($DB->get_prefix(), '/');
46 $sql = "SHOW FULL TABLES LIKE '$patprefix%'";
47 $rs = $DB->get_recordset_sql($sql);
48 foreach ($rs as $record) {
49 $record = array_change_key_case((array)$record, CASE_LOWER);
50 $type = $record['table_type'];
51 unset($record['table_type']);
52 $fullname = array_shift($record);
54 if ($pregprefix === '') {
58 $name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count);
64 if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) {
65 echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem');
68 if ($type === 'VIEW') {
69 echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem');
72 $tables[$name] = $name;
80 * Remove all signed numbers from current database and change
81 * text fields to long texts - mysql only.
83 function upgrade_mysql_fix_unsigned_and_lob_columns() {
84 // We are not using standard API for changes of column
85 // because everything 'signed'-related will be removed soon.
87 // If anybody already has numbers higher than signed limit the execution stops
88 // and tables must be fixed manually before continuing upgrade.
92 if ($DB->get_dbfamily() !== 'mysql') {
96 $pbar = new progress_bar('mysqlconvertunsignedlobs', 500, true);
98 $prefix = $DB->get_prefix();
99 $tables = upgrade_mysql_get_supported_tables();
101 $tablecount = count($tables);
103 foreach ($tables as $table) {
108 $sql = "SHOW COLUMNS FROM `{{$table}}`";
109 $rs = $DB->get_recordset_sql($sql);
110 foreach ($rs as $column) {
111 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
112 if (stripos($column->type, 'unsigned') !== false) {
114 if (preg_match('/^int/i', $column->type)) {
115 $maxvalue = 2147483647;
116 } else if (preg_match('/^medium/i', $column->type)) {
118 } else if (preg_match('/^smallint/i', $column->type)) {
120 } else if (preg_match('/^tinyint/i', $column->type)) {
124 // Make sure nobody is abusing our integer ranges - moodle int sizes are in digits, not bytes!!!
125 $invalidcount = $DB->get_field_sql("SELECT COUNT('x') FROM `{{$table}}` WHERE `$column->field` > :maxnumber", array('maxnumber'=>$maxvalue));
127 throw new moodle_exception('notlocalisederrormessage', 'error', new moodle_url('/admin/'), "Database table '{$table}'' contains unsigned column '{$column->field}' with $invalidcount values that are out of allowed range, upgrade can not continue.");
130 $type = preg_replace('/unsigned/i', 'signed', $column->type);
131 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
132 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
133 $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : '';
134 // Primary and unique not necessary here, change_database_structure does not add prefix.
135 $changes[] = "MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";
137 } else if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') {
138 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
139 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
140 // Primary, unique and inc are not supported for texts.
141 $changes[] = "MODIFY COLUMN `$column->field` LONGTEXT $notnull $default";
143 } else if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') {
144 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
145 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
146 // Primary, unique and inc are not supported for blobs.
147 $changes[] = "MODIFY COLUMN `$column->field` LONGBLOB $notnull $default";
154 // Set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case.
155 $count = $DB->count_records($table, array());
156 $timeout = ($count/1000)*60;
157 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
158 upgrade_set_timeout($timeout);
160 $sql = "ALTER TABLE `{$prefix}$table` ".implode(', ', $changes);
161 $DB->change_database_structure($sql);
164 $pbar->update($i, $tablecount, "Converted unsigned/lob columns in MySQL database - $i/$tablecount.");