Skip to content

Commit f6138ca

Browse files
swissspidyCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 005dc9b commit f6138ca

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

features/search-replace.feature

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,12 @@ Feature: Do global search/replace
15821582
And I run `wp db query "INSERT INTO wp_readonly_test (data) VALUES ('old-value');"`
15831583
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';"`
15841584

1585-
When I try `wp search-replace old-value new-value wp_readonly_test --all-tables-with-prefix`
1585+
When I try `wp search-replace old-value new-value wp_readonly_test`
15861586
Then STDERR should contain:
15871587
"""
15881588
Error updating column 'data' in table 'wp_readonly_test'
15891589
"""
1590+
And STDERR should contain:
1591+
"""
1592+
Table is read-only
1593+
"""

src/Search_Replace_Command.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class Search_Replace_Command extends WP_CLI_Command {
1919
*/
2020
private $export_handle = false;
2121

22+
/**
23+
* Tracks table/column combinations that have encountered update errors,
24+
* so we can avoid repeated failing updates and noisy per-row warnings.
25+
*
26+
* @var array
27+
*/
28+
private $update_error_columns = array();
29+
2230
/**
2331
* @var int
2432
*/
@@ -766,17 +774,36 @@ static function ( $key ) {
766774
$replacer->clear_log_data();
767775
}
768776

769-
++$count;
770-
if ( ! $this->dry_run ) {
777+
// If we've already seen an update error for this table/column and are not in dry-run,
778+
// skip further attempts to avoid repeated failures and noisy warnings.
779+
if ( ! $this->dry_run && ! empty( $this->update_error_columns[ $table ][ $col ] ) ) {
780+
continue;
781+
}
782+
783+
if ( $this->dry_run ) {
784+
// In dry-run mode, count replacements once a change has been detected.
785+
++$count;
786+
} else {
771787
$update_where = array();
772788
foreach ( (array) $keys as $k => $v ) {
773789
$update_where[ $k ] = $v;
774790
}
775791

776-
$wpdb->update( $table, [ $col => $value ], $update_where );
777-
if ( $wpdb->last_error ) {
778-
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s': %s", $col, $table, $wpdb->last_error ) );
792+
$result = $wpdb->update( $table, array( $col => $value ), $update_where );
793+
if ( false === $result ) {
794+
if ( empty( $this->update_error_columns[ $table ][ $col ] ) ) {
795+
$this->update_error_columns[ $table ][ $col ] = true;
796+
if ( $wpdb->last_error ) {
797+
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s': %s", $col, $table, $wpdb->last_error ) );
798+
} else {
799+
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s'.", $col, $table ) );
800+
}
801+
}
802+
continue;
779803
}
804+
805+
// Only count successful updates.
806+
++$count;
780807
}
781808
}
782809

0 commit comments

Comments
 (0)