Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable

CLIRunner::verify_cot_data()publicWC 1.0

Verify migrated order data with original posts data.

OPTIONS

[--batch-size=<batch-size>]
The number of orders to verify in each batch.
--- default: 500
---
[--start-from=<order_id>]
Order ID to start from.
--- default: 0
---
[--end-at=<order_id>]
Order ID to end at.
--- default: -1
---
[--verbose]
Whether to output errors as they happen in batch, or output them all together at the end.
--- default: false
---
[--order-types]
Comma seperated list of order types that needs to be verified. For example, --order-types=shop_order,shop_order_refund
--- default: Output of function wc_get_order_types('cot-migration')
[--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

# Verify migrated order data, 500 orders at a time.
wp wc cot verify_cot_data --batch-size=500 --start-from=0 --end-at=10000

Метод класса: CLIRunner{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$CLIRunner = new CLIRunner();
$CLIRunner->verify_cot_data( $args, $assoc_args );
$args(массив)
Positional arguments passed to the command.
По умолчанию: array()
$assoc_args(массив)
Associative arguments (options) passed to the command.
По умолчанию: array()

Код CLIRunner::verify_cot_data() WC 8.7.0

public function verify_cot_data( $args = array(), $assoc_args = array() ) {
	global $wpdb;

	if ( ! $this->synchronizer->check_orders_table_exists() ) {
		WP_CLI::error( __( 'Orders table does not exist.', 'woocommerce' ) );
		return;
	}

	$assoc_args = wp_parse_args(
		$assoc_args,
		array(
			'batch-size'  => 500,
			'start-from'  => 0,
			'end-at'      => - 1,
			'verbose'     => false,
			'order-types' => '',
			're-migrate'  => false,
		)
	);

	$batch_count    = 1;
	$total_time     = 0;
	$failed_ids     = array();
	$processed      = 0;
	$order_id_start = (int) $assoc_args['start-from'];
	$order_id_end   = (int) $assoc_args['end-at'];
	$order_id_end   = -1 === $order_id_end ? PHP_INT_MAX : $order_id_end;
	$batch_size     = ( (int) $assoc_args['batch-size'] ) === 0 ? 500 : (int) $assoc_args['batch-size'];
	$verbose        = (bool) $assoc_args['verbose'];
	$order_types    = wc_get_order_types( 'cot-migration' );
	$remigrate      = (bool) $assoc_args['re-migrate'];
	if ( ! empty( $assoc_args['order-types'] ) ) {
		$passed_order_types = array_map( 'trim', explode( ',', $assoc_args['order-types'] ) );
		$order_types        = array_intersect( $order_types, $passed_order_types );
	}

	if ( 0 === count( $order_types ) ) {
		return WP_CLI::error(
			sprintf(
			/* Translators: %s is the comma seperated list of order types. */
				__( 'Passed order type does not match any registered order types. Following order types are registered: %s', 'woocommerce' ),
				implode( ',', wc_get_order_types( 'cot-migration' ) )
			)
		);
	}

	$order_types_pl = implode( ',', array_fill( 0, count( $order_types ), '%s' ) );

	$order_count = $this->get_verify_order_count( $order_id_start, $order_id_end, $order_types, false );

	$progress = WP_CLI\Utils\make_progress_bar( 'Order Data Verification', $order_count / $batch_size );

	if ( ! $order_count ) {
		return WP_CLI::warning( __( 'There are no orders to verify, aborting.', 'woocommerce' ) );
	}

	while ( $order_count > 0 ) {
		WP_CLI::debug(
			sprintf(
				/* Translators: %1$d is the batch number, %2$d is the batch size. */
				__( 'Beginning verification for batch #%1$d (%2$d orders/batch).', 'woocommerce' ),
				$batch_count,
				$batch_size
			)
		);

		// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Inputs are prepared.
		$order_ids = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT ID FROM $wpdb->posts WHERE post_type in ( $order_types_pl ) AND ID >= %d AND ID <= %d ORDER BY ID ASC LIMIT %d",
				array_merge(
					$order_types,
					array(
						$order_id_start,
						$order_id_end,
						$batch_size,
					)
				)
			)
		);
		// phpcs:enable
		$batch_start_time            = microtime( true );
		$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;
		$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 ) {
				WP_CLI::warning(
					sprintf(
						__( 'Attempting to remigrate...', 'woocommerce' )
					)
				);
				$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 ( count( $errors_in_remigrate_batch ) > 0 ) {
					$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.',
								count( $errors_in_remigrate_batch ),
								'woocommerce'
							),
							count( $errors_in_remigrate_batch ),
							$formatted_errors
						)
					);
				} else {
					WP_CLI::warning( 'Re-migration successful.', 'woocommerce' );
				}
			}
		}

		$progress->tick();

		WP_CLI::debug(
			sprintf(
				/* Translators: %1$d is the batch number, %2$d is time taken to process batch. */
				__( 'Batch %1$d (%2$d orders) completed in %3$d seconds.', 'woocommerce' ),
				$batch_count,
				count( $order_ids ),
				$batch_total_time
			)
		);

		$order_id_start  = max( $order_ids ) + 1;
		$remaining_count = $this->get_verify_order_count( $order_id_start, $order_id_end, $order_types, false );
		if ( $remaining_count === $order_count ) {
			return WP_CLI::error( __( 'Infinite loop detected, aborting. No errors found.', 'woocommerce' ) );
		}
		$order_count = $remaining_count;
	}

	$progress->finish();
	WP_CLI::log( __( 'Verification completed.', 'woocommerce' ) );

	if ( $verbose ) {
		return;
	}

	if ( 0 === count( $failed_ids ) ) {
		return WP_CLI::success(
			sprintf(
				/* Translators: %1$d is the number of migrated orders and %2$d is time taken. */
				_n(
					'%1$d order was verified in %2$d seconds.',
					'%1$d orders were verified in %2$d seconds.',
					$processed,
					'woocommerce'
				),
				$processed,
				$total_time
			)
		);
	} else {
		$errors = wp_json_encode( $failed_ids, JSON_PRETTY_PRINT );

		return WP_CLI::error(
			sprintf(
				'%1$s %2$s',
				sprintf(
					/* Translators: %1$d is the number of migrated orders and %2$d is the execution time in seconds. */
					_n(
						'%1$d order was verified in %2$d seconds.',
						'%1$d orders were verified in %2$d seconds.',
						$processed,
						'woocommerce'
					),
					$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.',
						count( $failed_ids ),
						'woocommerce'
					),
					count( $failed_ids ),
					$errors
				)
			)
		);
	}
}