From 0aec9e7f7486edec3dcff745c253cca6bdc685e4 Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Tue, 21 Aug 2012 14:25:41 -0700 Subject: [PATCH] CS-16111 - DB upgrade; fixed foreign keys names Reviewed-by: Frank Zhang --- .../cloud/upgrade/dao/Upgrade304to305.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/server/src/com/cloud/upgrade/dao/Upgrade304to305.java b/server/src/com/cloud/upgrade/dao/Upgrade304to305.java index 7cac8bba472..a6ccb09e6fa 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade304to305.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade304to305.java @@ -61,6 +61,7 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade { updateRouterNetworkRef(conn); correctNetworkUsingExternalDevices(conn); updateSystemVms(conn); + fixForeignKeys(conn); } @Override @@ -368,4 +369,44 @@ public class Upgrade304to305 extends Upgrade30xBase implements DbUpgrade { s_logger.info("Successfully upgraded network using F5 and SRX devices to have a entry in the network_external_lb_device_map and network_external_firewall_device_map"); } } + + + private void fixForeignKeys(Connection conn) { + s_logger.debug("Fixing foreign keys' names in ssh_keypairs table"); + //Drop the keys (if exist) + List keys = new ArrayList(); + keys.add("fk_ssh_keypair__account_id"); + keys.add("fk_ssh_keypair__domain_id"); + keys.add("fk_ssh_keypairs__account_id"); + keys.add("fk_ssh_keypairs__domain_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "ssh_keypairs", keys, true); + + keys = new ArrayList(); + keys.add("fk_ssh_keypair__account_id"); + keys.add("fk_ssh_keypair__domain_id"); + keys.add("fk_ssh_keypairs__account_id"); + keys.add("fk_ssh_keypairs__domain_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "ssh_keypairs", keys, false); + + //insert the keys anew + try { + PreparedStatement pstmt; pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`ssh_keypairs` ADD " + + "CONSTRAINT `fk_ssh_keypair__account_id` FOREIGN KEY `fk_ssh_keypair__account_id` (`account_id`)" + + " REFERENCES `account` (`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute ssh_keypairs table update for adding account_id foreign key", e); + } + + try { + PreparedStatement pstmt; pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`ssh_keypairs` ADD CONSTRAINT" + + " `fk_ssh_keypair__domain_id` FOREIGN KEY `fk_ssh_keypair__domain_id` (`domain_id`) " + + "REFERENCES `domain` (`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute ssh_keypairs table update for adding domain_id foreign key", e); + } + } }