From 7acf712e45ed091e821a66eb26f307426fe78b99 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 9 Aug 2016 14:28:41 +0200 Subject: [PATCH] Check if there is a comment_type index before adding one Adding this check to avoid a MySQL error when running the query if the index already exists. --- includes/class-wc-install.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index bc78e9ffd9b..64652c75a08 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -404,9 +404,13 @@ class WC_Install { dbDelta( self::get_schema() ); - // Add an index to the field comment_type to improve the response time of the query - // used by WC_Comments::wp_count_comments() to get the number of comments by type. - $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" ); + $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE Column_name = 'comment_type' and Key_name = 'comment_type'" ); + + if ( is_null( $index_exists ) ) { + // Add an index to the field comment_type to improve the response time of the query + // used by WC_Comments::wp_count_comments() to get the number of comments by type. + $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" ); + } } /**