From 0d92fa25726681af7da9a077d0a5114a458af7fd Mon Sep 17 00:00:00 2001 From: Suryakant Upadhyay Date: Thu, 3 Dec 2026 18:50:43 +0530 Subject: [PATCH 1/3] Fix double counting in logged search-replace dry runs Fixes #255. --- features/search-replace.feature | 32 ++++++++++++++++++++++++++++++++ src/Search_Replace_Command.php | 23 ++++++++++------------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/features/search-replace.feature b/features/search-replace.feature index 05f176f7..826f9510 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -316,6 +316,38 @@ Feature: Do global search/replace http:\/\/example.com """ + @require-mysql + Scenario: Dry runs with logging count each matching row once + Given a WP install + + When I run `wp post create --post_content='Plain: https://old.example Escaped: https:\/\/old.example' --porcelain` + Then save STDOUT as {POST_ID} + + When I run `wp post create --post_content='https://old.example'` + And I run `wp post create --post_content='https:\/\/old.example'` + + And I run `wp search-replace https://old.example https://new.example wp_posts --include-columns=post_content --dry-run --log=replace.log --before_context=0 --after_context=0` + Then STDOUT should be a table containing rows: + | Table | Column | Replacements | Type | + | wp_posts | post_content | 3 | SQL | + And the replace.log file should contain: + """ + wp_posts.post_content:{POST_ID} + < https://old.example + > https://new.example + """ + And the replace.log file should contain: + """ + wp_posts.post_content:{POST_ID} + < https:\/\/old.example + > https:\/\/new.example + """ + + When I run `wp search-replace old.example new.example wp_posts --include-columns=post_content --dry-run --log=plain.log` + Then STDOUT should be a table containing rows: + | Table | Column | Replacements | Type | + | wp_posts | post_content | 3 | SQL | + @require-mysql Scenario: Search and replace with quoted strings Given a WP install diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index d6e73804..88240875 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -742,27 +742,24 @@ private function sql_handle_col( $col, $primary_keys, $table, $old, $new ) { $new_json = self::json_encode_strip_quotes( $new ); $has_json = $old_json !== $old; + $count = 0; + if ( $this->log_handle ) { + $count = $this->log_sql_diff( $col, $primary_keys, $table, $old, $new ); + if ( $has_json ) { + $this->log_sql_diff( $col, $primary_keys, $table, $old_json, $new_json ); + } + } + if ( $this->dry_run ) { - if ( $this->log_handle ) { - $count = $this->log_sql_diff( $col, $primary_keys, $table, $old, $new ); - if ( $has_json ) { - $count += $this->log_sql_diff( $col, $primary_keys, $table, $old_json, $new_json ); - } - } elseif ( $has_json ) { + if ( $has_json ) { // Single query with OR to avoid counting rows that match both forms twice. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT($col_sql) FROM $table_sql WHERE $col_sql LIKE BINARY %s OR $col_sql LIKE BINARY %s;", '%' . self::esc_like( $old ) . '%', '%' . self::esc_like( $old_json ) . '%' ) ); - } else { + } elseif ( ! $this->log_handle ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT($col_sql) FROM $table_sql WHERE $col_sql LIKE BINARY %s;", '%' . self::esc_like( $old ) . '%' ) ); } } else { - if ( $this->log_handle ) { - $this->log_sql_diff( $col, $primary_keys, $table, $old, $new ); - if ( $has_json ) { - $this->log_sql_diff( $col, $primary_keys, $table, $old_json, $new_json ); - } - } if ( $has_json ) { // Single nested REPLACE handles both plain and JSON-encoded forms in one pass. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident From b08d3a5c1ced92a0eb905cc2b23a7ee8a8700527 Mon Sep 17 00:00:00 2001 From: Suryakant Upadhyay Date: Thu, 3 Dec 2026 18:50:43 +0530 Subject: [PATCH 2/3] Reuse column stats for logged dry-run counts --- features/search-replace.feature | 7 +++++++ src/Search_Replace_Command.php | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/features/search-replace.feature b/features/search-replace.feature index 826f9510..c017721d 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -348,6 +348,13 @@ Feature: Do global search/replace | Table | Column | Replacements | Type | | wp_posts | post_content | 3 | SQL | + # --verbose skips the column stats, so this covers the fallback count query. + When I run `wp search-replace https://old.example https://new.example wp_posts --include-columns=post_content --dry-run --log=verbose.log --verbose --format=count` + Then STDOUT should be: + """ + 3 + """ + @require-mysql Scenario: Search and replace with quoted strings Given a WP install diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index 88240875..63aa2ea5 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -619,7 +619,7 @@ function ( $col ) use ( $table ) { // The dry run count is the number of matching rows, which is already known. $count = $stats['matches']; } else { - $count = $this->sql_handle_col( $col, $primary_keys, $table, $old, $new ); + $count = $this->sql_handle_col( $col, $primary_keys, $table, $old, $new, $stats['matches'] ?? null ); } } @@ -733,7 +733,7 @@ private function php_export_table( $table, $old, $new ) { return array( $table_report, $total_rows ); } - private function sql_handle_col( $col, $primary_keys, $table, $old, $new ) { + private function sql_handle_col( $col, $primary_keys, $table, $old, $new, $match_count = null ) { global $wpdb; $table_sql = self::esc_sql_ident( $table ); @@ -751,7 +751,9 @@ private function sql_handle_col( $col, $primary_keys, $table, $old, $new ) { } if ( $this->dry_run ) { - if ( $has_json ) { + if ( null !== $match_count ) { + $count = $match_count; + } elseif ( $has_json ) { // Single query with OR to avoid counting rows that match both forms twice. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT($col_sql) FROM $table_sql WHERE $col_sql LIKE BINARY %s OR $col_sql LIKE BINARY %s;", '%' . self::esc_like( $old ) . '%', '%' . self::esc_like( $old_json ) . '%' ) ); From 8ddba513d7708627548963d128792ae14e106d0b Mon Sep 17 00:00:00 2001 From: Suryakant Upadhyay Date: Thu, 3 Dec 2026 19:04:08 +0530 Subject: [PATCH 3/3] Test verbose logged dry runs for strings without a JSON form --- features/search-replace.feature | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/features/search-replace.feature b/features/search-replace.feature index c017721d..f3e1b910 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -355,6 +355,12 @@ Feature: Do global search/replace 3 """ + When I run `wp search-replace old.example new.example wp_posts --include-columns=post_content --dry-run --log=plain-verbose.log --verbose --format=count` + Then STDOUT should be: + """ + 3 + """ + @require-mysql Scenario: Search and replace with quoted strings Given a WP install