Commit | Line | Data |
---|---|---|
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 |
32 | defined('MOODLE_INTERNAL') || die(); |
33 | ||
7385cc00 PS |
34 | |
35 | /** | |
36 | * Remove all signed numbers from current database - mysql only. | |
37 | */ | |
38 | function 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 | } | |
436650b0 PS |
86 | |
87 | /** | |
88 | * Migrate all text and binary columns to big size - mysql only. | |
89 | */ | |
90 | function upgrade_mysql_fix_lob_columns() { | |
91 | // we are not using standard API for changes of column intentionally | |
92 | ||
93 | global $DB; | |
94 | ||
95 | if ($DB->get_dbfamily() !== 'mysql') { | |
96 | return; | |
97 | } | |
98 | ||
99 | $pbar = new progress_bar('mysqlconvertlobs', 500, true); | |
100 | ||
101 | $prefix = $DB->get_prefix(); | |
102 | $tables = $DB->get_tables(); | |
103 | asort($tables); | |
104 | ||
105 | $tablecount = count($tables); | |
106 | $i = 0; | |
107 | foreach ($tables as $table) { | |
108 | $i++; | |
109 | // set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case | |
110 | $count = $DB->count_records($table, array()); | |
111 | $timeout = ($count/1000)*60; | |
112 | $timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout; | |
113 | ||
114 | $sql = "SHOW COLUMNS FROM `{{$table}}`"; | |
115 | $rs = $DB->get_recordset_sql($sql); | |
116 | foreach ($rs as $column) { | |
117 | upgrade_set_timeout($timeout); | |
118 | ||
119 | $column = (object)array_change_key_case((array)$column, CASE_LOWER); | |
120 | if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') { | |
121 | $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; | |
122 | $default = !is_null($column->default) ? "DEFAULT '$column->default'" : ''; | |
123 | // primary, unique and inc are not supported for texts | |
124 | $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGTEXT $notnull $default"; | |
125 | $DB->change_database_structure($sql); | |
126 | } | |
127 | if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') { | |
128 | $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; | |
129 | $default = !is_null($column->default) ? "DEFAULT '$column->default'" : ''; | |
130 | // primary, unique and inc are not supported for blobs | |
131 | $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGBLOB $notnull $default"; | |
132 | $DB->change_database_structure($sql); | |
133 | } | |
134 | } | |
135 | $rs->close(); | |
136 | ||
137 | $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount."); | |
138 | } | |
139 | } |