diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 8bb9ce7e037..6377fa490dc 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -1101,6 +1101,7 @@ public class Upgrade218to22 implements DbUpgrade { ResultSet dcSet = pstmt.executeQuery(); if (!dcSet.next()) { + s_logger.error("Unable to get data_center information as a part of user_statistics update"); throw new CloudRuntimeException("Unable to get data_center information as a part of user_statistics update"); } @@ -1113,17 +1114,31 @@ public class Upgrade218to22 implements DbUpgrade { pstmt = conn.prepareStatement("SELECT id from vm_instance where account_id=? AND data_center_id=? AND type='DomainRouter'"); pstmt.setLong(1, accountId); pstmt.setLong(2, dataCenterId); + s_logger.debug("Query is " + pstmt); ResultSet rs1 = pstmt.executeQuery(); if (!rs1.next()) { - throw new CloudRuntimeException("Unable to update user_statistics table with device id"); - } + // check if there are any non-removed user vms existing for this account + // if all vms are expunged, and there is no domR, just skip this record + pstmt = conn.prepareStatement("SELECT * from vm_instance where account_id=? AND data_center_id=? AND removed IS NULL"); + pstmt.setLong(1, accountId); + pstmt.setLong(2, dataCenterId); + ResultSet nonRemovedVms = pstmt.executeQuery(); + if (nonRemovedVms.next()) { + s_logger.error("Unable to update user_statistics table with device id"); + throw new CloudRuntimeException("Unable to update user_statistics table with device id"); + } else { + s_logger.debug("Account id=" + accountId + " doesn't own any user vms and domRs, so skipping user_statistics update"); + continue; + } + } Long deviceId = rs1.getLong(1); pstmt = conn.prepareStatement("UPDATE user_statistics SET device_id=? where id=?"); pstmt.setLong(1, deviceId); pstmt.setLong(2, id); + s_logger.debug("Query is " + pstmt); pstmt.executeUpdate(); pstmt = conn.prepareStatement(""); diff --git a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java index 371ce97983b..6576a9f9d9f 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java @@ -190,33 +190,34 @@ public class Upgrade222to224 implements DbUpgrade { Long id = rs.getLong(1); Long instanceId = rs.getLong(2); - // Check if domR is already expunged; we shouldn't update user stats in this case as nics are gone too - pstmt = conn.prepareStatement("SELECT * from vm_instance where id=? and removed is not null"); - pstmt.setLong(1, instanceId); - ResultSet rs1 = pstmt.executeQuery(); + if (instanceId != null && instanceId.longValue() != 0) { + // Check if domR is already expunged; we shouldn't update user stats in this case as nics are gone too + pstmt = conn.prepareStatement("SELECT * from vm_instance where id=? and removed is not null"); + pstmt.setLong(1, instanceId); + ResultSet rs1 = pstmt.executeQuery(); - if (rs1.next()) { - s_logger.debug("Not updating user_statistics table for domR id=" + instanceId + " as domR is already expunged"); - continue; + if (rs1.next()) { + s_logger.debug("Not updating user_statistics table for domR id=" + instanceId + " as domR is already expunged"); + continue; + } + + pstmt = conn.prepareStatement("SELECT network_id FROM nics WHERE instance_id=? AND mode='Dhcp'"); + pstmt.setLong(1, instanceId); + ResultSet rs2 = pstmt.executeQuery(); + + if (!rs2.next()) { + throw new CloudRuntimeException("Failed to update user_statistics table as a part of 222 to 224 upgrade: couldn't get network_id from nics table"); + } + + Long networkId = rs2.getLong(1); + + if (networkId != null) { + pstmt = conn.prepareStatement("UPDATE user_statistics SET network_id=? where id=?"); + pstmt.setLong(1, networkId); + pstmt.setLong(2, id); + pstmt.executeUpdate(); + } } - - pstmt = conn.prepareStatement("SELECT network_id FROM nics WHERE instance_id=? AND mode='Dhcp'"); - pstmt.setLong(1, instanceId); - ResultSet rs2 = pstmt.executeQuery(); - - if (!rs2.next()) { - throw new CloudRuntimeException("Failed to update user_statistics table as a part of 222 to 224 upgrade: couldn't get network_id from nics table"); - } - - Long networkId = rs2.getLong(1); - - if (networkId != null) { - pstmt = conn.prepareStatement("UPDATE user_statistics SET network_id=? where id=?"); - pstmt.setLong(1, networkId); - pstmt.setLong(2, id); - pstmt.executeUpdate(); - } - } s_logger.debug("Successfully update user_statistics table with network_ids as a part of 222 to 224 upgrade");