From a3792fd9cff6a4b2a64a91601c283f97c2103698 Mon Sep 17 00:00:00 2001 From: Sheng Yang Date: Tue, 9 Jul 2013 16:34:51 -0700 Subject: [PATCH] CLOUDSTACK-3417: Prevent conflict of existed foreign keys of baremetal when upgrade Now we dropped the keys(if any) then re-add them. --- .../cloud/upgrade/dao/Upgrade410to420.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java index da683485908..96d402a577d 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java @@ -92,6 +92,7 @@ public class Upgrade410to420 implements DbUpgrade { removeFirewallServiceFromSharedNetworkOfferingWithSGService(conn); fix22xKVMSnapshots(conn); addIndexForAlert(conn); + fixBaremetalForeignKeys(conn); // storage refactor related migration // TODO: add clean-up scripts to delete the deprecated table. migrateSecondaryStorageToImageStore(conn); @@ -100,6 +101,54 @@ public class Upgrade410to420 implements DbUpgrade { migrateSnapshotStoreRef(conn); } + private void fixBaremetalForeignKeys(Connection conn) { + List keys = new ArrayList(); + keys.add("fk_external_dhcp_devices_nsp_id"); + keys.add("fk_external_dhcp_devices_host_id"); + keys.add("fk_external_dhcp_devices_pod_id"); + keys.add("fk_external_dhcp_devices_physical_network_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "baremetal_dhcp_devices", keys, true); + + keys.add("fk_external_pxe_devices_nsp_id"); + keys.add("fk_external_pxe_devices_host_id"); + keys.add("fk_external_pxe_devices_physical_network_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "baremetal_pxe_devices", keys, true); + + PreparedStatement pstmt = null; + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_dhcp_devices` ADD CONSTRAINT `fk_external_dhcp_devices_nsp_id` FOREIGN KEY (`nsp_id`) REFERENCES `physical_network_service_providers` (`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_dhcp_devices` ADD CONSTRAINT `fk_external_dhcp_devices_host_id` FOREIGN KEY (`host_id`) REFERENCES `host`(`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_dhcp_devices` ADD CONSTRAINT `fk_external_dhcp_devices_pod_id` FOREIGN KEY (`pod_id`) REFERENCES `host_pod_ref`(`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_dhcp_devices` ADD CONSTRAINT `fk_external_dhcp_devices_physical_network_id` FOREIGN KEY (`physical_network_id`) REFERENCES `physical_network`(`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + s_logger.debug("Added foreign keys for table baremetal_dhcp_devices"); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to add foreign keys to baremetal_dhcp_devices table", e); + } + + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_pxe_devices` ADD CONSTRAINT `fk_external_pxe_devices_nsp_id` FOREIGN KEY (`nsp_id`) REFERENCES `physical_network_service_providers` (`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_pxe_devices` ADD CONSTRAINT `fk_external_pxe_devices_host_id` FOREIGN KEY (`host_id`) REFERENCES `host`(`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`baremetal_pxe_devices` ADD CONSTRAINT `fk_external_pxe_devices_physical_network_id` FOREIGN KEY (`physical_network_id`) REFERENCES `physical_network`(`id`) ON DELETE CASCADE"); + pstmt.executeUpdate(); + pstmt.close(); + s_logger.debug("Added foreign keys for table baremetal_pxe_devices"); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to add foreign keys to baremetal_pxe_devices table", e); + } + } + private void addIndexForAlert(Connection conn) { //First drop if it exists. (Due to patches shipped to customers some will have the index and some wont.)