From 86a0fff73142b0af3717ccb3c82a2fd693ebaf53 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 11 Mar 2016 12:32:43 +0000 Subject: [PATCH] [2.5] Before updating with DBDELTA, drop existing indexes so they can be re-added without duplicate key errors. --- includes/class-wc-install.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index c2f9e5d7c7e..58f6d70e21b 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -338,7 +338,8 @@ class WC_Install { require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); /** - * Before updating with DBDELTA, remove any primary keys which could be modified due to schema updates. + * Before updating with DBDELTA, remove any primary keys which could be + * modified due to schema updates. */ if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_downloadable_product_permissions';" ) ) { if ( ! $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_downloadable_product_permissions` LIKE 'permission_id';" ) ) { @@ -346,6 +347,33 @@ class WC_Install { } } + /** + * Before updating with DBDELTA, drop existing indexes so they can be + * re-added without duplicate key errors. Index lengths were added + * in 2.5.3. + */ + $indexes_to_remove = array( + 'woocommerce_attribute_taxonomies' => 'attribute_name', + 'woocommerce_termmeta' => 'meta_key', + 'woocommerce_downloadable_product_permissions' => 'download_order_key_product', + 'woocommerce_order_itemmeta' => 'meta_key', + 'woocommerce_tax_rates' => 'tax_rate_country', + 'woocommerce_tax_rates' => 'tax_rate_state', + 'woocommerce_tax_rates' => 'tax_rate_class', + 'woocommerce_tax_rate_locations' => 'location_type_code', + 'woocommerce_shipping_zone_locations' => 'location_type_code', + ); + + foreach ( $indexes_to_remove as $table => $key ) { + $table = esc_sql( $wpdb->prefix . $table ); + $key = esc_sql( $key ); + if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table}';" ) ) { + if ( $wpdb->get_var( "SHOW INDEX FROM {$table} WHERE KEY_NAME = '{$key}';" ) ) { + $wpdb->query( "ALTER TABLE {$table} DROP INDEX `{$key}`;" ); + } + } + } + dbDelta( self::get_schema() ); }