diff --git a/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java b/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java index 2b985fb0aa8..6cf18e82251 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java @@ -60,6 +60,7 @@ public class Upgrade229to2210 implements DbUpgrade { @Override public void performDataMigration(Connection conn) { updateFirewallRules(conn); + updateSnapshots(conn); } @Override @@ -67,6 +68,43 @@ public class Upgrade229to2210 implements DbUpgrade { return null; } + private void updateSnapshots(Connection conn) { + PreparedStatement pstmt = null; + ResultSet rs = null; + long currentSnapshotId = 0; + try { + pstmt = conn.prepareStatement("select id, prev_snap_id from snapshots where sechost_id is NULL and prev_snap_id is not NULL order by id"); + rs = pstmt.executeQuery(); + while (rs.next()) { + long id = rs.getLong(1); + long preSnapId = rs.getLong(2); + currentSnapshotId = id; + pstmt = conn.prepareStatement("select sechost_id from snapshots where id=? and sechost_id is not NULL"); + pstmt.setLong(1, preSnapId); + ResultSet sechost = pstmt.executeQuery(); + if (sechost.next()) { + long secHostId = sechost.getLong(1); + pstmt = conn.prepareStatement("update snapshots set sechost_id=? where id=?"); + pstmt.setLong(1, secHostId); + pstmt.setLong(2, id); + pstmt.executeUpdate(); + } + } + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to update snapshots id=" + currentSnapshotId, e); + } finally { + try { + if (rs != null) { + rs.close(); + } + + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException e) { + } + } + } private void updateFirewallRules(Connection conn) { PreparedStatement pstmt = null;