Skip to content

Fix double counting in logged search-replace dry runs - #256

Open
elphizu wants to merge 3 commits into
wp-cli:mainfrom
elphizu:fix/search-replace-dry-run-count
Open

elphizu wants to merge 3 commits into
wp-cli:mainfrom
elphizu:fix/search-replace-dry-run-count

Conversation

@elphizu

@elphizu elphizu commented Dec 3, 2026 •

Copy link
Copy Markdown

Fixes #255.

When a row contains both https://old.example and its JSON-escaped form https:\/\/old.example, --dry-run reported 1 replacement for it, but --dry-run --log reported 2. The SQL path added up the row counts from its two log_sql_diff() calls, one per form, so a row with both forms was counted twice.

The log still lists both matches. The count now comes from a single count of matching rows, as it already did without --log. If the column stats from the table scan are available, it uses their match count. With --verbose there are no stats, so it falls back to the existing LIKE ... OR LIKE ... count query. Runs without --dry-run aren't affected.

Testing

I added the scenario "Dry runs with logging count each matching row once" to features/search-replace.feature. It creates three posts: one with both URL forms, one with only the plain URL and one with only the escaped URL. It then checks that:

  • a logged dry run reports 3 replacements, and the log shows both matches for the mixed post
  • a search for old.example, which has no escaped form, also reports 3
  • --verbose --format=count reports 3, which covers the fallback query

Summary by CodeRabbit

  • Bug Fixes
    • Dry-run search-and-replace results now count matching rows once, even when content contains both standard and escaped URL forms.
    • Dry-run logs show both forms, and count-only output consistently reports the matching total, including when using a fallback counting method.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The SQL column handler now accepts a precomputed match count for dry runs. SQL difference logging occurs before dry-run handling. New feature scenarios check counts for plain and escaped URL forms, plain-text searches, and verbose count-format runs.

Changes: Dry-run count and logging

Layer / File(s) Summary
Dry-run counting and SQL logging
src/Search_Replace_Command.php, features/search-replace.feature
The SQL column handler accepts an optional precomputed match count and logs SQL differences before dry-run counting. Feature scenarios check counts for plain and escaped URL forms, plain-text searches, and verbose count-format runs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: schlessera, gitlost, copilot

Merge Risk: Minimal · up to 8ddba

This change makes --dry-run --log count each matching row once, matching the count without --log. Runs without --dry-run are unaffected. No merge-blocking risk is evident from the supplied context.

Security architecture risk: Minimal · up to 8ddba

WP-CLI still logs both forms of a match when requested, but the dry-run total counts a row only once per searched column. The change does not expand database access or change how live replacements run.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: dry runs with logging no longer double count rows that contain both plain and JSON-escaped search strings. The raw summary confirms the changes in Search_Replace_Comma…
Linked Issues check Passed [#255] The issue requires that --dry-run and --dry-run --log each report one replacement for a row that holds both the plain and JSON-escaped URL, and that the log can still list both matches. The…
Out of Scope Changes check Passed All described changes serve the fix for #255. The changes are: the precomputed match count passed into sql_handle_col(), the reordering so SQL logging runs before dry-run handling, the fallback to t…
Full details: Title check

Explanation

The title names the main change: dry runs with logging no longer double count rows that contain both plain and JSON-escaped search strings. The raw summary confirms the changes in Search_Replace_Command and the added feature scenario match this aim.

Full details: Linked Issues check

Explanation

[#255] The issue requires that --dry-run and --dry-run --log each report one replacement for a row that holds both the plain and JSON-escaped URL, and that the log can still list both matches. The PR summary says __invoke() now passes a precomputed per-column match count to sql_handle_col(), which uses it for dry-run results when available. It also says SQL logging runs before dry-run handling, so the log still lists both log_sql_diff() matches, while the count is no longer the sum of two per-form counts. Without a supplied count (for example with --verbose), dry-run uses the existing combined LIKE ... OR LIKE ... query, which counts each row once. The added feature scenario checks a logged dry run that expects 3 replacements for three rows, with the log showing both matches for the mixed row. The current PR description states the same behavior. Limit: the code hunk in sql_handle_col() could not be read directly, because the file read was truncated. This verdict relies on the summary and the scenario description for that hunk.

Full details: Out of Scope Changes check

Explanation

All described changes serve the fix for #255. The changes are: the precomputed match count passed into sql_handle_col(), the reordering so SQL logging runs before dry-run handling, the fallback to the existing combined count query, and the new feature scenario that tests the dry-run count with logging, the plain-text search, and the verbose count-format fallback. Non-dry-run SQL updates and their logging are reported as unchanged. No unrelated behavior change appears in the summary. Limit: the full sql_handle_col() diff was not readable in this review, so the absence of unrelated edits there rests on the summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 54013c9c-236e-40c7-8caf-dba6e3db53ff
Commits

Reviewing files that changed from the base of the PR and between 17063d6 and 8ddba51.

Files selected for processing: 2

  • features/search-replace.feature
  • src/Search_Replace_Command.php

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions github-actions Bot added bug command:search-replace Related to 'search-replace' command scope:testing Related to testing labels Dec 3, 2026
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@elphizu
elphizu marked this pull request as ready for review December 3, 2026 13:40
@elphizu
elphizu requested a review from a team as a code owner December 3, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:search-replace Related to 'search-replace' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp search-replace --dry-run --log counts the same row twice for plain and JSON-escaped URLs

1 participant