Reduce the scope of transaction and enable by default for all.

This commit is contained in:
Vedanshu Jain 2023-07-03 13:45:29 +05:30
parent 2ed274dd78
commit 84f54d5618
7 changed files with 231 additions and 136 deletions

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Database\Migrations\CustomOrderTable;
use Automattic\WooCommerce\Database\Migrations\MetaToMetaTableMigrator;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
/**
* Helper class to migrate records from the WordPress post meta table
@ -39,13 +40,6 @@ class PostMetaToOrderMetaMigrator extends MetaToMetaTableMigrator {
*/
protected function get_meta_config(): array {
global $wpdb;
// TODO: Remove hardcoding.
$table_names = array(
'orders' => $wpdb->prefix . 'wc_orders',
'addresses' => $wpdb->prefix . 'wc_order_addresses',
'op_data' => $wpdb->prefix . 'wc_order_operational_data',
'meta' => $wpdb->prefix . 'wc_orders_meta',
);
return array(
'source' => array(
@ -57,15 +51,15 @@ class PostMetaToOrderMetaMigrator extends MetaToMetaTableMigrator {
'meta_value_column' => 'meta_value',
),
'entity' => array(
'table_name' => $table_names['orders'],
'source_id_column' => 'id',
'id_column' => 'id',
'table_name' => $wpdb->posts,
'source_id_column' => 'ID',
'id_column' => 'ID',
),
'excluded_keys' => $this->excluded_columns,
),
'destination' => array(
'meta' => array(
'table_name' => $table_names['meta'],
'table_name' => OrdersTableDataStore::get_meta_table_name(),
'entity_id_column' => 'order_id',
'meta_key_column' => 'meta_key',
'meta_value_column' => 'meta_value',

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Database\Migrations\CustomOrderTable;
use Automattic\WooCommerce\Database\Migrations\MetaToCustomTableMigrator;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
/**
* Helper class to migrate records from the WordPress post table
@ -38,21 +39,14 @@ class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator {
*/
protected function get_schema_config(): array {
global $wpdb;
// TODO: Remove hardcoding.
$table_names = array(
'orders' => $wpdb->prefix . 'wc_orders',
'addresses' => $wpdb->prefix . 'wc_order_addresses',
'op_data' => $wpdb->prefix . 'wc_order_operational_data',
'meta' => $wpdb->prefix . 'wc_orders_meta',
);
return array(
'source' => array(
'entity' => array(
'table_name' => $table_names['orders'],
'meta_rel_column' => 'id',
'destination_rel_column' => 'id',
'primary_key' => 'id',
'table_name' => $wpdb->posts,
'meta_rel_column' => 'ID',
'destination_rel_column' => 'ID',
'primary_key' => 'ID',
),
'meta' => array(
'table_name' => $wpdb->postmeta,
@ -63,7 +57,7 @@ class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator {
),
),
'destination' => array(
'table_name' => $table_names['addresses'],
'table_name' => OrdersTableDataStore::get_addresses_table_name(),
'source_rel_column' => 'order_id',
'primary_key' => 'id',
'primary_key_type' => 'int',
@ -80,7 +74,7 @@ class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator {
$type = $this->type;
return array(
'id' => array(
'ID' => array(
'type' => 'int',
'destination' => 'order_id',
),

View File

@ -6,6 +6,7 @@
namespace Automattic\WooCommerce\Database\Migrations\CustomOrderTable;
use Automattic\WooCommerce\Database\Migrations\MetaToCustomTableMigrator;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
/**
* Helper class to migrate records from the WordPress post table
@ -22,21 +23,14 @@ class PostToOrderOpTableMigrator extends MetaToCustomTableMigrator {
*/
protected function get_schema_config(): array {
global $wpdb;
// TODO: Remove hardcoding.
$table_names = array(
'orders' => $wpdb->prefix . 'wc_orders',
'addresses' => $wpdb->prefix . 'wc_order_addresses',
'op_data' => $wpdb->prefix . 'wc_order_operational_data',
'meta' => $wpdb->prefix . 'wc_orders_meta',
);
return array(
'source' => array(
'entity' => array(
'table_name' => $table_names['orders'],
'meta_rel_column' => 'id',
'destination_rel_column' => 'id',
'primary_key' => 'id',
'table_name' => $wpdb->posts,
'meta_rel_column' => 'ID',
'destination_rel_column' => 'ID',
'primary_key' => 'ID',
),
'meta' => array(
'table_name' => $wpdb->postmeta,
@ -47,7 +41,7 @@ class PostToOrderOpTableMigrator extends MetaToCustomTableMigrator {
),
),
'destination' => array(
'table_name' => $table_names['op_data'],
'table_name' => OrdersTableDataStore::get_operational_data_table_name(),
'source_rel_column' => 'order_id',
'primary_key' => 'id',
'primary_key_type' => 'int',
@ -63,7 +57,7 @@ class PostToOrderOpTableMigrator extends MetaToCustomTableMigrator {
*/
protected function get_core_column_mapping(): array {
return array(
'id' => array(
'ID' => array(
'type' => 'int',
'destination' => 'order_id',
),

View File

@ -26,7 +26,7 @@ class PostsToOrdersMigrationController {
/**
* Array of objects used to perform the migration.
*
* @var array
* @var array[TableMigrator]
*/
private $all_migrators;
@ -40,13 +40,13 @@ class PostsToOrdersMigrationController {
*/
public function __construct() {
$this->all_migrators = array();
$this->all_migrators[] = new PostToOrderTableMigrator();
$this->all_migrators[] = new PostToOrderAddressTableMigrator( 'billing' );
$this->all_migrators[] = new PostToOrderAddressTableMigrator( 'shipping' );
$this->all_migrators[] = new PostToOrderOpTableMigrator();
$this->all_migrators[] = new PostMetaToOrderMetaMigrator( $this->get_migrated_meta_keys() );
$this->error_logger = wc_get_logger();
$this->all_migrators = array();
$this->all_migrators['order'] = new PostToOrderTableMigrator();
$this->all_migrators['order_address_billing'] = new PostToOrderAddressTableMigrator( 'billing' );
$this->all_migrators['order_address_shipping'] = new PostToOrderAddressTableMigrator( 'shipping' );
$this->all_migrators['order_operational_data'] = new PostToOrderOpTableMigrator();
$this->all_migrators['order_meta'] = new PostMetaToOrderMetaMigrator( $this->get_migrated_meta_keys() );
$this->error_logger = wc_get_logger();
}
/**
@ -56,7 +56,7 @@ class PostsToOrdersMigrationController {
*/
public function get_migrated_meta_keys() {
$migrated_meta_keys = array();
foreach ( $this->all_migrators as $migrator ) {
foreach ( $this->all_migrators as $name => $migrator ) {
if ( method_exists( $migrator, 'get_meta_column_config' ) ) {
$migrated_meta_keys = array_merge( $migrated_meta_keys, $migrator->get_meta_column_config() );
}
@ -72,23 +72,71 @@ class PostsToOrdersMigrationController {
public function migrate_orders( array $order_post_ids ): void {
$this->error_logger = WC()->call_function( 'wc_get_logger' );
$using_transactions = $this->maybe_start_transaction();
if ( null === $using_transactions ) {
return;
}
$errors_were_logged = false;
foreach ( $this->all_migrators as $migrator ) {
$errors_were_logged = $this->do_orders_migration_step( $migrator, $order_post_ids );
if ( $errors_were_logged && $using_transactions ) {
$this->rollback_transaction();
break;
$data = array();
foreach ( $this->all_migrators as $name => $migrator ) {
$data[ $name ] = $migrator->fetch_sanitized_migration_data( $order_post_ids );
if ( ! empty( $data['errors'] ) ) {
$this->handle_migration_error( $order_post_ids, $data['errors'], null, null, $name );
return;
}
}
if ( ! $errors_were_logged && $using_transactions ) {
$this->commit_transaction();
$using_transactions = $this->maybe_start_transaction();
foreach ( $this->all_migrators as $name => $migrator ) {
$results = $migrator->process_migration_data( $data[ $name ] );
$errors = array_unique( $results['errors'] );
$exception = $results['exception'];
if ( null === $exception && empty( $errors ) ) {
continue;
}
$this->handle_migration_error( $order_post_ids, $errors, $exception, $using_transactions, $name );
break;
}
$this->commit_transaction();
}
/**
* Log migration errors if any.
*
* @param array $order_post_ids List of post IDs of the orders to migrate.
* @param array $errors List of errors to log.
* @param \Exception|null $exception Exception to log.
* @param bool|null $using_transactions Whether transactions were used.
* @param string $name Name of the migrator.
*/
private function handle_migration_error( array $order_post_ids, array $errors, ?\Exception $exception, ?bool $using_transactions, string $name ) {
$batch = ArrayUtil::to_ranges_string( $order_post_ids );
if ( null !== $exception ) {
$exception_class = get_class( $exception );
$this->error_logger->error(
"$name: when processing ids $batch: ($exception_class) {$exception->getMessage()}, {$exception->getTraceAsString()}",
array(
'source' => self::LOGS_SOURCE_NAME,
'ids' => $order_post_ids,
'exception' => $exception,
)
);
}
foreach ( $errors as $error ) {
$this->error_logger->error(
"$name: when processing ids $batch: $error",
array(
'source' => self::LOGS_SOURCE_NAME,
'ids' => $order_post_ids,
'error' => $error,
)
);
}
if ( $using_transactions ) {
$this->rollback_transaction();
}
}
@ -98,10 +146,11 @@ class PostsToOrdersMigrationController {
* @return bool|null True if transaction started, false if transactions won't be used, null if transaction failed to start.
*/
private function maybe_start_transaction(): ?bool {
$transaction_isolation_level = get_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, CustomOrdersTableController::DEFAULT_DB_TRANSACTIONS_ISOLATION_LEVEL );
$transaction_isolation_level = get_option( CustomOrdersTableController::DB_TRANSACTIONS_ISOLATION_LEVEL_OPTION, CustomOrdersTableController::DEFAULT_DB_TRANSACTIONS_ISOLATION_LEVEL );
$set_transaction_isolation_level_command = "SET TRANSACTION ISOLATION LEVEL $transaction_isolation_level";
if ( ! $this->db_query( $set_transaction_isolation_level_command ) ) {
// We suppress errors in transaction isolation level setting because it's not supported by all DB engines, additionally, this might be executing in context of another transaction with a different isolation level.
if ( ! $this->db_query( $set_transaction_isolation_level_command, true ) ) {
return null;
}
@ -129,15 +178,23 @@ class PostsToOrdersMigrationController {
/**
* Execute a database query and log any errors.
*
* @param string $query The SQL query to execute.
* @param string $query The SQL query to execute.
* @param bool $supress_errors Whether to suppress errors.
*
* @return bool True if the query succeeded, false if there were errors.
*/
private function db_query( string $query ): bool {
private function db_query( string $query, bool $supress_errors = false ): bool {
$wpdb = WC()->get_global( 'wpdb' );
try {
if ( $supress_errors ) {
$suppress = $wpdb->suppress_errors( true );
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( $query );
if ( $supress_errors ) {
$wpdb->suppress_errors( $suppress );
}
} catch ( \Exception $exception ) {
$exception_class = get_class( $exception );
$this->error_logger->error(
@ -165,52 +222,6 @@ class PostsToOrdersMigrationController {
return true;
}
/**
* Performs one step of the migration for a set of order posts using one given migration class.
* All database errors and exceptions are logged.
*
* @param object $migration_class The migration class to use, must have a `process_migration_batch_for_ids(array of ids)` method.
* @param array $order_post_ids List of post IDs of the orders to migrate.
* @return bool True if errors were logged, false otherwise.
*/
private function do_orders_migration_step( object $migration_class, array $order_post_ids ): bool {
$result = $migration_class->process_migration_batch_for_ids( $order_post_ids );
$errors = array_unique( $result['errors'] );
$exception = $result['exception'];
if ( null === $exception && empty( $errors ) ) {
return false;
}
$migration_class_name = ( new \ReflectionClass( $migration_class ) )->getShortName();
$batch = ArrayUtil::to_ranges_string( $order_post_ids );
if ( null !== $exception ) {
$exception_class = get_class( $exception );
$this->error_logger->error(
"$migration_class_name: when processing ids $batch: ($exception_class) {$exception->getMessage()}, {$exception->getTraceAsString()}",
array(
'source' => self::LOGS_SOURCE_NAME,
'ids' => $order_post_ids,
'exception' => $exception,
)
);
}
foreach ( $errors as $error ) {
$this->error_logger->error(
"$migration_class_name: when processing ids $batch: $error",
array(
'source' => self::LOGS_SOURCE_NAME,
'ids' => $order_post_ids,
'error' => $error,
)
);
}
return true;
}
/**
* Verify whether the given order IDs were migrated properly or not.
*

View File

@ -218,13 +218,13 @@ abstract class MetaToCustomTableMigrator extends TableMigrator {
}
/**
* Migrate a batch of entities from the posts table to the corresponding table.
* Return data to be migrated for a batch of entities.
*
* @param array $entity_ids Ids of entities to migrate.
*
* @return void
* @return array[] Data to be migrated. Would be of the form: array( 'data' => array( ... ), 'errors' => array( ... ) ).
*/
protected function process_migration_batch_for_ids_core( array $entity_ids ): void {
public function fetch_sanitized_migration_data( $entity_ids ) {
$data = $this->fetch_data_for_migration_for_ids( $entity_ids );
foreach ( $data['errors'] as $entity_id => $errors ) {
@ -232,19 +232,56 @@ abstract class MetaToCustomTableMigrator extends TableMigrator {
$this->add_error( "Error importing data for post with id $entity_id: column $column_name: $error_message" );
}
}
return $data;
}
/**
* Migrate a batch of entities from the posts table to the corresponding table.
*
* @param array $entity_ids Ids of entities to migrate.
*
* @return void
*/
protected function process_migration_batch_for_ids_core( array $entity_ids ): void {
$data = $this->fetch_sanitized_migration_data( $entity_ids );
$this->process_migration_data( $data );
}
/**
* Process migration data for a batch of entities.
*
* @param array $data Data to be migrated. Should be of the form: array( 'data' => array( ... ) ) as returned by the `fetch_sanitized_migration_data` method.
*
* @return array Array of errors and exception if any.
*/
public function process_migration_data( array $data ) {
$this->clear_errors();
$exception = null;
if ( count( $data['data'] ) === 0 ) {
return;
return array(
'errors' => $this->get_errors(),
'exception' => null,
);
}
$entity_ids = array_keys( $data['data'] );
$existing_records = $this->get_already_existing_records( $entity_ids );
try {
$entity_ids = array_keys( $data['data'] );
$existing_records = $this->get_already_existing_records( $entity_ids );
$to_insert = array_diff_key( $data['data'], $existing_records );
$this->process_insert_batch( $to_insert );
$to_insert = array_diff_key( $data['data'], $existing_records );
$this->process_insert_batch( $to_insert );
$to_update = array_intersect_key( $data['data'], $existing_records );
$this->process_update_batch( $to_update, $existing_records );
$to_update = array_intersect_key( $data['data'], $existing_records );
$this->process_update_batch( $to_update, $existing_records );
} catch ( \Exception $e ) {
$exception = $e;
}
return array(
'errors' => $this->get_errors(),
'exception' => $exception,
);
}
/**

View File

@ -60,34 +60,71 @@ abstract class MetaToMetaTableMigrator extends TableMigrator {
$this->schema_config = $this->get_meta_config();
}
/**
* Return data to be migrated for a batch of entities.
*
* @param array $entity_ids Ids of entities to migrate.
*
* @return array[] Data to be migrated. Would be of the form: array( 'data' => array( ... ), 'errors' => array( ... ) ).
*/
public function fetch_sanitized_migration_data( $entity_ids ) {
$to_migrate = $this->fetch_data_for_migration_for_ids( $entity_ids );
if ( empty( $to_migrate ) ) {
return array(
array(),
array(),
);
}
$already_migrated = $this->get_already_migrated_records( array_keys( $to_migrate ) );
return $this->classify_update_insert_records( $to_migrate, $already_migrated );
}
/**
* Migrate a batch of entities from the posts table to the corresponding table.
*
* @param array $entity_ids Ids of entities ro migrate.
*/
protected function process_migration_batch_for_ids_core( array $entity_ids ): void {
$to_migrate = $this->fetch_data_for_migration_for_ids( $entity_ids );
if ( empty( $to_migrate ) ) {
return;
}
$sanitized_data = $this->fetch_sanitized_migration_data( $entity_ids );
$this->process_migration_data( $sanitized_data );
}
$already_migrated = $this->get_already_migrated_records( array_keys( $to_migrate ) );
/**
* Process migration data for a batch of entities.
*
* @param array $data Data to be migrated. Should be of the form: array( 'data' => array( ... ) ) as returned by the `fetch_sanitized_migration_data` method.
*
* @return array Array of errors and exception if any.
*/
public function process_migration_data( array $data ) {
$this->clear_errors();
$exception = null;
$data = $this->classify_update_insert_records( $to_migrate, $already_migrated );
$to_insert = $data[0];
$to_update = $data[1];
if ( ! empty( $to_insert ) ) {
$insert_queries = $this->generate_insert_sql_for_batch( $to_insert );
$processed_rows_count = $this->db_query( $insert_queries );
$this->maybe_add_insert_or_update_error( 'insert', $processed_rows_count );
try {
if ( ! empty( $to_insert ) ) {
$insert_queries = $this->generate_insert_sql_for_batch( $to_insert );
$processed_rows_count = $this->db_query( $insert_queries );
$this->maybe_add_insert_or_update_error( 'insert', $processed_rows_count );
}
if ( ! empty( $to_update ) ) {
$update_queries = $this->generate_update_sql_for_batch( $to_update );
$processed_rows_count = $this->db_query( $update_queries );
$this->maybe_add_insert_or_update_error( 'update', $processed_rows_count );
}
} catch ( \Exception $e ) {
$exception = $e;
}
if ( ! empty( $to_update ) ) {
$update_queries = $this->generate_update_sql_for_batch( $to_update );
$processed_rows_count = $this->db_query( $update_queries );
$this->maybe_add_insert_or_update_error( 'update', $processed_rows_count );
}
return array(
'errors' => $this->get_errors(),
'exception' => $exception,
);
}
/**
@ -179,7 +216,7 @@ abstract class MetaToMetaTableMigrator extends TableMigrator {
* ...,
* )
*/
private function fetch_data_for_migration_for_ids( array $entity_ids ): array {
public function fetch_data_for_migration_for_ids( array $entity_ids ): array {
if ( empty( $entity_ids ) ) {
return array();
}

View File

@ -94,6 +94,8 @@ abstract class TableMigrator {
*
* @param array $entity_ids Order ids to migrate.
* @return array An array containing the keys 'errors' (array of strings) and 'exception' (exception object or null).
*
* @deprecated 8.0.0 Use `fetch_sanitized_migration_data` and `process_migration_data` instead.
*/
public function process_migration_batch_for_ids( array $entity_ids ): array {
$this->clear_errors();
@ -111,12 +113,38 @@ abstract class TableMigrator {
);
}
// phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn, Squiz.Commenting.FunctionCommentThrowTag.Missing -- Methods are not marked abstract for back compat.
/**
* Return data to be migrated for a batch of entities.
*
* @param array $entity_ids Ids of entities to migrate.
*
* @return array[] Data to be migrated. Would be of the form: array( 'data' => array( ... ), 'errors' => array( ... ) ).
*/
public function fetch_sanitized_migration_data( array $entity_ids ) {
throw new \Exception( 'Not implemented' );
}
/**
* Process migration data for a batch of entities.
*
* @param array $data Data to be migrated. Should be of the form: array( 'data' => array( ... ) ) as returned by the `fetch_sanitized_migration_data` method.
*
* @return array Array of errors and exception if any.
*/
public function process_migration_data( array $data ) {
throw new \Exception( 'Not implemented' );
}
// phpcs:enable
/**
* The core method that actually performs the migration for the supplied batch of order ids.
* It doesn't need to deal with database errors nor with exceptions.
*
* @param array $entity_ids Order ids to migrate.
* @return void
*
* @deprecated 8.0.0 Use `fetch_sanitized_migration_data` and `process_migration_data` instead.
*/
abstract protected function process_migration_batch_for_ids_core( array $entity_ids ): void;