From 5ad7730e062c235646c090e31120eec076fae779 Mon Sep 17 00:00:00 2001 From: alena Date: Thu, 28 Apr 2011 15:08:58 -0700 Subject: [PATCH] Modify keys for security groups in java code instead of sql file (need to check if the key exists before dropping it) --- .../com/cloud/upgrade/dao/Upgrade218to22.java | 47 +++++++++++++++++++ setup/db/db/schema-21to22.sql | 2 - 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index f9662db7298..e26a134a101 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -1926,6 +1926,9 @@ public class Upgrade218to22 implements DbUpgrade { // Upgrade volumes with incorrect Destroyed field cleanupVolumes(conn); + + // modify network_group indexes + modifyIndexes(conn); } catch (SQLException e) { s_logger.error("Can't perform data migration ", e); throw new CloudRuntimeException("Can't perform data migration ", e); @@ -2040,4 +2043,48 @@ public class Upgrade218to22 implements DbUpgrade { throw new CloudRuntimeException("Failed to cleanup volumes with incorrect Destroyed field (127):", e); } } + + private void modifyIndexes(Connection conn) { + try { + + // removed indexes + + PreparedStatement pstmt = conn.prepareStatement("SHOW INDEX FROM security_group WHERE KEY_NAME = 'fk_network_group__account_id'"); + s_logger.debug("Query is " + pstmt); + ResultSet rs = pstmt.executeQuery(); + + if (rs.next()) { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`security_group` DROP INDEX `fk_network_group__account_id`"); + s_logger.debug("Query is " + pstmt); + pstmt.executeUpdate(); + s_logger.debug("Unique key 'fk_network_group__account_id' is removed successfully"); + } + + rs.close(); + pstmt.close(); + + pstmt = conn.prepareStatement("SHOW INDEX FROM security_group WHERE KEY_NAME = 'fk_network_group___account_id'"); + s_logger.debug("Query is " + pstmt); + rs = pstmt.executeQuery(); + + if (rs.next()) { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`security_group` DROP INDEX `fk_network_group___account_id`"); + s_logger.debug("Query is " + pstmt); + pstmt.executeUpdate(); + s_logger.debug("Unique key 'fk_network_group___account_id' is removed successfully"); + } + + rs.close(); + pstmt.close(); + + // add indexes + pstmt = conn + .prepareStatement("ALTER TABLE `cloud`.`security_group` ADD CONSTRAINT `fk_security_group___account_id` FOREIGN KEY `fk_security_group__account_id` (`account_id`) REFERENCES `account` (`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to drop indexes for 'security_group' table due to:", e); + } + } } diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index 45e8aa6645c..84af927d2bd 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -413,7 +413,6 @@ ALTER TABLE `cloud`.`storage_pool` ADD COLUMN `status` varchar(32); ALTER TABLE `cloud`.`network_group` DROP foreign key `fk_network_group__domain_id`; ALTER TABLE `cloud`.`network_group` DROP INDEX `fk_network_group__domain_id`; ALTER TABLE `cloud`.`network_group` DROP foreign key `fk_network_group___account_id`; -ALTER TABLE `cloud`.`network_group` DROP INDEX `fk_network_group___account_id`;; ALTER TABLE `cloud`.`network_group` drop index `i_network_group_name`; @@ -433,7 +432,6 @@ ALTER TABLE `cloud`.`security_ingress_rule` CHANGE COLUMN `allowed_net_grp_acct` ALTER TABLE `cloud`.`network_group_vm_map` RENAME TO `security_group_vm_map`; ALTER TABLE `cloud`.`security_group_vm_map` CHANGE COLUMN `network_group_id` `security_group_id` bigint unsigned NOT NULL; -ALTER TABLE `cloud`.`security_group` ADD CONSTRAINT `fk_security_group___account_id` FOREIGN KEY `fk_security_group__account_id` (`account_id`) REFERENCES `account` (`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`security_group` ADD CONSTRAINT `fk_security_group__domain_id` FOREIGN KEY `fk_security_group__domain_id` (`domain_id`) REFERENCES `domain` (`id`); ALTER TABLE `cloud`.`security_group` ADD INDEX `i_security_group_name`(`name`);