MDL-31985 remove LOB sizes from code and UI
[moodle.git] / lib / db / upgradelib.php
CommitLineData
117bd748 1<?php
42ff9ce6 2
5b4a78e2
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Upgrade helper functions
20 *
42ff9ce6 21 * This file is used for special upgrade functions - for example groups and gradebook.
56a1a882 22 * These functions must use SQL and database related functions only- no other Moodle API,
42ff9ce6 23 * because it might depend on db structures that are not yet present during upgrade.
24 * (Do not use functions from accesslib.php, grades classes or group functions at all!)
5b4a78e2
PS
25 *
26 * @package core
27 * @subpackage admin
28 * @copyright 2007 Petr Skoda (http://skodak.org)
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42ff9ce6 30 */
31
5b4a78e2
PS
32defined('MOODLE_INTERNAL') || die();
33
7385cc00
PS
34
35/**
36 * Remove all signed numbers from current database - mysql only.
37 */
38function upgrade_mysql_fix_unsigned_columns() {
39 // we are not using standard API for changes of column
40 // because everything 'signed'-related will be removed soon
41
42 // if anybody already has numbers higher than signed limit the execution stops
43 // and tables must be fixed manually before continuing upgrade
44
45 global $DB;
46
47 if ($DB->get_dbfamily() !== 'mysql') {
48 return;
49 }
50
51 $pbar = new progress_bar('mysqlconvertunsigned', 500, true);
52
53 $prefix = $DB->get_prefix();
54 $tables = $DB->get_tables();
55
56 $tablecount = count($tables);
57 $i = 0;
58 foreach ($tables as $table) {
59 $i++;
60 // set appropriate timeout - 5 minutes per milion of records should be enough, min 60 minutes just in case
61 $count = $DB->count_records($table, array());
62 $timeout = ($count/1000000)*5*60;
63 $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
64
65 $sql = "SHOW COLUMNS FROM `{{$table}}`";
66 $rs = $DB->get_recordset_sql($sql);
67 foreach ($rs as $column) {
68 upgrade_set_timeout($timeout);
69
70 $column = (object)array_change_key_case((array)$column, CASE_LOWER);
71 if (stripos($column->type, 'unsigned') !== false) {
72 $type = preg_replace('/unsigned/i', 'signed', $column->type);
73 $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL';
74 $default = !is_null($column->default) ? "DEFAULT '$column->default'" : '';
75 $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : '';
76 // primary and unique not necessary here, change_database_structure does not add prefix
77 $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";
78 $DB->change_database_structure($sql);
79 }
80 }
81 $rs->close();
82
83 $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount.");
84 }
85}