Skip to content

Commit

Permalink
MDL-36493 improve timeout handling in mysql upgrade
Browse files Browse the repository at this point in the history
Credit goes to Mark Nielsen, thanks.
  • Loading branch information
skodak committed Dec 15, 2012
1 parent 4df8d19 commit 84b9192
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/db/upgradelib.php
Expand Up @@ -102,18 +102,12 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() {
$i = 0; $i = 0;
foreach ($tables as $table) { foreach ($tables as $table) {
$i++; $i++;
// Set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case.
$count = $DB->count_records($table, array());
$timeout = ($count/1000)*60;
$timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;


$changes = array(); $changes = array();


$sql = "SHOW COLUMNS FROM `{{$table}}`"; $sql = "SHOW COLUMNS FROM `{{$table}}`";
$rs = $DB->get_recordset_sql($sql); $rs = $DB->get_recordset_sql($sql);
foreach ($rs as $column) { foreach ($rs as $column) {
upgrade_set_timeout($timeout);

$column = (object)array_change_key_case((array)$column, CASE_LOWER); $column = (object)array_change_key_case((array)$column, CASE_LOWER);
if (stripos($column->type, 'unsigned') !== false) { if (stripos($column->type, 'unsigned') !== false) {
$maxvalue = 0; $maxvalue = 0;
Expand All @@ -138,7 +132,6 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() {
$default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : '';
$autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : ''; $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : '';
// Primary and unique not necessary here, change_database_structure does not add prefix. // Primary and unique not necessary here, change_database_structure does not add prefix.

$changes[] = "MODIFY COLUMN `$column->field` $type $notnull $default $autoinc"; $changes[] = "MODIFY COLUMN `$column->field` $type $notnull $default $autoinc";


} else if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') { } else if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') {
Expand All @@ -158,6 +151,12 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() {
$rs->close(); $rs->close();


if ($changes) { if ($changes) {
// Set appropriate timeout - 1 minute per thousand of records should be enough, min 60 minutes just in case.
$count = $DB->count_records($table, array());
$timeout = ($count/1000)*60;
$timeout = ($timeout < 60*60) ? 60*60 : (int)$timeout;
upgrade_set_timeout($timeout);

$sql = "ALTER TABLE `{$prefix}$table` ".implode(', ', $changes); $sql = "ALTER TABLE `{$prefix}$table` ".implode(', ', $changes);
$DB->change_database_structure($sql); $DB->change_database_structure($sql);
} }
Expand Down

0 comments on commit 84b9192

Please sign in to comment.