Commit | Line | Data |
---|---|---|
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 |
31 | defined('MOODLE_INTERNAL') || die(); |
32 | ||
7385cc00 PS |
33 | |
34 | /** | |
35 | * Remove all signed numbers from current database - mysql only. | |
36 | */ | |
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 | |
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) { | |
71 | $type = preg_replace('/unsigned/i', 'signed', $column->type); | |
72 | $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; | |
e427ad30 | 73 | $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; |
7385cc00 PS |
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); | |
78 | } | |
79 | } | |
80 | $rs->close(); | |
81 | ||
82 | $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount."); | |
83 | } | |
84 | } | |
436650b0 PS |
85 | |
86 | /** | |
87 | * Migrate all text and binary columns to big size - mysql only. | |
88 | */ | |
89 | function upgrade_mysql_fix_lob_columns() { | |
90 | // we are not using standard API for changes of column intentionally | |
91 | ||
92 | global $DB; | |
93 | ||
94 | if ($DB->get_dbfamily() !== 'mysql') { | |
95 | return; | |
96 | } | |
97 | ||
98 | $pbar = new progress_bar('mysqlconvertlobs', 500, true); | |
99 | ||
100 | $prefix = $DB->get_prefix(); | |
101 | $tables = $DB->get_tables(); | |
102 | asort($tables); | |
103 | ||
104 | $tablecount = count($tables); | |
105 | $i = 0; | |
106 | foreach ($tables as $table) { | |
107 | $i++; | |
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; | |
112 | ||
113 | $sql = "SHOW COLUMNS FROM `{{$table}}`"; | |
114 | $rs = $DB->get_recordset_sql($sql); | |
115 | foreach ($rs as $column) { | |
116 | upgrade_set_timeout($timeout); | |
117 | ||
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'; | |
e427ad30 | 121 | $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; |
436650b0 PS |
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); | |
125 | } | |
126 | if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') { | |
127 | $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; | |
e427ad30 | 128 | $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; |
436650b0 PS |
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); | |
132 | } | |
133 | } | |
134 | $rs->close(); | |
135 | ||
136 | $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount."); | |
137 | } | |
138 | } |