weekly on-sync release 2.5dev
[moodle.git] / lib / db / upgradelib.php
CommitLineData
117bd748 1<?php
5b4a78e2
PS
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Upgrade helper functions
19 *
42ff9ce6 20 * This file is used for special upgrade functions - for example groups and gradebook.
56a1a882 21 * These functions must use SQL and database related functions only- no other Moodle API,
42ff9ce6 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!)
5b4a78e2 24 *
39b90b51
EL
25 * @package core_install
26 * @category upgrade
27 * @copyright 2007 Petr Skoda (http://skodak.org)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42ff9ce6 29 */
30
5b4a78e2
PS
31defined('MOODLE_INTERNAL') || die();
32
3182d885
PS
33/**
34 * Returns all non-view and non-temp tables with sane names.
35 * Prints list of non-supported tables using $OUTPUT->notification()
36 *
37 * @return array
38 */
39function upgrade_mysql_get_supported_tables() {
40 global $OUTPUT, $DB;
41
42 $tables = array();
43 $patprefix = str_replace('_', '\\_', $DB->get_prefix());
44 $pregprefix = preg_quote($DB->get_prefix(), '/');
45
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);
53
54 if ($pregprefix === '') {
55 $name = $fullname;
56 } else {
57 $count = null;
58 $name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count);
59 if ($count !== 1) {
60 continue;
61 }
62 }
63
64 if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) {
65 echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem');
66 continue;
67 }
68 if ($type === 'VIEW') {
69 echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem');
70 continue;
71 }
72 $tables[$name] = $name;
73 }
74 $rs->close();
75
76 return $tables;
77}
7385cc00
PS
78
79/**
80 * Remove all signed numbers from current database - mysql only.
81 */
82function upgrade_mysql_fix_unsigned_columns() {
83 // we are not using standard API for changes of column
84 // because everything 'signed'-related will be removed soon
85
86 // if anybody already has numbers higher than signed limit the execution stops
87 // and tables must be fixed manually before continuing upgrade
88
89 global $DB;
90
91 if ($DB->get_dbfamily() !== 'mysql') {
92 return;
93 }
94
95 $pbar = new progress_bar('mysqlconvertunsigned', 500, true);
96
97 $prefix = $DB->get_prefix();
3182d885 98 $tables = upgrade_mysql_get_supported_tables();
7385cc00
PS
99
100 $tablecount = count($tables);
101 $i = 0;
102 foreach ($tables as $table) {
103 $i++;
104 // set appropriate timeout - 5 minutes per milion of records should be enough, min 60 minutes just in case
105 $count = $DB->count_records($table, array());
106 $timeout = ($count/1000000)*5*60;
107 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
108
109 $sql = "SHOW COLUMNS FROM `{{$table}}`";
110 $rs = $DB->get_recordset_sql($sql);
111 foreach ($rs as $column) {
112 upgrade_set_timeout($timeout);
113
114 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
115 if (stripos($column->type, 'unsigned') !== false) {
72263b8f
PS
116 $maxvalue = 0;
117 if (preg_match('/^int/i', $column->type)) {
118 $maxvalue = 2147483647;
119 } else if (preg_match('/^medium/i', $column->type)) {
120 $maxvalue = 8388607;
121 } else if (preg_match('/^smallint/i', $column->type)) {
122 $maxvalue = 32767;
123 } else if (preg_match('/^tinyint/i', $column->type)) {
124 $maxvalue = 127;
125 }
126 if ($maxvalue) {
127 // Make sure nobody is abusing our integer ranges - moodle int sizes are in digits, not bytes!!!
128 $invalidcount = $DB->get_field_sql("SELECT COUNT('x') FROM `{{$table}}` WHERE `$column->field` > :maxnumber", array('maxnumber'=>$maxvalue));
129 if ($invalidcount) {
130 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.");
131 }
132 }
7385cc00
PS
133 $type = preg_replace('/unsigned/i', 'signed', $column->type);
134 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 135 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
7385cc00
PS
136 $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : '';
137 // primary and unique not necessary here, change_database_structure does not add prefix
138 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";
139 $DB->change_database_structure($sql);
140 }
141 }
142 $rs->close();
143
144 $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount.");
145 }
146}
436650b0
PS
147
148/**
149 * Migrate all text and binary columns to big size - mysql only.
150 */
151function upgrade_mysql_fix_lob_columns() {
152 // we are not using standard API for changes of column intentionally
153
154 global $DB;
155
156 if ($DB->get_dbfamily() !== 'mysql') {
157 return;
158 }
159
160 $pbar = new progress_bar('mysqlconvertlobs', 500, true);
161
162 $prefix = $DB->get_prefix();
3182d885 163 $tables = upgrade_mysql_get_supported_tables();
436650b0
PS
164 asort($tables);
165
166 $tablecount = count($tables);
167 $i = 0;
168 foreach ($tables as $table) {
169 $i++;
170 // set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case
171 $count = $DB->count_records($table, array());
172 $timeout = ($count/1000)*60;
173 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
174
175 $sql = "SHOW COLUMNS FROM `{{$table}}`";
176 $rs = $DB->get_recordset_sql($sql);
177 foreach ($rs as $column) {
178 upgrade_set_timeout($timeout);
179
180 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
181 if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') {
182 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 183 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
436650b0
PS
184 // primary, unique and inc are not supported for texts
185 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGTEXT $notnull $default";
186 $DB->change_database_structure($sql);
187 }
188 if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') {
189 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 190 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
436650b0
PS
191 // primary, unique and inc are not supported for blobs
192 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGBLOB $notnull $default";
193 $DB->change_database_structure($sql);
194 }
195 }
196 $rs->close();
197
198 $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount.");
199 }
200}