Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for existing columns before trying to make DB/table updates #22

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions wp-rest-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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' );
Expand Down