Skip to content

Commit a4588af

Browse files
committed
Add fix
1 parent 017fd36 commit a4588af

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

features/search-replace.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ Feature: Do global search/replace
11511151
echo "CREATE TABLE \`wp_123_test\` (\`key\` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT, \`text\` TEXT, PRIMARY KEY (\`key\`) );" > test_db.sql
11521152
echo "INSERT INTO \`wp_123_test\` (\`text\`) VALUES" >> test_db.sql
11531153
index=1
1154-
while [[ $index -le 999 ]];
1154+
while [[ $index -le 199 ]];
11551155
do
11561156
echo "('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc'),('abc')," >> test_db.sql
11571157
index=`expr $index + 1`
@@ -1164,13 +1164,13 @@ Feature: Do global search/replace
11641164
When I run `wp search-replace --dry-run 'abc' 'def' --all-tables-with-prefix --skip-columns=guid,domain --precise`
11651165
Then STDOUT should contain:
11661166
"""
1167-
Success: 10000 replacements to be made.
1167+
Success: 2000 replacements to be made.
11681168
"""
11691169

11701170
When I run `wp search-replace 'abc' 'def' --all-tables-with-prefix --skip-columns=guid,domain --precise`
11711171
Then STDOUT should contain:
11721172
"""
1173-
Success: Made 10000 replacements.
1173+
Success: Made 2000 replacements.
11741174
"""
11751175

11761176
When I run `wp search-replace --dry-run 'abc' 'def' --all-tables-with-prefix --skip-columns=guid,domain --precise`

src/Search_Replace_Command.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ static function ( $key ) {
552552
$limit = 1000;
553553
$offset = 0;
554554

555+
// Updates have to be deferred to after the chunking is completed, as
556+
// the offset will otherwise not work correctly.
557+
$updates = [];
558+
555559
// 2 errors:
556560
// - WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
557561
// - WordPress.CodeAnalysis.AssignmentInCondition -- no reason to do copy-paste for a single valid assignment in while
@@ -560,7 +564,7 @@ static function ( $key ) {
560564
foreach ( $rows as $keys ) {
561565
$where_sql = '';
562566
foreach ( (array) $keys as $k => $v ) {
563-
if ( $where_sql !== '' ) {
567+
if ( '' !== $where_sql ) {
564568
$where_sql .= ' AND ';
565569
}
566570
$where_sql .= self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v );
@@ -584,21 +588,24 @@ static function ( $key ) {
584588
$replacer->clear_log_data();
585589
}
586590

587-
if ( $this->dry_run ) {
588-
$count++;
589-
} else {
591+
$count++;
592+
if ( ! $this->dry_run ) {
590593
$update_where = array();
591594
foreach ( (array) $keys as $k => $v ) {
592595
$update_where[ $k ] = $v;
593596
}
594597

595-
$count += $wpdb->update( $table, array( $col => $value ), $update_where );
598+
$updates[] = [ $table, array( $col => $value ), $update_where ];
596599
}
597600
}
598601

599602
$offset += $limit;
600603
}
601604

605+
foreach ( $updates as $update ) {
606+
$wpdb->update( ...$update );
607+
}
608+
602609
if ( $this->verbose && 'table' === $this->format ) {
603610
$time = round( microtime( true ) - $this->start_time, 3 );
604611
WP_CLI::log( sprintf( '%d rows affected using PHP (in %ss).', $count, $time ) );

0 commit comments

Comments
 (0)