diff --git a/features/search-replace.feature b/features/search-replace.feature index 05f176f7..f3e1b910 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -316,6 +316,51 @@ 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 | + + # --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 + """ + + 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 diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index d6e73804..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 ); @@ -742,27 +742,26 @@ 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 ); - } + 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 ) . '%' ) ); - } 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