diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java index 3073324f151..aa1a55ce75c 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java @@ -95,6 +95,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); @@ -103,6 +104,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.)