From 2ce58d2821ea4b571fbb194167352a01857f9f61 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Mon, 2 May 2016 20:44:41 +0200 Subject: [PATCH] Fixed error when executing update script multiple times #533 --- php/database/update_030102.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/php/database/update_030102.php b/php/database/update_030102.php index 7607284..c18cdcf 100644 --- a/php/database/update_030102.php +++ b/php/database/update_030102.php @@ -10,36 +10,61 @@ use Lychee\Modules\Response; // Change type of the album id field $query = Database::prepare($connection, "ALTER TABLE `?` CHANGE `album` `album` BIGINT UNSIGNED NOT NULL", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); + if ($result===false) Response::error('Could not change type of the album id field!'); // Add index to the album id field -$query = Database::prepare($connection, "ALTER TABLE `?` ADD INDEX `Index_album` (`album`)", array(LYCHEE_TABLE_PHOTOS)); +$query = Database::prepare($connection, "SHOW INDEX FROM `?` WHERE KEY_NAME = 'Index_album'", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); -if ($result===false) Response::error('Could not add index to the album id field!'); + +if ($result===false) Response::error('Could not check if Index_album exists!'); + +if ($result->num_rows===0) { + + $query = Database::prepare($connection, "ALTER TABLE `?` ADD INDEX `Index_album` (`album`)", array(LYCHEE_TABLE_PHOTOS)); + $result = Database::execute($connection, $query, 'update_030102', __LINE__); + + if ($result===false) Response::error('Could not add index to the album id field!'); + +} // Add index to the star field -$query = Database::prepare($connection, "ALTER TABLE `?` ADD INDEX `Index_star` (`star`)", array(LYCHEE_TABLE_PHOTOS)); +$query = Database::prepare($connection, "SHOW INDEX FROM `?` WHERE KEY_NAME = 'Index_star'", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); -if ($result===false) Response::error('Could not add index to the star field!'); + +if ($result===false) Response::error('Could not check if Index_star exists!'); + +if ($result->num_rows===0) { + + $query = Database::prepare($connection, "ALTER TABLE `?` ADD INDEX `Index_star` (`star`)", array(LYCHEE_TABLE_PHOTOS)); + $result = Database::execute($connection, $query, 'update_030102', __LINE__); + + if ($result===false) Response::error('Could not add index to the star field!'); + +} // Change type of the checksum field $query = Database::prepare($connection, "ALTER TABLE `?` CHANGE `checksum` `checksum` CHAR(40) NULL DEFAULT NULL", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); + if ($result===false) Response::error('Could not change type of the checksum field!'); // Change type of the thumbUrl field $query = Database::prepare($connection, "ALTER TABLE `?` CHANGE `thumbUrl` `thumbUrl` CHAR(37) NOT NULL", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); + if ($result===false) Response::error('Could not change type of the thumbUrl field!'); // Change type of the id field $query = Database::prepare($connection, "ALTER TABLE `?` CHANGE `id` `id` BIGINT(14) UNSIGNED NOT NULL", array(LYCHEE_TABLE_PHOTOS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); + if ($result===false) Response::error('Could not change type of the id field!'); // Change type of the id field $query = Database::prepare($connection, "ALTER TABLE `?` CHANGE `id` `id` BIGINT(14) UNSIGNED NOT NULL", array(LYCHEE_TABLE_ALBUMS)); $result = Database::execute($connection, $query, 'update_030102', __LINE__); + if ($result===false) Response::error('Could not change type of the id field!'); // Set version