[2.5] Before updating with DBDELTA, drop existing indexes so they can be re-added without duplicate key errors.

This commit is contained in:
Mike Jolley 2016-03-11 12:32:43 +00:00
parent 38a668e402
commit 86a0fff731
1 changed files with 29 additions and 1 deletions

View File

@ -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() );
}