weekly release 2.4dev
[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
7385cc00
PS
33
34/**
35 * Remove all signed numbers from current database - mysql only.
36 */
37function upgrade_mysql_fix_unsigned_columns() {
38 // we are not using standard API for changes of column
39 // because everything 'signed'-related will be removed soon
40
41 // if anybody already has numbers higher than signed limit the execution stops
42 // and tables must be fixed manually before continuing upgrade
43
44 global $DB;
45
46 if ($DB->get_dbfamily() !== 'mysql') {
47 return;
48 }
49
50 $pbar = new progress_bar('mysqlconvertunsigned', 500, true);
51
52 $prefix = $DB->get_prefix();
53 $tables = $DB->get_tables();
54
55 $tablecount = count($tables);
56 $i = 0;
57 foreach ($tables as $table) {
58 $i++;
59 // set appropriate timeout - 5 minutes per milion of records should be enough, min 60 minutes just in case
60 $count = $DB->count_records($table, array());
61 $timeout = ($count/1000000)*5*60;
62 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
63
64 $sql = "SHOW COLUMNS FROM `{{$table}}`";
65 $rs = $DB->get_recordset_sql($sql);
66 foreach ($rs as $column) {
67 upgrade_set_timeout($timeout);
68
69 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
70 if (stripos($column->type, 'unsigned') !== false) {
72263b8f
PS
71 $maxvalue = 0;
72 if (preg_match('/^int/i', $column->type)) {
73 $maxvalue = 2147483647;
74 } else if (preg_match('/^medium/i', $column->type)) {
75 $maxvalue = 8388607;
76 } else if (preg_match('/^smallint/i', $column->type)) {
77 $maxvalue = 32767;
78 } else if (preg_match('/^tinyint/i', $column->type)) {
79 $maxvalue = 127;
80 }
81 if ($maxvalue) {
82 // Make sure nobody is abusing our integer ranges - moodle int sizes are in digits, not bytes!!!
83 $invalidcount = $DB->get_field_sql("SELECT COUNT('x') FROM `{{$table}}` WHERE `$column->field` > :maxnumber", array('maxnumber'=>$maxvalue));
84 if ($invalidcount) {
85 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.");
86 }
87 }
7385cc00
PS
88 $type = preg_replace('/unsigned/i', 'signed', $column->type);
89 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 90 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
7385cc00
PS
91 $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : '';
92 // primary and unique not necessary here, change_database_structure does not add prefix
93 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";
94 $DB->change_database_structure($sql);
95 }
96 }
97 $rs->close();
98
99 $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount.");
100 }
101}
436650b0
PS
102
103/**
104 * Migrate all text and binary columns to big size - mysql only.
105 */
106function upgrade_mysql_fix_lob_columns() {
107 // we are not using standard API for changes of column intentionally
108
109 global $DB;
110
111 if ($DB->get_dbfamily() !== 'mysql') {
112 return;
113 }
114
115 $pbar = new progress_bar('mysqlconvertlobs', 500, true);
116
117 $prefix = $DB->get_prefix();
118 $tables = $DB->get_tables();
119 asort($tables);
120
121 $tablecount = count($tables);
122 $i = 0;
123 foreach ($tables as $table) {
124 $i++;
125 // set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case
126 $count = $DB->count_records($table, array());
127 $timeout = ($count/1000)*60;
128 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
129
130 $sql = "SHOW COLUMNS FROM `{{$table}}`";
131 $rs = $DB->get_recordset_sql($sql);
132 foreach ($rs as $column) {
133 upgrade_set_timeout($timeout);
134
135 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
136 if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') {
137 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 138 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
436650b0
PS
139 // primary, unique and inc are not supported for texts
140 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGTEXT $notnull $default";
141 $DB->change_database_structure($sql);
142 }
143 if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') {
144 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
e427ad30 145 $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
436650b0
PS
146 // primary, unique and inc are not supported for blobs
147 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGBLOB $notnull $default";
148 $DB->change_database_structure($sql);
149 }
150 }
151 $rs->close();
152
153 $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount.");
154 }
155}