From 60cbef1fb8d676e7fc10e1e0e7ec1505af0fecb7 Mon Sep 17 00:00:00 2001 From: Edison Su Date: Mon, 29 Aug 2011 15:27:53 -0700 Subject: [PATCH] bug 11217: add db upgrade script, if sechost_id is missing of a snapshot, add one got from presnapshot_id status 11217: resovled fixed Conflicts: server/src/com/cloud/upgrade/dao/Upgrade229to2210.java --- .../cloud/upgrade/dao/Upgrade229to2210.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java b/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java index 5ce4468083c..b7ba2e9057e 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade229to2210.java @@ -60,13 +60,52 @@ public class Upgrade229to2210 implements DbUpgrade { @Override public void performDataMigration(Connection conn) { updateFirewallRules(conn); + updateSnapshots(conn); } @Override public File[] getCleanupScripts() { 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; ResultSet rs = null;