Skip to content

Commit 1918289

Browse files
swissspidyclaude
andcommitted
Compare string primary keys as strings when updating a row with --precise
wpdb->update() picks the format of each WHERE column from $wpdb->field_types when none is given, so a VARCHAR primary key named `ID` was compared as %d: the key '1' then also matched '01', and the row '01' was updated although --where did not select it. Pass %s for string keys (and for the replaced text value), and keep wpdb's default for the other keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 5702dfc commit 1918289

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎features/search-replace-where.feature‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,30 @@ Feature: Test search-replace --where option
573573
| |
574574
| --precise |
575575

576+
@require-mysql
577+
Scenario Outline: Where with --precise updates only the selected row of a string primary key named like a WordPress integer column
578+
Given a WP install
579+
And I run `wp db query "CREATE TABLE wp_idkeyed ( ID VARCHAR(20) NOT NULL, title TEXT, body TEXT, PRIMARY KEY (ID) )"`
580+
And I run `wp db query "INSERT INTO wp_idkeyed VALUES ('01', 'zero one foo', 'zero one foo'), ('1', 'one foo', 'one foo')"`
581+
582+
When I run `wp search-replace foo bar --all-tables --where="<where>" --precise --format=count`
583+
Then STDOUT should be:
584+
"""
585+
<count>
586+
"""
587+
588+
When I run `wp db query "SELECT CONCAT_WS('|', ID, title, body) FROM wp_idkeyed ORDER BY ID" --skip-column-names`
589+
Then STDOUT should be:
590+
"""
591+
01|zero one foo|zero one foo
592+
1|<title>|one bar
593+
"""
594+
595+
Examples:
596+
| where | count | title |
597+
| wp_idkeyed:body:ID = '1' | 1 | one foo |
598+
| wp_idkeyed::ID = '1' | 2 | one bar |
599+
576600
@require-mysql
577601
Scenario Outline: A failed row selection stops before any column is replaced
578602
Given a WP install

‎src/Search_Replace_Command.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,12 +1016,20 @@ static function ( $key ) {
10161016
// In dry-run mode, count replacements once a change has been detected.
10171017
++$count;
10181018
} else {
1019-
$update_where = array();
1019+
// Pass the formats explicitly, as wpdb would otherwise pick them from $wpdb->field_types by
1020+
// column name only, e.g. %d for any `ID` column: a string key '1' would then also match '01'.
1021+
$update_where = array();
1022+
$where_formats = array();
10201023
foreach ( (array) $keys as $k => $v ) {
10211024
$update_where[ $k ] = $v;
1025+
if ( in_array( $k, $this->string_primary_keys[ $table ], true ) ) {
1026+
$where_formats[] = '%s';
1027+
} else {
1028+
$where_formats[] = isset( $wpdb->field_types[ $k ] ) ? $wpdb->field_types[ $k ] : '%s';
1029+
}
10221030
}
10231031

1024-
$result = $wpdb->update( $table, array( $col => $value ), $update_where );
1032+
$result = $wpdb->update( $table, array( $col => $value ), $update_where, array( '%s' ), $where_formats );
10251033
if ( false === $result ) {
10261034
if ( empty( $this->update_error_columns[ $table ][ $col ] ) ) {
10271035
$this->update_error_columns[ $table ][ $col ] = true;

0 commit comments

Comments
 (0)