From dd2fe3869bc4d8563f1ad6d6cb128d27ec7f6620 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 27 Mar 2023 17:44:52 +0530 Subject: [PATCH 1/4] Add support for end_at ID to allow partial verification. --- .../Migrations/CustomOrderTable/CLIRunner.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index ccc17741a33..4e3cf52c14c 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -288,10 +288,16 @@ class CLIRunner { * default: 0 * --- * + * [--end-at=] + * : Order ID to end at. + * --- + * default: -1 + * --- + * * ## EXAMPLES * * # Verify migrated order data, 500 orders at a time. - * wp wc cot verify_cot_data --batch-size=500 --start-from=0 + * wp wc cot verify_cot_data --batch-size=500 --start-from=0 --end-at=10000 * * @param array $args Positional arguments passed to the command. * @param array $assoc_args Associative arguments (options) passed to the command. @@ -308,6 +314,7 @@ class CLIRunner { array( 'batch-size' => 500, 'start-from' => 0, + 'end-at' => -1, ) ); @@ -316,7 +323,9 @@ class CLIRunner { $failed_ids = array(); $processed = 0; $order_id_start = (int) $assoc_args['start-from']; - $order_count = $this->get_verify_order_count( $order_id_start ); + $order_id_end = (int) $assoc_args['end-at']; + $order_id_end = $order_id_end === - 1 ? PHP_INT_MAX : $order_id_end; + $order_count = $this->get_verify_order_count( $order_id_start, $order_id_end ); $batch_size = ( (int) $assoc_args['batch-size'] ) === 0 ? 500 : (int) $assoc_args['batch-size']; $progress = WP_CLI\Utils\make_progress_bar( 'Order Data Verification', $order_count / $batch_size ); @@ -337,8 +346,9 @@ class CLIRunner { $order_ids = $wpdb->get_col( $wpdb->prepare( - "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d ORDER BY ID ASC LIMIT %d", + "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d AND ID < %d ORDER BY ID ASC LIMIT %d", $order_id_start, + $order_id_end, $batch_size ) ); @@ -424,17 +434,19 @@ class CLIRunner { * Helper method to get count for orders needing verification. * * @param int $order_id_start Order ID to start from. + * @param int $order_id_end Order ID to end at. * @param bool $log Whether to also log an error message. * * @return int Order count. */ - private function get_verify_order_count( int $order_id_start, $log = true ) : int { + private function get_verify_order_count( int $order_id_start, int $order_id_end, $log = true ) : int { global $wpdb; $order_count = (int) $wpdb->get_var( $wpdb->prepare( - "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d", - $order_id_start + "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d AND ID < %d", + $order_id_start, + $order_id_end ) ); From bb0ca9184659b533d763ff92f0a1c4688ea4a4b2 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 27 Mar 2023 17:45:44 +0530 Subject: [PATCH 2/4] Add changelog. --- plugins/woocommerce/changelog/hpos-end_at_support | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/hpos-end_at_support diff --git a/plugins/woocommerce/changelog/hpos-end_at_support b/plugins/woocommerce/changelog/hpos-end_at_support new file mode 100644 index 00000000000..e73f5dd38f3 --- /dev/null +++ b/plugins/woocommerce/changelog/hpos-end_at_support @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Add support for end_at ID to allow partial verification. From 4245ec17b9463c226213b03421c7631b7d2b2455 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 27 Mar 2023 20:44:38 +0530 Subject: [PATCH 3/4] Yoda conditions, I have used. --- .../src/Database/Migrations/CustomOrderTable/CLIRunner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index 4e3cf52c14c..005220121d9 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -324,7 +324,7 @@ class CLIRunner { $processed = 0; $order_id_start = (int) $assoc_args['start-from']; $order_id_end = (int) $assoc_args['end-at']; - $order_id_end = $order_id_end === - 1 ? PHP_INT_MAX : $order_id_end; + $order_id_end = -1 === $order_id_end ? PHP_INT_MAX : $order_id_end; $order_count = $this->get_verify_order_count( $order_id_start, $order_id_end ); $batch_size = ( (int) $assoc_args['batch-size'] ) === 0 ? 500 : (int) $assoc_args['batch-size']; From 99e4ed4963ab0ca86a85e491a97be1bc3bf61880 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Tue, 28 Mar 2023 18:38:59 +0530 Subject: [PATCH 4/4] Make params inclusive. --- .../src/Database/Migrations/CustomOrderTable/CLIRunner.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php index 005220121d9..0b54d70cae3 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/CLIRunner.php @@ -346,7 +346,7 @@ class CLIRunner { $order_ids = $wpdb->get_col( $wpdb->prepare( - "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d AND ID < %d ORDER BY ID ASC LIMIT %d", + "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID >= %d AND ID <= %d ORDER BY ID ASC LIMIT %d", $order_id_start, $order_id_end, $batch_size @@ -444,7 +444,7 @@ class CLIRunner { $order_count = (int) $wpdb->get_var( $wpdb->prepare( - "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID > %d AND ID < %d", + "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'shop_order' AND ID >= %d AND ID <= %d", $order_id_start, $order_id_end )