Make WordPress Core

Opened 11 years ago

Closed 10 years ago

#31679 closed defect (bug) (fixed)

dbDelta generates wrong SQL on ALTER TABLE with columns containing hyphens in name

Reported by: fulippo Owned by: ocean90
Priority: normal Milestone: 4.6
Component: Database Version: 4.1.1
Severity: normal Keywords:
Cc: Focuses:

Description

Upgrading a table using dbDelta a wrong SQL statement is generated when existing table's names contain hyphens.

Example of wrong SQL statement generated by dbDelta:

ALTER TABLE wp_stores CHANGE COLUMN _store-lng `_store-lng` text NULL

This is because the result for the query

$tablefields = $wpdb->get_results("DESCRIBE {$table};");

contains column names without backticks causing the SQL query to fail.

Since there is no way to filter column names it is impossible to upgrade tables.

Change History (5)

#1 @fulippo
11 years ago

  • Summary dbDelta generates wrong SQL on ALTER TABLE with fields containing hyphens in namedbDelta generates wrong SQL on ALTER TABLE with columns containing hyphens in name

#2 @jdgrimes
11 years ago

Related: #30655, #20263

dbDelta() just doesn't support backticks. I think the function could really use a complete rewrite. (I'd even be willing to undertake it.)

#3 @fulippo
11 years ago

As regards the problem reported above, I think that just changing the code around line 1764 from

$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];

to

$cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[strtolower($tablefield->Field)];

would make all work correctly

Last edited 11 years ago by fulippo (previous) (diff)

#4 @ocean90
10 years ago

  • Component PluginsDatabase
  • Milestone Awaiting Review4.6

This was fixed in [37583]. Going to commit a unit test for this case.

#5 @ocean90
10 years ago

  • Owner set to ocean90
  • Resolutionfixed
  • Status newclosed

In 38044:

Database: Add unit test to test that a column type change for a table name with a hyphen is working after [37583].

Fixes #31679.

Note: See TracTickets for help on using tickets.