diff --git a/server/src/com/cloud/upgrade/dao/Upgrade304to305.java b/server/src/com/cloud/upgrade/dao/Upgrade304to305.java index 4e0bb067627..86d9e5136c9 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade304to305.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade304to305.java @@ -53,6 +53,7 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade { @Override public void performDataMigration(Connection conn) { + addHostDetailsUniqueKey(conn); addVpcProvider(conn); updateRouterNetworkRef(conn); } @@ -171,4 +172,36 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade { } s_logger.debug("Done updating router/network references"); } + + private void addHostDetailsUniqueKey(Connection conn) { + s_logger.debug("Checking if host_details unique key exists, if not we will add it"); + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + pstmt = conn.prepareStatement("SHOW INDEX FROM `cloud`.`host_details` WHERE KEY_NAME = 'uk_host_id_name'"); + rs = pstmt.executeQuery(); + if (rs.next()) { + s_logger.debug("Unique key already exists on host_details - not adding new one"); + }else{ + //add the key + PreparedStatement pstmtUpdate = conn.prepareStatement("ALTER TABLE `cloud`.`host_details` ADD CONSTRAINT UNIQUE KEY `uk_host_id_name` (`host_id`, `name`)"); + pstmtUpdate.executeUpdate(); + s_logger.debug("Unique key did not exist on host_details - added new one"); + pstmtUpdate.close(); + } + } catch (SQLException e) { + throw new CloudRuntimeException("Failed to check/update the host_details unique key ", e); + } finally { + try { + if (rs != null) { + rs.close(); + } + + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException e) { + } + } + } } diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index f76d2837641..161c941f9ad 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -833,7 +833,7 @@ CREATE TABLE `cloud`.`host_details` ( `value` varchar(255) NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk_host_details__host_id` FOREIGN KEY (`host_id`) REFERENCES `host`(`id`) ON DELETE CASCADE, - CONSTRAINT UNIQUE KEY (`host_id`, `name`) + CONSTRAINT UNIQUE KEY `uk_host_id_name` (`host_id`, `name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `cloud`.`mshost` ( diff --git a/setup/db/db/schema-304to305.sql b/setup/db/db/schema-304to305.sql index 4f5e039a2d8..52ffeb93c4d 100755 --- a/setup/db/db/schema-304to305.sql +++ b/setup/db/db/schema-304to305.sql @@ -350,7 +350,5 @@ UPDATE `cloud`.`hypervisor_capabilities` SET `max_data_volumes_limit`=13 WHERE ` SET SQL_SAFE_UPDATES=1; UPDATE `cloud`.`configuration` SET description='In second, timeout for creating volume from snapshot' WHERE name='create.volume.from.snapshot.wait'; -ALTER TABLE `cloud`.`host_details` ADD CONSTRAINT UNIQUE KEY (`host_id`, `name`); - INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Account Defaults', 'DEFAULT', 'management-server', 'max.account.vpcs', '20', 'The default maximum number of vpcs that can be created for an account'); INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Project Defaults', 'DEFAULT', 'management-server', 'max.project.vpcs', '20', 'The default maximum number of vpcs that can be created for a project');