diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/MetaToMetaTableMigrator.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/MetaToMetaTableMigrator.php index 7a1335c43b8..a41578fc288 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/MetaToMetaTableMigrator.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/MetaToMetaTableMigrator.php @@ -14,7 +14,7 @@ use Automattic\WooCommerce\Database\Migrations\MigrationHelper; * * @package Automattic\WooCommerce\Database\Migrations\CustomOrderTable */ -class MetaToMetaTableMigrator { +abstract class MetaToMetaTableMigrator { /** * Schema config, see __construct for more details. @@ -24,14 +24,29 @@ class MetaToMetaTableMigrator { private $schema_config; /** - * MetaToMetaTableMigrator constructor. + * Store errors along with entity IDs from migrations. * - * @param array $schema_config This parameters provides general but essential information about tables under migrations. Must be of the form- - * TODO: Add structure. + * @var array */ - public function __construct( $schema_config ) { - // TODO: Validate params. - $this->schema_config = $schema_config; + protected $errors; + + public abstract function get_meta_config(); + + public function __construct() { + $this->schema_config = $this->get_meta_config(); + $this->errors = array(); + } + + public function process_migration_batch_for_ids( $entity_ids ) { + global $wpdb; + $data_to_migrate = $this->fetch_data_for_migration_for_ids( $entity_ids ); + $insert_queries = $this->generate_insert_sql_for_batch( $data_to_migrate['data'], 'insert' ); + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_queries should already be escaped in the generating function. + $result = $wpdb->query( $insert_queries ); + if ( count( $data_to_migrate['data'] ) !== $result ) { + // TODO: Find and log entity ids that were not inserted. + echo 'error'; + } } /** diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostMetaToOrderMetaMigrator.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostMetaToOrderMetaMigrator.php new file mode 100644 index 00000000000..e155a93009f --- /dev/null +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostMetaToOrderMetaMigrator.php @@ -0,0 +1,55 @@ +excluded_columns = $excluded_columns; + parent::__construct(); + } + + /** + * Generate config for meta data migration. + * + * @return array Meta data migration config. + */ + public function get_meta_config() { + global $wpdb; + // TODO: Remove hardcoding. + $this->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( + 'meta' => array( + 'table_name' => $wpdb->postmeta, + 'entity_id_column' => 'post_id', + 'meta_key_column' => 'meta_key', + 'meta_value_column' => 'meta_value', + ), + 'excluded_keys' => $this->excluded_columns, + ), + 'destination' => array( + 'meta' => array( + 'table_name' => $this->table_names['meta'], + 'entity_id_column' => 'order_id', + 'meta_key_column' => 'meta_key', + 'meta_value_column' => 'meta_value', + 'entity_id_type' => 'int', + ), + 'entity' => array( + 'table_name' => $this->table_names['orders'], + 'source_id_column' => 'post_id', + 'id_column' => 'id', + ), + ), + ); + } +} diff --git a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostToCOTMigrator.php b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostToCOTMigrator.php index 81f5f1e97c9..a2d7a463054 100644 --- a/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostToCOTMigrator.php +++ b/plugins/woocommerce/src/Database/Migrations/CustomOrderTable/WPPostToCOTMigrator.php @@ -66,58 +66,13 @@ class WPPostToCOTMigrator { $this->shipping_address_table_migrator = new WPPostToOrderAddressTableMigrator( 'shipping' ); $this->operation_data_table_migrator = new WPPostToOrderOpTableMigrator(); - $meta_data_config = $this->get_config_for_meta_table(); - - $this->meta_table_migrator = new MetaToMetaTableMigrator( $meta_data_config ); - - $this->error_logger = new MigrationErrorLogger(); - } - - /** - * Generate config for meta data migration. - * - * @return array Meta data migration config. - */ - private function get_config_for_meta_table() { - global $wpdb; - // TODO: Remove hardcoding. - $this->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', - ); - $excluded_columns = array_keys( $this->order_table_migrator->get_meta_column_config() ); $excluded_columns = array_merge( $excluded_columns, array_keys( $this->billing_address_table_migrator->get_meta_column_config() ) ); $excluded_columns = array_merge( $excluded_columns, array_keys( $this->shipping_address_table_migrator->get_meta_column_config() ) ); $excluded_columns = array_merge( $excluded_columns, array_keys( $this->operation_data_table_migrator->get_meta_column_config() ) ); - return array( - 'source' => array( - 'meta' => array( - 'table_name' => $wpdb->postmeta, - 'entity_id_column' => 'post_id', - 'meta_key_column' => 'meta_key', - 'meta_value_column' => 'meta_value', - ), - 'excluded_keys' => $excluded_columns, - ), - 'destination' => array( - 'meta' => array( - 'table_name' => $this->table_names['meta'], - 'entity_id_column' => 'order_id', - 'meta_key_column' => 'meta_key', - 'meta_value_column' => 'meta_value', - 'entity_id_type' => 'int', - ), - 'entity' => array( - 'table_name' => $this->table_names['orders'], - 'source_id_column' => 'post_id', - 'id_column' => 'id', - ), - ), - ); + $this->meta_table_migrator = new WPPostMetaToOrderMetaMigrator( $excluded_columns ); + $this->error_logger = new MigrationErrorLogger(); } /** @@ -135,6 +90,7 @@ class WPPostToCOTMigrator { $this->process_migration_for_ids( $order_post_ids ); $last_post_migrated = max( $order_post_ids ); $this->update_checkpoint( $last_post_migrated ); + return false; } @@ -148,27 +104,10 @@ class WPPostToCOTMigrator { $this->billing_address_table_migrator->process_migration_batch_for_ids( $order_post_ids ); $this->shipping_address_table_migrator->process_migration_batch_for_ids( $order_post_ids ); $this->operation_data_table_migrator->process_migration_batch_for_ids( $order_post_ids ); - $this->process_meta_migration( $order_post_ids ); + $this->meta_table_migrator->process_migration_batch_for_ids( $order_post_ids ); // TODO: Return merged error array. } - /** - * Process migration for metadata for given post ids. - * - * @param array $order_post_ids Post IDs. - */ - private function process_meta_migration( $order_post_ids ) { - global $wpdb; - $data_to_migrate = $this->meta_table_migrator->fetch_data_for_migration_for_ids( $order_post_ids ); - $insert_queries = $this->meta_table_migrator->generate_insert_sql_for_batch( $data_to_migrate['data'], 'insert' ); - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $insert_queries should already be escaped in the generating function. - $result = $wpdb->query( $insert_queries ); - if ( count( $data_to_migrate['data'] ) !== $result ) { - // TODO: Find and log entity ids that were not inserted. - echo 'error'; - } - } - /** * Method to migrate single record. *