Skip to content

Commit 9df13d2

Browse files
swissspidyclaude
andcommitted
Keep the primary key prefixes in the --where temporary table
A primary key on a TEXT or BLOB column indexes a prefix, e.g. PRIMARY KEY (slug(32)), and the temporary table that holds the rows --where selects could not be created without it. Read the prefix lengths of the table's primary key and index the same prefixes; the columns still hold the full values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 1918289 commit 9df13d2

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎features/search-replace-where.feature‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,36 @@ Feature: Test search-replace --where option
597597
| wp_idkeyed:body:ID = '1' | 1 | one foo |
598598
| wp_idkeyed::ID = '1' | 2 | one bar |
599599

600+
@require-mysql
601+
Scenario Outline: Where on a table whose primary key is a prefix of a text column
602+
Given a WP install
603+
And I run `wp db query "CREATE TABLE wp_prefixed ( slug TEXT NOT NULL, title TEXT, body TEXT, PRIMARY KEY (slug(32)) )"`
604+
And I run `wp db query "INSERT INTO wp_prefixed VALUES ('one-slug-that-is-longer-than-its-32-character-key', 'foo', 'foo'), ('two-slug-that-is-longer-than-its-32-character-key', 'foo', 'foo')"`
605+
606+
When I run `wp search-replace foo bar --all-tables --where="wp_prefixed::slug = 'one-slug-that-is-longer-than-its-32-character-key'" --dry-run --format=count <flags>`
607+
Then STDOUT should be:
608+
"""
609+
2
610+
"""
611+
612+
When I run `wp search-replace foo bar --all-tables --where="wp_prefixed::slug = 'one-slug-that-is-longer-than-its-32-character-key'" --format=count <flags>`
613+
Then STDOUT should be:
614+
"""
615+
2
616+
"""
617+
618+
When I run `wp db query "SELECT CONCAT_WS('|', slug, title, body) FROM wp_prefixed ORDER BY slug" --skip-column-names`
619+
Then STDOUT should be:
620+
"""
621+
one-slug-that-is-longer-than-its-32-character-key|bar|bar
622+
two-slug-that-is-longer-than-its-32-character-key|foo|foo
623+
"""
624+
625+
Examples:
626+
| flags |
627+
| |
628+
| --precise |
629+
600630
@require-mysql
601631
Scenario Outline: A failed row selection stops before any column is replaced
602632
Given a WP install

‎src/Search_Replace_Command.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,25 @@ private function create_where_key_tables( $table, $primary_keys, $columns ) {
18711871
$table_sql = self::esc_sql_ident( $table );
18721872
$suffix = substr( md5( uniqid( '', true ) ), 0, 8 );
18731873

1874+
// The temporary table holds the full key values, but its primary key must index the same
1875+
// prefixes as the table's own, as a TEXT or BLOB column can only be indexed by a prefix.
1876+
$key_prefixes = array();
1877+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
1878+
foreach ( (array) $wpdb->get_results( "SHOW INDEX FROM $table_sql WHERE Key_name = 'PRIMARY'" ) as $index_col ) {
1879+
if ( isset( $index_col->Column_name, $index_col->Sub_part ) && (int) $index_col->Sub_part > 0 ) {
1880+
$key_prefixes[ $index_col->Column_name ] = (int) $index_col->Sub_part;
1881+
}
1882+
}
1883+
$key_index_sql = implode(
1884+
', ',
1885+
array_map(
1886+
static function ( $key ) use ( $key_prefixes ) {
1887+
return self::esc_sql_ident( $key ) . ( isset( $key_prefixes[ $key ] ) ? "({$key_prefixes[ $key ]})" : '' );
1888+
},
1889+
$primary_keys
1890+
)
1891+
);
1892+
18741893
$key_clauses = array();
18751894
$key_tables = array();
18761895
foreach ( array_keys( $groups ) as $index => $condition ) {
@@ -1879,7 +1898,7 @@ private function create_where_key_tables( $table, $primary_keys, $columns ) {
18791898

18801899
$suppress_errors = $wpdb->suppress_errors();
18811900
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident, the --where clauses are raw SQL by design
1882-
$wpdb->query( "CREATE TEMPORARY TABLE $key_table_sql ( PRIMARY KEY ( $keys_sql ) ) SELECT $keys_sql FROM $table_sql WHERE $condition" );
1901+
$wpdb->query( "CREATE TEMPORARY TABLE $key_table_sql ( PRIMARY KEY ( $key_index_sql ) ) SELECT $keys_sql FROM $table_sql WHERE $condition" );
18831902
$error = $wpdb->last_error;
18841903
$wpdb->suppress_errors( $suppress_errors );
18851904
if ( $error ) {

0 commit comments

Comments
 (0)