From a517653adb3975bbb0c4777d6d18ea9907bfa1bb Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Mon, 28 Feb 2011 14:36:57 -0800 Subject: [PATCH] made changes --- .../src/com/cloud/upgrade/dao/DbUpgrade.java | 3 +- .../com/cloud/upgrade/dao/Upgrade217to22.java | 61 ++++++++++++++++++- .../com/cloud/upgrade/dao/VersionDaoImpl.java | 8 ++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/server/src/com/cloud/upgrade/dao/DbUpgrade.java b/server/src/com/cloud/upgrade/dao/DbUpgrade.java index f73e6c04741..327599bed13 100644 --- a/server/src/com/cloud/upgrade/dao/DbUpgrade.java +++ b/server/src/com/cloud/upgrade/dao/DbUpgrade.java @@ -18,6 +18,7 @@ package com.cloud.upgrade.dao; import java.io.File; +import java.sql.Connection; public interface DbUpgrade { String[] getUpgradableVersionRange(); @@ -35,7 +36,7 @@ public interface DbUpgrade { /** * Performs the actual data migration. */ - void performDataMigration(); + void performDataMigration(Connection conn); /** * diff --git a/server/src/com/cloud/upgrade/dao/Upgrade217to22.java b/server/src/com/cloud/upgrade/dao/Upgrade217to22.java index 75d722a51b5..6f3326c7b0e 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade217to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade217to22.java @@ -18,8 +18,15 @@ package com.cloud.upgrade.dao; import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import com.cloud.utils.Pair; import com.cloud.utils.PropertiesUtil; +import com.cloud.utils.db.DB; import com.cloud.utils.exception.CloudRuntimeException; public class Upgrade217to22 implements DbUpgrade { @@ -34,12 +41,62 @@ public class Upgrade217to22 implements DbUpgrade { return file; } - protected void upgradeNetworks() { + @DB + protected void upgradeDataCenter() { } + protected void upgradeNetworks(Connection conn) { + String getAccountsSql = "SELECT id, domain_id FROM accounts WHERE removed IS NULL AND id > 1"; + String getDataCenterSql = "SELECT id FROM data_center"; + String getNextNetworkSequenceSql = "SELECT value from sequence where name='networks_seq'"; + String advanceNetworkSequenceSql = "UPDATE sequence set value=value+1 where name='networks_seq'"; + String insertNetworkSql = "INSERT INTO NETWORKS(id, name, display_text, traffic_type, broadcast_domain_type, gateway, cidr, mode, network_offering_id, data_center_id, guru_name, state, domain_id, account_id, dns1, dns2, guest_type, shared, is_default, created) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, , false, true, now())"; + PreparedStatement pstmt; + try { + pstmt = conn.prepareStatement(getAccountsSql); + ResultSet rs = pstmt.executeQuery(); + ArrayList> accountIds = new ArrayList>(); + while (rs.next()) { + accountIds.add(new Pair(rs.getLong(1), rs.getLong(2))); + } + rs.close(); + pstmt.close(); + pstmt = conn.prepareStatement(getDataCenterSql); + rs = pstmt.executeQuery(); + ArrayList dataCenterIds = new ArrayList(); + while (rs.next()) { + dataCenterIds.add(rs.getLong(1)); + } + rs.close(); + pstmt.close(); + + for (Pair accountId : accountIds) { + for (Long dataCenterId : dataCenterIds) { + pstmt = conn.prepareStatement(getNextNetworkSequenceSql); + rs = pstmt.executeQuery(); + rs.next(); + long seq = rs.getLong(1); + rs.close(); + pstmt.close(); + + pstmt = conn.prepareStatement(advanceNetworkSequenceSql); + pstmt.executeUpdate(); + pstmt.close(); + + pstmt = conn.prepareStatement(insertNetworkSql); + } + } + + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to perform upgrade", e); + } + } + @Override - public void performDataMigration() { + public void performDataMigration(Connection conn) { } @Override diff --git a/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java b/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java index 5280ab1bf13..8f69ea197b5 100644 --- a/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java +++ b/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java @@ -173,11 +173,17 @@ public class VersionDaoImpl extends GenericDaoBase implements V s_logger.info("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion()); Transaction txn = Transaction.currentTxn(); txn.start(); + Connection conn; + try { + conn = txn.getConnection(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to upgrade the database", e); + } File script = upgrade.getPrepareScript(); if (script != null) { runScript(script); } - upgrade.performDataMigration(); + upgrade.performDataMigration(conn); VersionVO version = new VersionVO(upgrade.getUpgradedVersion()); persist(version); txn.commit();