Skip to content

Alert user of database table permission errors during search-replace#216

Open
Copilot wants to merge 2 commits intomainfrom
copilot/alert-user-db-permissions-errors
Open

Alert user of database table permission errors during search-replace#216
Copilot wants to merge 2 commits intomainfrom
copilot/alert-user-db-permissions-errors

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

When a database UPDATE fails (e.g. read-only table, insufficient permissions), search-replace silently reported 0 replacements with no indication of why — even after a dry-run showing expected changes.

Changes

  • SQL path (sql_handle_col): Check $wpdb->last_error after $wpdb->query() UPDATE calls; emit WP_CLI::warning() with the table name, column name, and the raw database error.
  • PHP path (php_handle_col): Same check after each $wpdb->update() call.

Example output when a table is not writable:

Warning: Error updating column 'post_content' in table 'wp_posts': UPDATE command denied to user 'wpuser'@'localhost' for table 'wp_posts'
  • Behat test: Covers the SQL path by creating a table with a BEFORE UPDATE trigger that raises a MySQL signal error, verifying that STDERR contains the expected warning with the column and table name.
Original prompt

This section details on the original issue you should resolve

<issue_title>Alert the user of db table permissions errors</issue_title>
<issue_description>## Feature Request

Use Case and user story

When there are database table permissions errors, search-replace should be able to alert the user.

I was working with an older DB that I had manually imported the wp_posts table with the wrong DB permissions. (the table was read-only)

When I did a dry-run of the search-replace it worked great, showed me all the replacements to be made, etc.

Then when I did a live run, it went through the process and still reported that there were 527 changes to make, but instead of making them it returned Success: 0 Replacements made

I downloaded a more blunt tool for forcing the DB updates through, and that's when I discovered that the table in question is read-only. Once I knew that, the workaround was simple: export the DB to a fresh DB using wp db export and then import that fresh database, which resets the permissions, then search-replace worked like a charm.

I also tried with --verbose and --debug but the critical piece (when the actual SQL is running) is not outputting anything useful.

Output of the Dry Run

527 Replacements to be made

Output of the --debug argument on a live run

Success: made 0 replacements

Proposed solution

It would be more helpful if table permission errors are preventing the actual changing, that this is output.

I'd propose an additional argument of --errors or adding this output to the existing --debug argument.
</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera Thanks for the report, @Benunc! I think it might make even more sense to actually forward errors or warnings, and in this case show an error that a read-only db/table cannot be changed.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add alert for database table permissions errors Alert user of database table permission errors during search-replace Mar 15, 2026
Copilot AI requested a review from swissspidy March 15, 2026 07:51
@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Search_Replace_Command.php 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 15, 2026 08:21
@swissspidy swissspidy requested a review from a team as a code owner March 15, 2026 08:21
Copilot AI review requested due to automatic review settings March 15, 2026 08:21
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds user-facing warnings to wp search-replace when database UPDATE operations fail, so permission/read-only errors don’t result in a misleading “0 replacements” outcome with no explanation.

Changes:

  • Emit WP_CLI::warning() after failed SQL UPDATE queries by checking $wpdb->last_error.
  • Emit WP_CLI::warning() after failed per-row $wpdb->update() calls in the PHP replacement path.
  • Add a Behat scenario that forces an UPDATE failure via a MySQL trigger and asserts a warning is printed.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Search_Replace_Command.php Adds warnings for DB update failures in both SQL and PHP execution paths.
features/search-replace.feature Adds an end-to-end Behat scenario to verify warning output on SQL UPDATE failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 776 to +779
$wpdb->update( $table, [ $col => $value ], $update_where );
if ( $wpdb->last_error ) {
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s': %s", $col, $table, $wpdb->last_error ) );
}
And I run `wp db query "INSERT INTO wp_readonly_test (data) VALUES ('old-value');"`
And I run `wp db query "CREATE TRIGGER prevent_update BEFORE UPDATE ON wp_readonly_test FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Table is read-only';"`

When I try `wp search-replace old-value new-value wp_readonly_test --all-tables-with-prefix`
Then STDERR should contain:
"""
Error updating column 'data' in table 'wp_readonly_test'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alert the user of db table permissions errors

3 participants