Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1573,3 +1573,17 @@ Feature: Do global search/replace
"""
--old-content
"""


@require-mysql
Scenario: Warn when updating a table fails due to a database error
Given a WP install
And I run `wp db query "CREATE TABLE wp_readonly_test ( id int(11) unsigned NOT NULL AUTO_INCREMENT, data TEXT, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"`
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'
"""
6 changes: 6 additions & 0 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,9 @@ private function sql_handle_col( $col, $primary_keys, $table, $old, $new ) {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$count = (int) $wpdb->query( $wpdb->prepare( "UPDATE $table_sql SET $col_sql = REPLACE($col_sql, %s, %s);", $old, $new ) );
}
if ( $wpdb->last_error ) {
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s': %s", $col, $table, $wpdb->last_error ) );
}
}

if ( $this->verbose && 'table' === $this->format ) {
Expand Down Expand Up @@ -771,6 +774,9 @@ static function ( $key ) {
}

$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 ) );
}
Comment on lines 776 to +779
}
}

Expand Down
Loading