From 9ca387498e523aa59ef3ceb82a7dea75e3200e29 Mon Sep 17 00:00:00 2001 From: Maura Teal Date: Mon, 17 Jul 2017 11:45:07 -0700 Subject: [PATCH] Add checks for existing columns before trying to make DB/table updates --- wp-rest-cache.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/wp-rest-cache.php b/wp-rest-cache.php index 1e81cf7..d716a46 100644 --- a/wp-rest-cache.php +++ b/wp-rest-cache.php @@ -251,8 +251,16 @@ protected static function maybe_upgrade_table() { if ( ! $table_version || 2 == (int) $table_version ) { // Version 2 adds a `rest_status_code` column to the table. global $wpdb; - $query1 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` ADD `rest_status_code` VARCHAR(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '200' AFTER `rest_args`;" ); - $query2 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` ADD `rest_key` VARCHAR(65) COLLATE utf8_unicode_ci NOT NULL AFTER `rest_md5`;" ); + // Check to see if the columns already exist before attempting to add them in + $query1 = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->rest_cache}' AND COLUMN_NAME='rest_status_code';" ); + if ( 1 != $query1 ) { + $query1 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` ADD `rest_status_code` VARCHAR(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '200' AFTER `rest_args`;" ); + } + + $query2 = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->rest_cache}' AND COLUMN_NAME='rest_key';" ); + if ( 1 != $query2 ) { + $query2 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` ADD `rest_key` VARCHAR(65) COLLATE utf8_unicode_ci NOT NULL AFTER `rest_md5`;" ); + } if ( $query1 && $query2 ) { update_site_option( self::$table_version_key, '3' ); @@ -267,8 +275,18 @@ protected static function maybe_upgrade_table() { if ( 2 == (int) $table_version ) { // Version 3 adds a `status_code` column to the table. global $wpdb; - $query1 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` DROP COLUMN `status_code`;" ); - $query2 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` DROP COLUMN `key`" ); + + // Only attempt to drop the columns if they're currently in the table. + // The check handles edge cases where the site option might not have properly updated. + $query1 = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->rest_cache}' AND COLUMN_NAME='status_code';" ); + if ( 1 == $query1 ) { + $query1 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` DROP COLUMN `status_code`;" ); + } + + $query2 = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='{$wpdb->dbname}' AND TABLE_NAME = '{$wpdb->rest_cache}' AND COLUMN_NAME='key';" ); + if ( 1 == $query2 ) { + $query2 = $wpdb->query( "ALTER TABLE `{$wpdb->rest_cache}` DROP COLUMN `key`" ); + } if ( $query1 && $query2 ) { update_site_option( self::$table_version_key, '3' );