CustomOrderTable
1 year ago
MetaToCustomTableMigrator.php
1 year ago
MetaToMetaTableMigrator.php
1 year ago
MigrationHelper.php
3 years ago
TableMigrator.php
1 year ago
MigrationHelper.php
289 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper class with utility functions for migrations. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Database\Migrations; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer; |
| 9 | use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil; |
| 10 | use Automattic\WooCommerce\Utilities\ArrayUtil; |
| 11 | use Automattic\WooCommerce\Utilities\StringUtil; |
| 12 | |
| 13 | /** |
| 14 | * Helper class to assist with migration related operations. |
| 15 | */ |
| 16 | class MigrationHelper { |
| 17 | |
| 18 | /** |
| 19 | * Placeholders that we will use in building $wpdb queries. |
| 20 | * |
| 21 | * @var string[] |
| 22 | */ |
| 23 | private static $wpdb_placeholder_for_type = array( |
| 24 | 'int' => '%d', |
| 25 | 'decimal' => '%f', |
| 26 | 'string' => '%s', |
| 27 | 'date' => '%s', |
| 28 | 'date_epoch' => '%s', |
| 29 | 'bool' => '%d', |
| 30 | ); |
| 31 | |
| 32 | /** |
| 33 | * Helper method to escape backtick in various schema fields. |
| 34 | * |
| 35 | * @param array $schema_config Schema config. |
| 36 | * |
| 37 | * @return array Schema config escaped for backtick. |
| 38 | */ |
| 39 | public static function escape_schema_for_backtick( array $schema_config ): array { |
| 40 | array_walk( $schema_config['source']['entity'], array( self::class, 'escape_and_add_backtick' ) ); |
| 41 | array_walk( $schema_config['source']['meta'], array( self::class, 'escape_and_add_backtick' ) ); |
| 42 | array_walk( $schema_config['destination'], array( self::class, 'escape_and_add_backtick' ) ); |
| 43 | return $schema_config; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Helper method to escape backtick in column and table names. |
| 48 | * WP does not provide a method to escape table/columns names yet, but hopefully soon in @link https://core.trac.wordpress.org/ticket/52506 |
| 49 | * |
| 50 | * @param string|array $identifier Column or table name. |
| 51 | * |
| 52 | * @return array|string|string[] Escaped identifier. |
| 53 | */ |
| 54 | public static function escape_and_add_backtick( $identifier ) { |
| 55 | return '`' . str_replace( '`', '``', $identifier ) . '`'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return $wpdb->prepare placeholder for data type. |
| 60 | * |
| 61 | * @param string $type Data type. |
| 62 | * |
| 63 | * @return string $wpdb placeholder. |
| 64 | */ |
| 65 | public static function get_wpdb_placeholder_for_type( string $type ): string { |
| 66 | return self::$wpdb_placeholder_for_type[ $type ]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Generates ON DUPLICATE KEY UPDATE clause to be used in migration. |
| 71 | * |
| 72 | * @param array $columns List of column names. |
| 73 | * |
| 74 | * @return string SQL clause for INSERT...ON DUPLICATE KEY UPDATE |
| 75 | */ |
| 76 | public static function generate_on_duplicate_statement_clause( array $columns ): string { |
| 77 | $db_util = wc_get_container()->get( DatabaseUtil::class ); |
| 78 | return $db_util->generate_on_duplicate_statement_clause( $columns ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Migrate state codes in all the required places in the database, needed after they change for a given country. |
| 83 | * |
| 84 | * @param string $country_code The country that has the states for which the migration is needed. |
| 85 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 86 | * @return bool True if there are more records that need to be migrated, false otherwise. |
| 87 | */ |
| 88 | public static function migrate_country_states( string $country_code, array $old_to_new_states_mapping ): bool { |
| 89 | $more_remaining = self::migrate_country_states_for_orders( $country_code, $old_to_new_states_mapping ); |
| 90 | if ( ! $more_remaining ) { |
| 91 | self::migrate_country_states_for_misc_data( $country_code, $old_to_new_states_mapping ); |
| 92 | } |
| 93 | return $more_remaining; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Migrate state codes in all the required places in the database (except orders). |
| 98 | * |
| 99 | * @param string $country_code The country that has the states for which the migration is needed. |
| 100 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 101 | * @return void |
| 102 | */ |
| 103 | private static function migrate_country_states_for_misc_data( string $country_code, array $old_to_new_states_mapping ): void { |
| 104 | self::migrate_country_states_for_shipping_locations( $country_code, $old_to_new_states_mapping ); |
| 105 | self::migrate_country_states_for_tax_rates( $country_code, $old_to_new_states_mapping ); |
| 106 | self::migrate_country_states_for_store_location( $country_code, $old_to_new_states_mapping ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Migrate state codes in the shipping locations table. |
| 111 | * |
| 112 | * @param string $country_code The country that has the states for which the migration is needed. |
| 113 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 114 | * @return void |
| 115 | */ |
| 116 | private static function migrate_country_states_for_shipping_locations( string $country_code, array $old_to_new_states_mapping ): void { |
| 117 | global $wpdb; |
| 118 | |
| 119 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 120 | |
| 121 | $sql = "SELECT location_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_code LIKE '{$country_code}:%'"; |
| 122 | $locations_data = $wpdb->get_results( $sql, ARRAY_A ); |
| 123 | |
| 124 | foreach ( $locations_data as $location_data ) { |
| 125 | $old_state_code = substr( $location_data['location_code'], 3 ); |
| 126 | if ( array_key_exists( $old_state_code, $old_to_new_states_mapping ) ) { |
| 127 | $new_location_code = "{$country_code}:{$old_to_new_states_mapping[$old_state_code]}"; |
| 128 | $update_query = $wpdb->prepare( |
| 129 | "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code=%s WHERE location_id=%d", |
| 130 | $new_location_code, |
| 131 | $location_data['location_id'] |
| 132 | ); |
| 133 | $wpdb->query( $update_query ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Migrate the state code for the store location. |
| 142 | * |
| 143 | * @param string $country_code The country that has the states for which the migration is needed. |
| 144 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 145 | * @return void |
| 146 | */ |
| 147 | private static function migrate_country_states_for_store_location( string $country_code, array $old_to_new_states_mapping ): void { |
| 148 | $store_location = get_option( 'woocommerce_default_country', '' ); |
| 149 | if ( StringUtil::starts_with( $store_location, "{$country_code}:" ) ) { |
| 150 | $old_location_code = substr( $store_location, 3 ); |
| 151 | if ( array_key_exists( $old_location_code, $old_to_new_states_mapping ) ) { |
| 152 | $new_location_code = "{$country_code}:{$old_to_new_states_mapping[$old_location_code]}"; |
| 153 | update_option( 'woocommerce_default_country', $new_location_code ); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Migrate state codes for orders in the orders table and in the posts table. |
| 160 | * It will migrate only N*2*(number of states) records, being N equal to 100 by default |
| 161 | * but this number can be modified via the woocommerce_migrate_country_states_for_orders_batch_size filter. |
| 162 | * |
| 163 | * @param string $country_code The country that has the states for which the migration is needed. |
| 164 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 165 | * @return bool True if there are more records that need to be migrated, false otherwise. |
| 166 | */ |
| 167 | private static function migrate_country_states_for_orders( string $country_code, array $old_to_new_states_mapping ): bool { |
| 168 | global $wpdb; |
| 169 | |
| 170 | /** |
| 171 | * Filters the value of N, where the maximum count of database records that will be updated in one single run of migrate_country_states_for_orders |
| 172 | * is N*2*count($old_to_new_states_mapping) if the woocommerce_orders table exists, or N*count($old_to_new_states_mapping) otherwise. |
| 173 | * |
| 174 | * @param int $batch_size Default value for the count of records to update. |
| 175 | * @param string $country_code Country code for the update. |
| 176 | * @param array $old_to_new_states_mapping Associative array of old to new state codes. |
| 177 | * |
| 178 | * @since 7.2.0 |
| 179 | */ |
| 180 | $limit = apply_filters( 'woocommerce_migrate_country_states_for_orders_batch_size', 100, $country_code, $old_to_new_states_mapping ); |
| 181 | $cot_exists = wc_get_container()->get( DataSynchronizer::class )->check_orders_table_exists(); |
| 182 | |
| 183 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 184 | |
| 185 | foreach ( $old_to_new_states_mapping as $old_state => $new_state ) { |
| 186 | if ( $cot_exists ) { |
| 187 | $update_query = $wpdb->prepare( |
| 188 | "UPDATE {$wpdb->prefix}wc_order_addresses SET state=%s WHERE country=%s AND state=%s LIMIT %d", |
| 189 | $new_state, |
| 190 | $country_code, |
| 191 | $old_state, |
| 192 | $limit |
| 193 | ); |
| 194 | |
| 195 | $wpdb->query( $update_query ); |
| 196 | } |
| 197 | |
| 198 | // We need to split the update query for the postmeta table in two, select + update, |
| 199 | // because MySQL doesn't support the LIMIT keyword in multi-table UPDATE statements. |
| 200 | |
| 201 | $select_meta_ids_query = $wpdb->prepare( |
| 202 | "SELECT meta_id FROM {$wpdb->prefix}postmeta, |
| 203 | (SELECT DISTINCT post_id FROM {$wpdb->prefix}postmeta |
| 204 | WHERE (meta_key = '_billing_country' OR meta_key='_shipping_country') AND meta_value=%s) |
| 205 | AS states_in_country |
| 206 | WHERE (meta_key='_billing_state' OR meta_key='_shipping_state') |
| 207 | AND meta_value=%s |
| 208 | AND {$wpdb->postmeta}.post_id = states_in_country.post_id |
| 209 | LIMIT %d", |
| 210 | $country_code, |
| 211 | $old_state, |
| 212 | $limit |
| 213 | ); |
| 214 | |
| 215 | $meta_ids = $wpdb->get_results( $select_meta_ids_query, ARRAY_A ); |
| 216 | if ( ! empty( $meta_ids ) ) { |
| 217 | $meta_ids = ArrayUtil::select( $meta_ids, 'meta_id' ); |
| 218 | $meta_ids_as_comma_separated = '(' . join( ',', $meta_ids ) . ')'; |
| 219 | |
| 220 | $update_query = $wpdb->prepare( |
| 221 | "UPDATE {$wpdb->prefix}postmeta |
| 222 | SET meta_value=%s |
| 223 | WHERE meta_id IN {$meta_ids_as_comma_separated}", |
| 224 | $new_state |
| 225 | ); |
| 226 | |
| 227 | $wpdb->query( $update_query ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | $states_as_comma_separated = "('" . join( "','", array_keys( $old_to_new_states_mapping ) ) . "')"; |
| 232 | |
| 233 | $posts_exist_query = $wpdb->prepare( |
| 234 | " |
| 235 | SELECT 1 FROM {$wpdb->prefix}postmeta |
| 236 | WHERE (meta_key='_billing_state' OR meta_key='_shipping_state') |
| 237 | AND meta_value IN {$states_as_comma_separated} |
| 238 | AND post_id IN ( |
| 239 | SELECT post_id FROM {$wpdb->prefix}postmeta WHERE |
| 240 | (meta_key = '_billing_country' OR meta_key='_shipping_country') |
| 241 | AND meta_value=%s |
| 242 | )", |
| 243 | $country_code |
| 244 | ); |
| 245 | |
| 246 | if ( $cot_exists ) { |
| 247 | $more_exist_query = $wpdb->prepare( |
| 248 | " |
| 249 | SELECT EXISTS( |
| 250 | SELECT 1 FROM {$wpdb->prefix}wc_order_addresses |
| 251 | WHERE country=%s AND state IN {$states_as_comma_separated} |
| 252 | ) |
| 253 | OR EXISTS ( |
| 254 | {$posts_exist_query} |
| 255 | )", |
| 256 | $country_code |
| 257 | ); |
| 258 | } else { |
| 259 | $more_exist_query = "SELECT EXISTS ({$posts_exist_query})"; |
| 260 | } |
| 261 | |
| 262 | return (int) ( $wpdb->get_var( $more_exist_query ) ) !== 0; |
| 263 | |
| 264 | // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Migrate state codes in the tax rates table. |
| 269 | * |
| 270 | * @param string $country_code The country that has the states for which the migration is needed. |
| 271 | * @param array $old_to_new_states_mapping An associative array where keys are the old state codes and values are the new state codes. |
| 272 | * @return void |
| 273 | */ |
| 274 | private static function migrate_country_states_for_tax_rates( string $country_code, array $old_to_new_states_mapping ): void { |
| 275 | global $wpdb; |
| 276 | |
| 277 | foreach ( $old_to_new_states_mapping as $old_state_code => $new_state_code ) { |
| 278 | $wpdb->query( |
| 279 | $wpdb->prepare( |
| 280 | "UPDATE {$wpdb->prefix}woocommerce_tax_rates SET tax_rate_state=%s WHERE tax_rate_country=%s AND tax_rate_state=%s", |
| 281 | $new_state_code, |
| 282 | $country_code, |
| 283 | $old_state_code |
| 284 | ) |
| 285 | ); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 |