diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 3b1e1af173b..8a6cdedab13 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -1078,6 +1078,9 @@ public class Upgrade218to22 implements DbUpgrade { // Update user statistics upadteUserStats(conn); + // delete orphaned (storage pool no longer exists) template_spool_ref(s) + deleteOrphanedTemplateRef(conn); + } catch (SQLException e) { throw new CloudRuntimeException("Can't update data center ", e); } @@ -1300,7 +1303,8 @@ public class Upgrade218to22 implements DbUpgrade { rs = pstmt.executeQuery(); if (!rs.next()) { - throw new CloudRuntimeException("Unable to find public IP address " + publicIp); + s_logger.warn("Unable to find public IP address " + publicIp + "; skipping lb rule id=" + originalLbId + " from update"); + continue; } int ipAddressId = rs.getInt(1); @@ -1749,4 +1753,39 @@ public class Upgrade218to22 implements DbUpgrade { public boolean supportsRollingUpgrade() { return false; } + + private void deleteOrphanedTemplateRef(Connection conn) { + try { + PreparedStatement pstmt = conn.prepareStatement("SELECT id, pool_id from template_spool_ref"); + ResultSet rs = pstmt.executeQuery(); + if (!rs.next()) { + s_logger.debug("No records in template_spool_ref, skipping this upgrade part"); + return; + } + + while (rs.next()) { + Long id = rs.getLong(1); + Long poolId = rs.getLong(2); + + pstmt = conn.prepareStatement("SELECT * from storage_pool where id=?"); + pstmt.setLong(1, poolId); + ResultSet rs1 = pstmt.executeQuery(); + + if (!rs1.next()) { + s_logger.debug("Orphaned template_spool_ref record is found (storage pool doesn't exist any more0) id=" + id + "; so removing the record"); + pstmt = conn.prepareStatement("DELETE FROM template_spool_ref where id=?"); + pstmt.setLong(1, id); + pstmt.executeUpdate(); + } + + } + rs.close(); + pstmt.close(); + + s_logger.debug("Finished deleting orphaned template_spool_ref(s)"); + } catch (Exception e) { + s_logger.error("Failed to delete orphaned template_spool_ref(s): ", e); + throw new CloudRuntimeException("Failed to delete orphaned template_spool_ref(s): ", e); + } + } } diff --git a/setup/db/db/schema-21to22-cleanup.sql b/setup/db/db/schema-21to22-cleanup.sql index b10ec054981..2ab593946b7 100644 --- a/setup/db/db/schema-21to22-cleanup.sql +++ b/setup/db/db/schema-21to22-cleanup.sql @@ -117,3 +117,5 @@ ALTER TABLE `cloud`.`user_ip_address` ADD CONSTRAINT `fk_user_ip_address__networ ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__account_id` FOREIGN KEY `fk_vm_instance__account_id` (`account_id`) REFERENCES `account` (`id`); ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__service_offering_id` FOREIGN KEY `fk_vm_instance__service_offering_id` (`service_offering_id`) REFERENCES `service_offering` (`id`); +ALTER TABLE `cloud`.`template_spool_ref` ADD CONSTRAINT `fk_template_spool_ref__pool_id` FOREIGN KEY (`pool_id`) REFERENCES `storage_pool`(`id`) ON DELETE CASCADE; + diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index f8fe02a0297..6c56dcddf74 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -6,7 +6,6 @@ ALTER TABLE `cloud`.`cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'Clo ALTER TABLE `cloud`.`vm_template` ADD COLUMN `hypervisor_type` varchar(32) COMMENT 'hypervisor that the template is belonged to'; ALTER TABLE `cloud`.`vm_template` ADD COLUMN `extractable` int(1) unsigned NOT NULL default 0 COMMENT 'Is this template extractable'; ALTER TABLE `cloud`.`template_spool_ref` ADD CONSTRAINT `fk_template_spool_ref__template_id` FOREIGN KEY (`template_id`) REFERENCES `vm_template`(`id`); -ALTER TABLE `cloud`.`template_spool_ref` ADD CONSTRAINT `fk_template_spool_ref__pool_id` FOREIGN KEY (`pool_id`) REFERENCES `storage_pool`(`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`guest_os` modify `name` varchar(255) ;