Database: Support backticks around field names when parsing a query for the field type.

Avoids an undefined index PHP warning by `dbDelta()`.

Props davidmosterd, ocean90.
See #20263.

git-svn-id: https://develop.svn.wordpress.org/trunk@37538 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2016-05-23 18:11:32 +00:00
parent 2111185ab9
commit 52caa1f06a
2 changed files with 25 additions and 1 deletions

View File

@ -2221,7 +2221,7 @@ function dbDelta( $queries = '', $execute = true ) {
if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
// Get the field type from the query.
preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[ $tablefield_field_lowercased ], $matches);
preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
$fieldtype = $matches[1];
$fieldtype_lowercased = strtolower( $fieldtype );

View File

@ -464,4 +464,28 @@ class Tests_dbDelta extends WP_UnitTestCase {
=> "Changed type of {$wpdb->prefix}dbdelta_test.column_3 from blob to mediumblob"
), $result );
}
/**
* @ticket 20263
*/
function test_query_with_backticks_does_not_throw_an_undefined_index_warning() {
global $wpdb;
$schema = "
CREATE TABLE {$wpdb->prefix}dbdelta_test2 (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`column_1` varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY compound_key (id,column_1)
)
";
$wpdb->query( $schema );
$updates = dbDelta( $schema, false );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}dbdelta_test2" );
$this->assertEmpty( $updates );
}
}