From 57160c25d7a49050247e88d98efe6e6f1f4228fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Mon, 12 Nov 2012 19:48:18 +0100 Subject: [PATCH] MDL-36493 improve timeout handling in mysql upgrade Credit goes to Mark Nielsen, thanks. --- lib/db/upgradelib.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index f0ee0fd7845..6edc5d8c827 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -102,18 +102,12 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() { $i = 0; foreach ($tables as $table) { $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(); $sql = "SHOW COLUMNS FROM `{{$table}}`"; $rs = $DB->get_recordset_sql($sql); foreach ($rs as $column) { - upgrade_set_timeout($timeout); - $column = (object)array_change_key_case((array)$column, CASE_LOWER); if (stripos($column->type, 'unsigned') !== false) { $maxvalue = 0; @@ -138,7 +132,6 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() { $default = (!is_null($column->default) and $column->default !== '') ? "DEFAULT '$column->default'" : ''; $autoinc = (stripos($column->extra, 'auto_increment') !== false) ? 'AUTO_INCREMENT' : ''; // Primary and unique not necessary here, change_database_structure does not add prefix. - $changes[] = "MODIFY COLUMN `$column->field` $type $notnull $default $autoinc"; } else if ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') { @@ -158,6 +151,12 @@ function upgrade_mysql_fix_unsigned_and_lob_columns() { $rs->close(); 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); $DB->change_database_structure($sql); }