Skip to content

Commit 0d531c9

Browse files
Copilotswissspidy
andcommitted
Move progress bar initialization into while loop
Moved progress bar setup from before the while loop to inside the loop on first iteration. This ensures the progress bar is created when rows are actually fetched, even if the initial COUNT query returns 0. Addresses issue where COUNT might return 0 but the while loop still fetches rows to process. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 3efdf0a commit 0d531c9

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

src/Search_Replace_Command.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -670,24 +670,24 @@ static function ( $key ) {
670670
);
671671
$order_by_sql = 'ORDER BY ' . implode( ',', $order_by_keys );
672672
$limit = 1000;
673-
674-
// Set up progress bar if appropriate
675-
$progress = null;
676-
if ( $this->should_show_progress_bar() ) {
677-
// Count total rows to process
678-
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
679-
$total_rows = $wpdb->get_var( "SELECT COUNT(*) FROM {$table_sql} {$where_key}" );
680-
if ( $total_rows > 0 ) {
681-
WP_CLI::log( sprintf( 'Updating %s.%s (%d rows)', $table, $col, $total_rows ) );
682-
$progress = \WP_CLI\Utils\make_progress_bar( sprintf( 'Processing %s.%s', $table, $col ), $total_rows );
683-
}
684-
}
673+
$progress = null;
685674

686675
// 2 errors:
687676
// - WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
688677
// - WordPress.CodeAnalysis.AssignmentInCondition -- no reason to do copy-paste for a single valid assignment in while
689678
// phpcs:ignore
690679
while ( $rows = $wpdb->get_results( "SELECT {$primary_keys_sql} FROM {$table_sql} {$where_key} {$order_by_sql} LIMIT {$limit}" ) ) {
680+
// Set up progress bar on first iteration if we have rows to process
681+
if ( null === $progress && $this->should_show_progress_bar() ) {
682+
// Count total rows to process
683+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
684+
$total_rows = $wpdb->get_var( "SELECT COUNT(*) FROM {$table_sql} {$where_key}" );
685+
if ( $total_rows > 0 ) {
686+
WP_CLI::log( sprintf( 'Updating %s.%s (%d rows)', $table, $col, $total_rows ) );
687+
$progress = \WP_CLI\Utils\make_progress_bar( sprintf( 'Processing %s.%s', $table, $col ), $total_rows );
688+
}
689+
}
690+
691691
foreach ( $rows as $keys ) {
692692
$where_sql = '';
693693
foreach ( (array) $keys as $k => $v ) {

0 commit comments

Comments
 (0)