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();
35 * Remove all signed numbers from current database - mysql only.
37 function 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
41 // if anybody already has numbers higher than signed limit the execution stops
42 // and tables must be fixed manually before continuing upgrade
46 if ($DB->get_dbfamily() !== 'mysql') {
50 $pbar = new progress_bar('mysqlconvertunsigned', 500, true);
52 $prefix = $DB->get_prefix();
53 $tables = $DB->get_tables();
55 $tablecount = count($tables);
57 foreach ($tables as $table) {
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;
64 $sql = "SHOW COLUMNS FROM `{{$table}}`";
65 $rs = $DB->get_recordset_sql($sql);
66 foreach ($rs as $column) {
67 upgrade_set_timeout($timeout);
69 $column = (object)array_change_key_case((array)$column, CASE_LOWER
);
70 if (stripos($column->type
, 'unsigned') !== false) {
71 $type = preg_replace('/unsigned/i', 'signed', $column->type
);
72 $notnull = ($column->null === 'NO') ?
'NOT NULL' : 'NULL';
73 $default = (!is_null($column->default) and $column->default !== '') ?
"DEFAULT '$column->default'" : '';
74 $autoinc = (stripos($column->extra
, 'auto_increment') !== false) ?
'AUTO_INCREMENT' : '';
75 // primary and unique not necessary here, change_database_structure does not add prefix
76 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";
77 $DB->change_database_structure($sql);
82 $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount.");
87 * Migrate all text and binary columns to big size - mysql only.
89 function upgrade_mysql_fix_lob_columns() {
90 // we are not using standard API for changes of column intentionally
94 if ($DB->get_dbfamily() !== 'mysql') {
98 $pbar = new progress_bar('mysqlconvertlobs', 500, true);
100 $prefix = $DB->get_prefix();
101 $tables = $DB->get_tables();
104 $tablecount = count($tables);
106 foreach ($tables as $table) {
108 // set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case
109 $count = $DB->count_records($table, array());
110 $timeout = ($count/1000)*60;
111 $timeout = ($timeout < 60*60) ?
60*60 : (int)$timeout;
113 $sql = "SHOW COLUMNS FROM `{{$table}}`";
114 $rs = $DB->get_recordset_sql($sql);
115 foreach ($rs as $column) {
116 upgrade_set_timeout($timeout);
118 $column = (object)array_change_key_case((array)$column, CASE_LOWER
);
119 if ($column->type
=== 'tinytext' or $column->type
=== 'mediumtext' or $column->type
=== 'text') {
120 $notnull = ($column->null === 'NO') ?
'NOT NULL' : 'NULL';
121 $default = (!is_null($column->default) and $column->default !== '') ?
"DEFAULT '$column->default'" : '';
122 // primary, unique and inc are not supported for texts
123 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGTEXT $notnull $default";
124 $DB->change_database_structure($sql);
126 if ($column->type
=== 'tinyblob' or $column->type
=== 'mediumblob' or $column->type
=== 'blob') {
127 $notnull = ($column->null === 'NO') ?
'NOT NULL' : 'NULL';
128 $default = (!is_null($column->default) and $column->default !== '') ?
"DEFAULT '$column->default'" : '';
129 // primary, unique and inc are not supported for blobs
130 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGBLOB $notnull $default";
131 $DB->change_database_structure($sql);
136 $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount.");