From 2810ceddc8592d9f1c059d6152d120d8f86d367e Mon Sep 17 00:00:00 2001 From: Jorge Torres Date: Thu, 15 Feb 2024 13:55:08 -0800 Subject: [PATCH 1/4] =?UTF-8?q?Allow=20`=E2=80=94re-migrate`=20to=20work?= =?UTF-8?q?=20even=20when=20`=E2=80=94verbose`=20is=20not=20set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Migrations/CustomOrderTable/CLIRunner.php | 111 ++++++++++-------- 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index 8c49e43887ec..6c43b25e8181 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -371,6 +371,8 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { $progress = WP_CLI\Utils\make_progress_bar( 'Order Data Verification', $order_count / $batch_size ); + $error_processing = false; + if ( ! $order_count ) { return WP_CLI::warning( __( 'There are no orders to verify, aborting.', 'woocommerce' ) ); } @@ -404,53 +406,64 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { $failed_ids_in_current_batch = $this->post_to_cot_migrator->verify_migrated_orders( $order_ids ); $failed_ids_in_current_batch = $this->verify_meta_data( $order_ids, $failed_ids_in_current_batch ); $failed_ids = $verbose ? array() : $failed_ids + $failed_ids_in_current_batch; + $error_processing = $error_processing || ! empty( $failed_ids_in_current_batch ); $processed += count( $order_ids ); $batch_total_time = microtime( true ) - $batch_start_time; $batch_count ++; $total_time += $batch_total_time; - if ( $verbose && count( $failed_ids_in_current_batch ) > 0 ) { - $errors = wp_json_encode( $failed_ids_in_current_batch, JSON_PRETTY_PRINT ); - WP_CLI::warning( - sprintf( - /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ - _n( - '%1$d error found: %2$s. Please review the error above.', - '%1$d errors found: %2$s. Please review the errors above.', - count( $failed_ids_in_current_batch ), - 'woocommerce' - ), - count( $failed_ids_in_current_batch ), - $errors - ) - ); - if ( $remigrate ) { + if ( count( $failed_ids_in_current_batch ) > 0 ) { + if ( $verbose ) { + $errors = wp_json_encode( $failed_ids_in_current_batch, JSON_PRETTY_PRINT ); WP_CLI::warning( sprintf( - __( 'Attempting to remigrate...', 'woocommerce' ) + /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ + _n( + '%1$d error found: %2$s. Please review the error above.', + '%1$d errors found: %2$s. Please review the errors above.', + count( $failed_ids_in_current_batch ), + 'woocommerce' + ), + count( $failed_ids_in_current_batch ), + $errors ) ); - $failed_ids = array_keys( $failed_ids_in_current_batch ); - $this->synchronizer->process_batch( $failed_ids ); - $errors_in_remigrate_batch = $this->post_to_cot_migrator->verify_migrated_orders( $failed_ids ); - $errors_in_remigrate_batch = $this->verify_meta_data( $failed_ids, $errors_in_remigrate_batch ); + } + + if ( $remigrate ) { + $failed_ids = $failed_ids ? array_diff_key( $failed_ids, $failed_ids_in_current_batch ) : array(); + $error_processing = ( ! $verbose ) && $failed_ids; + + $verbose && WP_CLI::warning( sprintf( __( 'Attempting to remigrate...', 'woocommerce' ) ) ); + + $failed_ids_in_current_batch_keys = array_keys( $failed_ids_in_current_batch ); + $this->synchronizer->process_batch( $failed_ids_in_current_batch_keys ); + $errors_in_remigrate_batch = $this->post_to_cot_migrator->verify_migrated_orders( $failed_ids_in_current_batch_keys ); + $errors_in_remigrate_batch = $this->verify_meta_data( $failed_ids_in_current_batch_keys, $errors_in_remigrate_batch ); + if ( count( $errors_in_remigrate_batch ) > 0 ) { + $error_processing = true; $formatted_errors = wp_json_encode( $errors_in_remigrate_batch, JSON_PRETTY_PRINT ); - WP_CLI::warning( - sprintf( - /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ - _n( - '%1$d error found: %2$s when re-migrating order. Please review the error above.', - '%1$d errors found: %2$s when re-migrating orders. Please review the errors above.', + + if ( $verbose ) { + WP_CLI::warning( + sprintf( + /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ + _n( + '%1$d error found: %2$s when re-migrating order. Please review the error above.', + '%1$d errors found: %2$s when re-migrating orders. Please review the errors above.', + count( $errors_in_remigrate_batch ), + 'woocommerce' + ), count( $errors_in_remigrate_batch ), - 'woocommerce' - ), - count( $errors_in_remigrate_batch ), - $formatted_errors - ) - ); + $formatted_errors + ) + ); + } else { + $failed_ids = $failed_ids + $errors_in_remigrate_batch; + } } else { - WP_CLI::warning( 'Re-migration successful.', 'woocommerce' ); + $verbose && WP_CLI::warning( 'Re-migration successful.', 'woocommerce' ); } } } @@ -478,11 +491,7 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { $progress->finish(); WP_CLI::log( __( 'Verification completed.', 'woocommerce' ) ); - if ( $verbose ) { - return; - } - - if ( 0 === count( $failed_ids ) ) { + if ( ! $error_processing ) { return WP_CLI::success( sprintf( /* Translators: %1$d is the number of migrated orders and %2$d is time taken. */ @@ -497,8 +506,6 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { ) ); } else { - $errors = wp_json_encode( $failed_ids, JSON_PRETTY_PRINT ); - return WP_CLI::error( sprintf( '%1$s %2$s', @@ -513,17 +520,19 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { $processed, $total_time ), - sprintf( - /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ - _n( - '%1$d error found: %2$s. Please review the error above.', - '%1$d errors found: %2$s. Please review the errors above.', + $failed_ids + ? sprintf( + /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */ + _n( + '%1$d error found: %2$s. Please review the error above.', + '%1$d errors found: %2$s. Please review the errors above.', + count( $failed_ids ), + 'woocommerce' + ), count( $failed_ids ), - 'woocommerce' - ), - count( $failed_ids ), - $errors - ) + wp_json_encode( $failed_ids, JSON_PRETTY_PRINT ) + ) + : __( 'Please review the errors above.', 'woocommerce' ) ) ); } From 7b2cae7abcbc63e40c3b04e7ac932968e672eed4 Mon Sep 17 00:00:00 2001 From: Jorge Torres Date: Thu, 15 Feb 2024 13:56:24 -0800 Subject: [PATCH 2/4] Add changelog --- plugins/woocommerce/changelog/fix-41248 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-41248 diff --git a/plugins/woocommerce/changelog/fix-41248 b/plugins/woocommerce/changelog/fix-41248 new file mode 100644 index 000000000000..1c8c58489a32 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-41248 @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Allow --re-migrate to work without --verbose in HPOS CLI verification tool. From ed85320aad74e82fd415a1557709f4f3386a7dc1 Mon Sep 17 00:00:00 2001 From: Jorge Torres Date: Fri, 23 Feb 2024 10:04:35 -0300 Subject: [PATCH 3/4] Docblock update --- .../src/Database/Migrations/CustomOrderTable/CLIRunner.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index 6c43b25e8181..3f44263c49d4 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -308,7 +308,6 @@ public function migrate( $args = array(), $assoc_args = array() ) { * * [--re-migrate] * : Attempt to re-migrate orders that failed verification. You should only use this option when you have never run the site with HPOS as authoritative source of order data yet, or you have manually checked the reported errors, otherwise, you risk stale data overwriting the more recent data. - * This option can only be enabled when --verbose flag is also set. * default: false * * ## EXAMPLES From 19dc888782024cd88ff97c1946faa3212f7ff9d0 Mon Sep 17 00:00:00 2001 From: Jorge Torres Date: Thu, 29 Feb 2024 16:15:58 -0300 Subject: [PATCH 4/4] Indicate that remigration was attempted in errors array --- .../src/Database/Migrations/CustomOrderTable/CLIRunner.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index 3f44263c49d4..65446c9b870e 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -459,6 +459,12 @@ public function verify_cot_data( $args = array(), $assoc_args = array() ) { ) ); } else { + array_walk( + $errors_in_remigrate_batch, + function( &$errors_for_order ) { + $errors_for_order[] = array( 'remigrate_failed' => true ); + } + ); $failed_ids = $failed_ids + $errors_in_remigrate_batch; } } else {