CustomOrderTable
1 year ago
MetaToCustomTableMigrator.php
1 year ago
MetaToMetaTableMigrator.php
1 year ago
MigrationHelper.php
3 years ago
TableMigrator.php
1 year ago
TableMigrator.php
168 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for all the WP posts to order table migrator. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Database\Migrations; |
| 7 | |
| 8 | /** |
| 9 | * Base class for implementing WP posts to order tables migrations handlers. |
| 10 | * It mainly contains methods to deal with error handling. |
| 11 | * |
| 12 | * @package Automattic\WooCommerce\Database\Migrations |
| 13 | */ |
| 14 | abstract class TableMigrator { |
| 15 | |
| 16 | /** |
| 17 | * An array of cumulated error messages. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $errors; |
| 22 | |
| 23 | /** |
| 24 | * Clear the error messages list. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | protected function clear_errors(): void { |
| 29 | $this->errors = array(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Add an error message to the errors list unless it's there already. |
| 34 | * |
| 35 | * @param string $error The error message to add. |
| 36 | * @return void |
| 37 | */ |
| 38 | protected function add_error( string $error ): void { |
| 39 | if ( is_null( $this->errors ) ) { |
| 40 | $this->errors = array(); |
| 41 | } |
| 42 | |
| 43 | if ( ! in_array( $error, $this->errors, true ) ) { |
| 44 | $this->errors[] = $error; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the list of error messages added. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | protected function get_errors(): array { |
| 54 | return $this->errors; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Run $wpdb->query and add the error, if any, to the errors list. |
| 59 | * |
| 60 | * @param string $query The SQL query to run. |
| 61 | * @return mixed Whatever $wpdb->query returns. |
| 62 | */ |
| 63 | protected function db_query( string $query ) { |
| 64 | $wpdb = WC()->get_global( 'wpdb' ); |
| 65 | |
| 66 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 67 | $result = $wpdb->query( $query ); |
| 68 | |
| 69 | if ( '' !== $wpdb->last_error ) { |
| 70 | $this->add_error( $wpdb->last_error ); |
| 71 | } |
| 72 | |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Run $wpdb->get_results and add the error, if any, to the errors list. |
| 78 | * |
| 79 | * @param string|null $query The SQL query to run. |
| 80 | * @param string $output Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
| 81 | * @return mixed Whatever $wpdb->get_results returns. |
| 82 | */ |
| 83 | protected function db_get_results( ?string $query = null, string $output = OBJECT ) { |
| 84 | $wpdb = WC()->get_global( 'wpdb' ); |
| 85 | |
| 86 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 87 | $result = $wpdb->get_results( $query, $output ); |
| 88 | |
| 89 | if ( '' !== $wpdb->last_error ) { |
| 90 | $this->add_error( $wpdb->last_error ); |
| 91 | } |
| 92 | |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Migrate a batch of orders, logging any database error that could arise and the exception thrown if any. |
| 98 | * |
| 99 | * @param array $entity_ids Order ids to migrate. |
| 100 | * @return array An array containing the keys 'errors' (array of strings) and 'exception' (exception object or null). |
| 101 | * |
| 102 | * @deprecated 8.0.0 Use `fetch_sanitized_migration_data` and `process_migration_data` instead. |
| 103 | */ |
| 104 | public function process_migration_batch_for_ids( array $entity_ids ): array { |
| 105 | $this->clear_errors(); |
| 106 | $exception = null; |
| 107 | |
| 108 | try { |
| 109 | $this->process_migration_batch_for_ids_core( $entity_ids ); |
| 110 | } catch ( \Exception $ex ) { |
| 111 | $exception = $ex; |
| 112 | } |
| 113 | |
| 114 | return array( |
| 115 | 'errors' => $this->get_errors(), |
| 116 | 'exception' => $exception, |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | // phpcs:disable Squiz.Commenting.FunctionComment.InvalidNoReturn, Squiz.Commenting.FunctionCommentThrowTag.Missing -- Methods are not marked abstract for back compat. |
| 121 | /** |
| 122 | * Return data to be migrated for a batch of entities. |
| 123 | * |
| 124 | * @param array $entity_ids Ids of entities to migrate. |
| 125 | * |
| 126 | * @return array[] Data to be migrated. Would be of the form: array( 'data' => array( ... ), 'errors' => array( ... ) ). |
| 127 | */ |
| 128 | public function fetch_sanitized_migration_data( array $entity_ids ) { |
| 129 | throw new \Exception( 'Not implemented' ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Process migration data for a batch of entities. |
| 134 | * |
| 135 | * @param array $data Data to be migrated. Should be of the form: array( 'data' => array( ... ) ) as returned by the `fetch_sanitized_migration_data` method. |
| 136 | * |
| 137 | * @return array Array of errors and exception if any. |
| 138 | */ |
| 139 | public function process_migration_data( array $data ) { |
| 140 | throw new \Exception( 'Not implemented' ); |
| 141 | } |
| 142 | // phpcs:enable |
| 143 | |
| 144 | /** |
| 145 | * The core method that actually performs the migration for the supplied batch of order ids. |
| 146 | * It doesn't need to deal with database errors nor with exceptions. |
| 147 | * |
| 148 | * @param array $entity_ids Order ids to migrate. |
| 149 | * @return void |
| 150 | * |
| 151 | * @deprecated 8.0.0 Use `fetch_sanitized_migration_data` and `process_migration_data` instead. |
| 152 | */ |
| 153 | abstract protected function process_migration_batch_for_ids_core( array $entity_ids ): void; |
| 154 | |
| 155 | /** |
| 156 | * Check if the amount of processed database rows matches the amount of orders to process, and log an error if not. |
| 157 | * |
| 158 | * @param string $operation Operation performed, 'insert' or 'update'. |
| 159 | * @param array|bool $received_rows_count Value returned by @wpdb after executing the query. |
| 160 | * @return void |
| 161 | */ |
| 162 | protected function maybe_add_insert_or_update_error( string $operation, $received_rows_count ) { |
| 163 | if ( false === $received_rows_count ) { |
| 164 | $this->add_error( "$operation operation didn't complete, the database query failed" ); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 |