From 436650b075f2aec23e6d4759b8b957176f1efcfd Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 11 Mar 2012 15:51:49 +0100 Subject: [PATCH] MDL-31985 convert all existing LOBs --- lib/db/upgrade.php | 8 +++++++ lib/db/upgradelib.php | 54 +++++++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index d335eb43482..c42d27c87e4 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -204,6 +204,14 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2012030100.02); } + if ($oldversion < 2012030900.01) { + // migrate all texts and binaries to big size - it should be safe to interrupt this and continue later + upgrade_mysql_fix_lob_columns(); + + // Main savepoint reached + upgrade_main_savepoint(true, 2012030900.01); + } + return true; } diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index d589a5a2433..7960694a309 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -83,3 +83,57 @@ function upgrade_mysql_fix_unsigned_columns() { $pbar->update($i, $tablecount, "Converted unsigned columns in MySQL database - $i/$tablecount."); } } + +/** + * Migrate all text and binary columns to big size - mysql only. + */ +function upgrade_mysql_fix_lob_columns() { + // we are not using standard API for changes of column intentionally + + global $DB; + + if ($DB->get_dbfamily() !== 'mysql') { + return; + } + + $pbar = new progress_bar('mysqlconvertlobs', 500, true); + + $prefix = $DB->get_prefix(); + $tables = $DB->get_tables(); + asort($tables); + + $tablecount = count($tables); + $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; + + $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 ($column->type === 'tinytext' or $column->type === 'mediumtext' or $column->type === 'text') { + $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; + $default = !is_null($column->default) ? "DEFAULT '$column->default'" : ''; + // primary, unique and inc are not supported for texts + $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGTEXT $notnull $default"; + $DB->change_database_structure($sql); + } + if ($column->type === 'tinyblob' or $column->type === 'mediumblob' or $column->type === 'blob') { + $notnull = ($column->null === 'NO') ? 'NOT NULL' : 'NULL'; + $default = !is_null($column->default) ? "DEFAULT '$column->default'" : ''; + // primary, unique and inc are not supported for blobs + $sql = "ALTER TABLE `{$prefix}$table` MODIFY COLUMN `$column->field` LONGBLOB $notnull $default"; + $DB->change_database_structure($sql); + } + } + $rs->close(); + + $pbar->update($i, $tablecount, "Converted LOB columns in MySQL database - $i/$tablecount."); + } +} diff --git a/version.php b/version.php index 3b88d2320bc..a76f0576e90 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2012030900.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2012030900.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes