Modify keys for security groups in java code instead of sql file (need to check if the key exists before dropping it)

This commit is contained in:
alena 2011-04-28 15:08:58 -07:00
parent 1c2935a5ed
commit 5ad7730e06
2 changed files with 47 additions and 2 deletions

View File

@ -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);
}
}
}

View File

@ -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`);