diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 8d08a1b09d8..4344c34f8fe 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -1069,21 +1069,6 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); } } - // Update hypervisor type for user vm to be consistent with original 2.2.4 - pstmt = conn.prepareStatement("UPDATE vm_instance SET hypervisor_type='XenServer' WHERE hypervisor_type='xenserver'"); - pstmt.executeUpdate(); - pstmt.close(); - - // Set account=systemAccount and domain=ROOT for CPVM/SSVM - pstmt = conn.prepareStatement("UPDATE vm_instance SET account_id=1, domain_id=1 WHERE type='ConsoleProxy' or type='SecondaryStorageVm'"); - pstmt.executeUpdate(); - pstmt.close(); - - // Update user statistics - upadteUserStats(conn); - - // delete orphaned (storage pool no longer exists) template_spool_ref(s) - deleteOrphanedTemplateRef(conn); } catch (SQLException e) { s_logger.error("Can't update data center ", e); @@ -1735,6 +1720,24 @@ public class Upgrade218to22 implements DbUpgrade { upgradePortForwardingRules(conn); upgradeLoadBalancingRules(conn); migrateEvents(conn); + // Update hypervisor type for user vm to be consistent with original 2.2.4 + pstmt = conn.prepareStatement("UPDATE vm_instance SET hypervisor_type='XenServer' WHERE hypervisor_type='xenserver'"); + pstmt.executeUpdate(); + pstmt.close(); + + // Set account=systemAccount and domain=ROOT for CPVM/SSVM + pstmt = conn.prepareStatement("UPDATE vm_instance SET account_id=1, domain_id=1 WHERE type='ConsoleProxy' or type='SecondaryStorageVm'"); + pstmt.executeUpdate(); + pstmt.close(); + + // Update user statistics + upadteUserStats(conn); + + // delete orphaned (storage pool no longer exists) template_spool_ref(s) + deleteOrphanedTemplateRef(conn); + + // Upgrade volumes with incorrect Destroyed field + cleanupVolumes(conn); } catch (SQLException e) { s_logger.error("Can't perform data migration ", e); throw new CloudRuntimeException("Can't perform data migration ", e); @@ -1801,4 +1804,52 @@ public class Upgrade218to22 implements DbUpgrade { throw new CloudRuntimeException("Failed to delete orphaned template_spool_ref(s): ", e); } } + + private void cleanupVolumes(Connection conn) { + try { + PreparedStatement pstmt = conn.prepareStatement("SELECT id, instance_id, account_id from volumes where destroyed=127"); + ResultSet rs = pstmt.executeQuery(); + + while (rs.next()) { + Long id = rs.getLong(1); + s_logger.debug("Volume id is " + id); + Long instanceId = rs.getLong(2); + Long accountId = rs.getLong(3); + + boolean removeVolume = false; + + pstmt = conn.prepareStatement("SELECT * from account where id=? and removed is not null"); + pstmt.setLong(1, accountId); + ResultSet rs1 = pstmt.executeQuery(); + + if (rs1.next()) { + removeVolume = true; + } + + if (instanceId != null) { + pstmt = conn.prepareStatement("SELECT * from vm_instance where id=? and removed is not null"); + pstmt.setLong(1, instanceId); + rs1 = pstmt.executeQuery(); + + if (rs1.next()) { + removeVolume = true; + } + } + + if (removeVolume) { + pstmt = conn.prepareStatement("UPDATE volumes SET state='Destroy' WHERE id=?"); + pstmt.setLong(1, id); + pstmt.executeUpdate(); + s_logger.debug("Volume with id=" + id + " is marked with Destroy state as a part of volume cleanup (it's Destroyed had 127 value)"); + } + } + rs.close(); + pstmt.close(); + + s_logger.debug("Finished cleaning up volumes with incorrect Destroyed field (127)"); + } catch (Exception e) { + s_logger.error("Failed to cleanup volumes with incorrect Destroyed field (127):", e); + throw new CloudRuntimeException("Failed to cleanup volumes with incorrect Destroyed field (127):", e); + } + } } diff --git a/setup/db/db/schema-21to22-cleanup.sql b/setup/db/db/schema-21to22-cleanup.sql index 2ab593946b7..4f879f7b59c 100644 --- a/setup/db/db/schema-21to22-cleanup.sql +++ b/setup/db/db/schema-21to22-cleanup.sql @@ -119,3 +119,5 @@ ALTER TABLE `cloud`.`vm_instance` ADD CONSTRAINT `fk_vm_instance__service_offeri 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`.`volumes` MODIFY COLUMN `state` VARCHAR(32) NOT NULL; + diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index 6c56dcddf74..4f22540d85c 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -269,9 +269,8 @@ ALTER TABLE `cloud`.`volumes` ADD COLUMN `chain_info` text; ALTER TABLE `cloud`.`volumes` MODIFY COLUMN `volume_type` VARCHAR(64) NOT NULL; ALTER TABLE `cloud`.`volumes` ADD COLUMN `state` VARCHAR(32); -UPDATE `cloud`.`volumes` SET state='Destroy' WHERE removed IS NOT NULL OR destroyed>=1 OR status='Creating' OR status='Corrupted' OR status='Failed'; +UPDATE `cloud`.`volumes` SET state='Destroy' WHERE removed IS NOT NULL OR destroyed=1 OR status='Creating' OR status='Corrupted' OR status='Failed'; UPDATE `cloud`.`volumes` SET state='Ready' WHERE removed IS NULL AND (destroyed=0 OR destroyed is NULL) AND status='Created'; -ALTER TABLE `cloud`.`volumes` MODIFY COLUMN `state` VARCHAR(32) NOT NULL; ALTER TABLE `cloud`.`vlan` ADD COLUMN `network_id` bigint unsigned NOT NULL; ALTER TABLE `cloud`.`vlan` ADD CONSTRAINT `fk_vlan__data_center_id` FOREIGN KEY `fk_vlan__data_center_id`(`data_center_id`) REFERENCES `data_center`(`id`);