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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Database / Migrations / MigrationHelper.php
woocommerce / src / Database / Migrations Last commit date
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