From 18ca26574b0178813e27b0039f6ea86b1b8fa3e0 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 23 May 2016 08:36:28 +0000 Subject: [PATCH] Database: `dbDelta()` will no longer try to downgrade the size of `TEXT` and `BLOB` columns. When upgrading to `utf8mb4`, `TEXT` fields will be upgraded to `MEDIUMTEXT` (and likewise for all other `*TEXT` and `*BLOB` fields). This is to allow for the additional space requirements of `utf8mb4`. On the subsequent upgrade, after the `utf8mb4` upgrade, `dbDelta()` would try and downgrade the fields to their original size again. At best, this it a waste of time, at worst, this could truncate any data larger than the original size. There's no harm in leaving them at their original size, so let's do that. Fixes #36748. Built from https://develop.svn.wordpress.org/trunk@37525 git-svn-id: http://core.svn.wordpress.org/trunk@37493 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/upgrade.php | 22 ++++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index 05d7f97f28..c8f74733b0 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -2154,6 +2154,9 @@ function dbDelta( $queries = '', $execute = true ) { */ $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries ); + $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' ); + $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' ); + $global_tables = $wpdb->tables( 'global' ); foreach ( $cqueries as $table => $qry ) { // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal. @@ -2223,9 +2226,24 @@ function dbDelta( $queries = '', $execute = true ) { // Is actual field type different from the field type in query? if ($tablefield->Type != $fieldtype) { + $do_change = true; + if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) { + if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) { + $do_change = false; + } + } + + if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) { + if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) { + $do_change = false; + } + } + + if ( $do_change ) { // Add a query to change the column type - $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; - $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; + $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; + $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; + } } // Get the default value from the array diff --git a/wp-includes/version.php b/wp-includes/version.php index 0d5c8fe1ac..92a58b6353 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6-alpha-37524'; +$wp_version = '4.6-alpha-37525'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.