From d3e59b770b87d13c412effa32aa10cf49e219da5 Mon Sep 17 00:00:00 2001 From: Gerhard Date: Mon, 15 Oct 2018 14:07:48 +0200 Subject: [PATCH] Do not try and create primary key using upgrade routine, dbdelta does that automatically. Just drop the key if it exists and add a new unique key. --- includes/wc-update-functions.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/wc-update-functions.php b/includes/wc-update-functions.php index ff086d5eafc..c5482176130 100644 --- a/includes/wc-update-functions.php +++ b/includes/wc-update-functions.php @@ -1999,9 +1999,20 @@ function wc_update_350_reviews_comment_type() { function wc_update_350_change_woocommerce_sessions_schema() { global $wpdb; - $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}woocommerce_sessions` DROP PRIMARY KEY, DROP KEY `session_id`, ADD PRIMARY KEY(`session_id`), ADD UNIQUE KEY(`session_key`)" - ); + $results = $wpdb->get_results( " + SELECT CONSTRAINT_NAME + FROM information_schema.TABLE_CONSTRAINTS + WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}' + AND CONSTRAINT_TYPE = 'UNIQUE' + AND CONSTRAINT_NAME = 'session_id' + AND TABLE_NAME = '{$wpdb->prefix}woocommerce_sessions' + " ); + + if ( $results ) { + $wpdb->query( + "ALTER TABLE `{$wpdb->prefix}woocommerce_sessions` DROP KEY `session_id`, ADD UNIQUE KEY(`session_key`)" + ); + } } /**