Skip to content

Commit ef7a034

Browse files
authored
Merge pull request #34 from wp-cli/sql_like_binary
Make dry run SQL LIKE query case sensitive.
2 parents 2f33320 + 6195b7e commit ef7a034

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

features/search-replace-export.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Feature: Search / replace with file export
176176
"""
177177
And STDOUT should contain:
178178
"""
179-
Success: Made 1 replacements and exported to wordpress.sql.
179+
Success: Made 1 replacement and exported to wordpress.sql.
180180
"""
181181

182182
When I run `wp db import wordpress.sql`

features/search-replace.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,36 @@ Feature: Do global search/replace
585585
"""
586586
Warning: No primary keys for table 'no_key'.
587587
"""
588+
589+
Scenario: Search / replace is case sensitive
590+
Given a WP install
591+
When I run `wp post create --post_title='Case Sensitive' --porcelain`
592+
Then save STDOUT as {POST_ID}
593+
594+
When I run `wp search-replace sensitive insensitive`
595+
Then STDOUT should contain:
596+
"""
597+
Success: Made 0 replacements.
598+
"""
599+
And STDERR should be empty
600+
601+
When I run `wp search-replace sensitive insensitive --dry-run`
602+
Then STDOUT should contain:
603+
"""
604+
Success: 0 replacements to be made.
605+
"""
606+
And STDERR should be empty
607+
608+
When I run `wp search-replace Sensitive insensitive --dry-run`
609+
Then STDOUT should contain:
610+
"""
611+
Success: 1 replacement to be made.
612+
"""
613+
And STDERR should be empty
614+
615+
When I run `wp search-replace Sensitive insensitive`
616+
Then STDOUT should contain:
617+
"""
618+
Success: Made 1 replacement.
619+
"""
620+
And STDERR should be empty

src/Search_Replace_Command.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ public function __invoke( $args, $assoc_args ) {
297297

298298
if ( ! $this->dry_run ) {
299299
if ( ! empty( $assoc_args['export'] ) ) {
300-
$success_message = "Made {$total} replacements and exported to {$assoc_args['export']}.";
300+
$success_message = 1 === $total ? "Made 1 replacement and exported to {$assoc_args['export']}." : "Made {$total} replacements and exported to {$assoc_args['export']}.";
301301
} else {
302-
$success_message = "Made $total replacements.";
302+
$success_message = 1 === $total ? "Made 1 replacement." : "Made $total replacements.";
303303
if ( $total && 'Default' !== WP_CLI\Utils\wp_get_cache_type() ) {
304304
$success_message .= ' Please remember to flush your persistent object cache with `wp cache flush`.';
305305
if ( is_multisite() ) {
@@ -375,7 +375,7 @@ private function sql_handle_col( $col, $table, $old, $new ) {
375375
$table_sql = self::esc_sql_ident( $table );
376376
$col_sql = self::esc_sql_ident( $col );
377377
if ( $this->dry_run ) {
378-
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT($col_sql) FROM $table_sql WHERE $col_sql LIKE %s;", '%' . self::esc_like( $old ) . '%' ) );
378+
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT($col_sql) FROM $table_sql WHERE $col_sql LIKE BINARY %s;", '%' . self::esc_like( $old ) . '%' ) );
379379
} else {
380380
$count = $wpdb->query( $wpdb->prepare( "UPDATE $table_sql SET $col_sql = REPLACE($col_sql, %s, %s);", $old, $new ) );
381381
}
@@ -395,7 +395,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
395395

396396
$table_sql = self::esc_sql_ident( $table );
397397
$col_sql = self::esc_sql_ident( $col );
398-
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE %s', '%' . self::esc_like( $old ) . '%' );
398+
$where = $this->regex ? '' : " WHERE $col_sql" . $wpdb->prepare( ' LIKE BINARY %s', '%' . self::esc_like( $old ) . '%' );
399399
$primary_keys_sql = implode( ',', self::esc_sql_ident( $primary_keys ) );
400400
$rows = $wpdb->get_results( "SELECT {$primary_keys_sql} FROM {$table_sql} {$where}" );
401401
foreach ( $rows as $keys ) {

0 commit comments

Comments
 (0)