Skip to content

Commit 005dc9b

Browse files
Copilotswissspidy
andcommitted
Alert user of DB table permission errors via WP_CLI::warning()
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 82068dc commit 005dc9b

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

features/search-replace.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,3 +1573,17 @@ Feature: Do global search/replace
15731573
"""
15741574
--old-content
15751575
"""
1576+
1577+
1578+
@require-mysql
1579+
Scenario: Warn when updating a table fails due to a database error
1580+
Given a WP install
1581+
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;"`
1582+
And I run `wp db query "INSERT INTO wp_readonly_test (data) VALUES ('old-value');"`
1583+
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';"`
1584+
1585+
When I try `wp search-replace old-value new-value wp_readonly_test --all-tables-with-prefix`
1586+
Then STDERR should contain:
1587+
"""
1588+
Error updating column 'data' in table 'wp_readonly_test'
1589+
"""

src/Search_Replace_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ private function sql_handle_col( $col, $primary_keys, $table, $old, $new ) {
685685
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
686686
$count = (int) $wpdb->query( $wpdb->prepare( "UPDATE $table_sql SET $col_sql = REPLACE($col_sql, %s, %s);", $old, $new ) );
687687
}
688+
if ( $wpdb->last_error ) {
689+
WP_CLI::warning( sprintf( "Error updating column '%s' in table '%s': %s", $col, $table, $wpdb->last_error ) );
690+
}
688691
}
689692

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

773776
$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 ) );
779+
}
774780
}
775781
}
776782

0 commit comments

Comments
 (0)