Skip to content

Commit 19e8989

Browse files
Database: Do not report an extra update in dbDelta() with backticks in table name.
Follow-up to [10948], [20704]. Props leewillis77, swissspidy, johnbillion, SergeyBiryukov. Fixes #63976. git-svn-id: https://develop.svn.wordpress.org/trunk@60782 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6df41d8 commit 19e8989

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/wp-admin/includes/upgrade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,8 +2962,10 @@ function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.N
29622962
// Create a tablename index for an array ($cqueries) of recognized query types.
29632963
foreach ( $queries as $qry ) {
29642964
if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) {
2965-
$cqueries[ trim( $matches[1], '`' ) ] = $qry;
2966-
$for_update[ $matches[1] ] = 'Created table ' . $matches[1];
2965+
$table_name = trim( $matches[1], '`' );
2966+
2967+
$cqueries[ $table_name ] = $qry;
2968+
$for_update[ $table_name ] = 'Created table ' . $matches[1];
29672969
continue;
29682970
}
29692971

tests/phpunit/tests/db/dbDelta.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function test_creating_a_table() {
141141
}
142142

143143
/**
144-
* Test that it does nothing for an existing table.
144+
* Test that no update is reported for an existing table.
145145
*/
146146
public function test_existing_table() {
147147

@@ -156,7 +156,33 @@ public function test_existing_table() {
156156
KEY key_1 (column_1($this->max_index_length)),
157157
KEY compound_key (id,column_1($this->max_index_length))
158158
)
159+
",
160+
false
161+
);
162+
163+
$this->assertSame( array(), $updates );
164+
}
165+
166+
/**
167+
* Test that no update is reported for an existing table name in backticks.
168+
*
169+
* @ticket 63976
170+
*/
171+
public function test_existing_table_name_in_backticks() {
172+
173+
global $wpdb;
174+
175+
$updates = dbDelta(
159176
"
177+
CREATE TABLE `{$wpdb->prefix}dbdelta_test` (
178+
id bigint(20) NOT NULL AUTO_INCREMENT,
179+
column_1 varchar(255) NOT NULL,
180+
PRIMARY KEY (id),
181+
KEY key_1 (column_1($this->max_index_length)),
182+
KEY compound_key (id,column_1($this->max_index_length))
183+
)
184+
",
185+
false
160186
);
161187

162188
$this->assertSame( array(), $updates );

0 commit comments

Comments
 (0)