cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahu...@apache.org
Subject [32/56] [abbrv] [partial] Moved most of the VOs and DAOs from server package into engine-schema as well
Date Mon, 06 May 2013 23:35:12 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java b/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java
new file mode 100755
index 0000000..50eb47b
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/DatabaseIntegrityChecker.java
@@ -0,0 +1,264 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.maint.Version;
+import com.cloud.upgrade.dao.VersionDao;
+
+import com.cloud.utils.component.AdapterBase;
+import com.cloud.utils.component.ComponentLifecycle;
+import com.cloud.utils.component.SystemIntegrityChecker;
+import com.cloud.utils.db.GlobalLock;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+@Component
+@Local(value = {SystemIntegrityChecker.class})
+public class DatabaseIntegrityChecker extends AdapterBase implements SystemIntegrityChecker {
+	private final Logger s_logger = Logger.getLogger(DatabaseIntegrityChecker.class);
+	
+    @Inject VersionDao _dao;
+    
+    public DatabaseIntegrityChecker() {
+    	setRunLevel(ComponentLifecycle.RUN_LEVEL_FRAMEWORK_BOOTSTRAP);
+    }
+	
+	/*
+	 * Check if there were multiple hosts connect to the same local storage. This is from a 2.1.x bug,
+	 * we didn't prevent adding host with the same IP.
+	 */
+	private String formatDuplicateHostToReadText(Long poolId, ResultSet rs) throws SQLException {
+		boolean has = false;
+		StringBuffer buf = new StringBuffer();
+		String fmt = "|%1$-8s|%2$-16s|%3$-16s|%4$-24s|%5$-8s|\n";
+		String head = String.format(fmt, "id", "status", "removed", "private_ip_address", "pool_id");
+		buf.append(head);
+		while (rs.next()) {
+			String h = String.format(fmt, rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4), poolId);
+			buf.append(h);
+			has = true;
+		}
+		
+		if (!has) {
+			throw new CloudRuntimeException("Local storage with Id " + poolId + " shows there are multiple hosts connect to it, but 'select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)' returns nothing");
+		} else {
+			return buf.toString();
+		}
+	}
+	
+	private Boolean checkDuplicateHostWithTheSameLocalStorage() {
+		Transaction txn = Transaction.open("Integrity");
+		txn.start();
+		try {
+			Connection conn;
+			try {
+				conn = txn.getConnection();
+				PreparedStatement pstmt = conn.prepareStatement("SELECT pool_id FROM host INNER JOIN storage_pool_host_ref INNER JOIN storage_pool WHERE storage_pool.id = storage_pool_host_ref.pool_id and storage_pool.pool_type='LVM' AND host.id=storage_pool_host_ref.host_id AND host.removed IS NULL group by pool_id having count(*) > 1");
+				ResultSet rs = pstmt.executeQuery();
+				
+				boolean noDuplicate = true;
+				StringBuffer helpInfo = new StringBuffer();
+				String note = "DATABASE INTEGRITY ERROR\nManagement server detected there are some hosts connect to the same loacal storage, please contact CloudStack support team for solution. Below are detialed info, please attach all of them to CloudStack support. Thank you\n";
+				helpInfo.append(note);
+				while (rs.next()) {
+					long poolId = rs.getLong(1);
+					pstmt = conn.prepareStatement("select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)");
+					pstmt.setLong(1, poolId);
+					ResultSet dhrs = pstmt.executeQuery();
+					String help = formatDuplicateHostToReadText(poolId, dhrs);
+					helpInfo.append(help);
+					helpInfo.append("\n");
+					noDuplicate = false;
+				}
+				
+				if (noDuplicate) {
+					s_logger.debug("No duplicate hosts with the same local storage found in database");
+				} else {
+					s_logger.error(helpInfo.toString());
+				}
+				
+				return noDuplicate;
+			} catch (SQLException e) {
+				s_logger.error("Unable to check duplicate hosts with the same local storage in database", e);
+				throw new CloudRuntimeException("Unable to check duplicate hosts with the same local storage in database", e);
+			}
+		} finally {
+			txn.commit();
+			txn.close();
+		}
+	}
+	
+	private boolean check21to22PremiumUprage(Connection conn) throws SQLException {
+	    PreparedStatement pstmt = conn.prepareStatement("show tables in cloud_usage");
+	    ResultSet rs = pstmt.executeQuery();
+	    int num = 0;
+	    
+	    while (rs.next()) {
+	        String tableName = rs.getString(1);
+	        if (tableName.equalsIgnoreCase("usage_event") || tableName.equalsIgnoreCase("usage_port_forwarding") || tableName.equalsIgnoreCase("usage_network_offering")) {
+	            num ++;
+	            s_logger.debug("Checking 21to22PremiumUprage table " + tableName + " found");
+	        }
+	        if (num == 3) {
+	            return true;
+	        }
+	    }
+	    
+	    return false;
+	}
+	
+	private boolean isColumnExisted(Connection conn, String dbName, String tableName, String column) throws SQLException {
+	    PreparedStatement pstmt = conn.prepareStatement(String.format("describe %1$s.%2$s", dbName, tableName));
+        ResultSet rs = pstmt.executeQuery();
+        boolean found = false;
+        while (rs.next()) {
+            if (column.equalsIgnoreCase(rs.getString(1))) {
+                s_logger.debug(String.format("Column %1$s.%2$s.%3$s found", dbName, tableName, column));
+                found = true;
+                break;
+            }
+        }
+        return found;
+	}
+	
+    private boolean check221to222PremiumUprage(Connection conn) throws SQLException {
+        if (!isColumnExisted(conn, "cloud_usage", "cloud_usage", "network_id")) {
+            return false;
+        }
+
+        if (!isColumnExisted(conn, "cloud_usage", "usage_network", "network_id")) {
+            return false;
+        }
+
+        return isColumnExisted(conn, "cloud_usage", "user_statistics", "network_id");
+    }
+	
+	private boolean check222to224PremiumUpgrade(Connection conn) throws SQLException {
+	    if (!isColumnExisted(conn, "cloud_usage", "usage_vm_instance", "hypervisor_type")) {
+            return false;
+        }
+	    
+	    return isColumnExisted(conn, "cloud_usage", "usage_event", "resource_type");
+	}
+	
+	private boolean checkMissedPremiumUpgradeFor228() {
+		Transaction txn = Transaction.open("Integrity");
+		txn.start();
+		try {
+		    String dbVersion = _dao.getCurrentVersion();
+
+		    if ( dbVersion == null )
+		    	return false;
+
+		    if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch("2.2.8")) != 0) {
+		        return true;
+		    }
+		    
+			Connection conn;
+			try {
+				conn = txn.getConnection();
+				PreparedStatement pstmt = conn.prepareStatement("show databases");
+				ResultSet rs = pstmt.executeQuery();
+				boolean hasUsage = false;
+				while (rs.next()) {
+				    String dbName = rs.getString(1);
+				    if (dbName.equalsIgnoreCase("cloud_usage")) {
+				        hasUsage = true;
+				        break;
+				    }
+				}
+				
+				if (!hasUsage) {
+				    s_logger.debug("No cloud_usage found in database, no need to check missed premium upgrade");
+				    return true;
+				}
+				
+				if (!check21to22PremiumUprage(conn)) {
+				    s_logger.error("21to22 premium upgrade missed");
+				    return false;
+				}
+				
+				if (!check221to222PremiumUprage(conn)) {
+				    s_logger.error("221to222 premium upgrade missed");
+				    return false;
+				}
+				
+				if (!check222to224PremiumUpgrade(conn)) {
+                    s_logger.error("222to224 premium upgrade missed");
+                    return false;
+				}
+				
+				return true;
+			} catch (SQLException e) {
+				s_logger.error("Unable to check missed premiumg upgrade");
+				throw new CloudRuntimeException("Unable to check missed premiumg upgrade");
+			}
+		} finally {
+			txn.commit();
+			txn.close();
+		}
+	}
+	
+	@Override
+	public void check() {
+		GlobalLock lock = GlobalLock.getInternLock("DatabaseIntegrity");
+        try {
+            s_logger.info("Grabbing lock to check for database integrity.");
+            if (!lock.lock(20 * 60)) {
+                throw new CloudRuntimeException("Unable to acquire lock to check for database integrity.");
+            }
+
+            try {
+                s_logger.info("Performing database integrity check");
+                if (!checkDuplicateHostWithTheSameLocalStorage()) {
+                    throw new CloudRuntimeException("checkDuplicateHostWithTheSameLocalStorage detected error");
+                }
+                
+                if (!checkMissedPremiumUpgradeFor228()) {
+                    s_logger.error("Your current database version is 2.2.8, management server detected some missed premium upgrade, please contact CloudStack support and attach log file. Thank you!");
+                    throw new CloudRuntimeException("Detected missed premium upgrade");
+                }
+            } finally {
+                lock.unlock();
+            }
+        } finally {
+            lock.releaseRef();
+        }
+	}
+	
+	@Override
+	public boolean start() {
+		try {
+			check();
+		} catch(Exception e) {
+			s_logger.error("System integrity check exception", e);
+			System.exit(1);
+		}
+		return true;
+	}
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java b/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java
new file mode 100755
index 0000000..9bc0ba5
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/DatabaseUpgradeChecker.java
@@ -0,0 +1,369 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.TreeMap;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.maint.Version;
+import com.cloud.upgrade.dao.DbUpgrade;
+import com.cloud.upgrade.dao.Upgrade217to218;
+import com.cloud.upgrade.dao.Upgrade218to22;
+import com.cloud.upgrade.dao.Upgrade218to224DomainVlans;
+import com.cloud.upgrade.dao.Upgrade2210to2211;
+import com.cloud.upgrade.dao.Upgrade2211to2212;
+import com.cloud.upgrade.dao.Upgrade2212to2213;
+import com.cloud.upgrade.dao.Upgrade2213to2214;
+import com.cloud.upgrade.dao.Upgrade2214to30;
+import com.cloud.upgrade.dao.Upgrade221to222;
+import com.cloud.upgrade.dao.Upgrade222to224;
+import com.cloud.upgrade.dao.Upgrade224to225;
+import com.cloud.upgrade.dao.Upgrade225to226;
+import com.cloud.upgrade.dao.Upgrade227to228;
+import com.cloud.upgrade.dao.Upgrade228to229;
+import com.cloud.upgrade.dao.Upgrade229to2210;
+import com.cloud.upgrade.dao.Upgrade301to302;
+import com.cloud.upgrade.dao.Upgrade302to40;
+import com.cloud.upgrade.dao.Upgrade30to301;
+import com.cloud.upgrade.dao.Upgrade40to41;
+import com.cloud.upgrade.dao.Upgrade410to420;
+import com.cloud.upgrade.dao.UpgradeSnapshot217to224;
+import com.cloud.upgrade.dao.UpgradeSnapshot223to224;
+import com.cloud.upgrade.dao.VersionDao;
+import com.cloud.upgrade.dao.VersionDaoImpl;
+import com.cloud.upgrade.dao.VersionVO;
+import com.cloud.upgrade.dao.VersionVO.Step;
+import com.cloud.utils.component.SystemIntegrityChecker;
+import com.cloud.utils.db.GlobalLock;
+import com.cloud.utils.db.ScriptRunner;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+@Local(value = { SystemIntegrityChecker.class })
+public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
+    private final Logger s_logger = Logger.getLogger(DatabaseUpgradeChecker.class);
+
+    protected HashMap<String, DbUpgrade[]> _upgradeMap = new HashMap<String, DbUpgrade[]>();
+
+    VersionDao _dao;
+
+    public DatabaseUpgradeChecker() {
+        _dao = new VersionDaoImpl();
+        _upgradeMap.put("2.1.7", new DbUpgrade[] { new Upgrade217to218(), new Upgrade218to22(), new Upgrade221to222(),
+                new UpgradeSnapshot217to224(), new Upgrade222to224(), new Upgrade224to225(), new Upgrade225to226(),
+                new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(),
+                new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.1.8", new DbUpgrade[] { new Upgrade218to22(), new Upgrade221to222(), new UpgradeSnapshot217to224(),
+                new Upgrade222to224(), new Upgrade218to224DomainVlans(), new Upgrade224to225(), new Upgrade225to226(),
+                new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.1.9", new DbUpgrade[] { new Upgrade218to22(), new Upgrade221to222(), new UpgradeSnapshot217to224(),
+                new Upgrade222to224(), new Upgrade218to224DomainVlans(), new Upgrade224to225(), new Upgrade225to226(),
+                new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(),
+                new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.1", new DbUpgrade[] { new Upgrade221to222(), new UpgradeSnapshot223to224(), new Upgrade222to224(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.2", new DbUpgrade[] { new Upgrade222to224(), new UpgradeSnapshot223to224(), new Upgrade224to225(),
+                new Upgrade225to226(), new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.3", new DbUpgrade[] { new Upgrade222to224(), new UpgradeSnapshot223to224(), new Upgrade224to225(),
+                new Upgrade225to226(), new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.4", new DbUpgrade[] { new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228(),
+                new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.5", new DbUpgrade[] { new Upgrade225to226(), new Upgrade227to228(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.6", new DbUpgrade[] { new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.7", new DbUpgrade[] { new Upgrade227to228(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.8", new DbUpgrade[] { new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30()
+        , new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.9", new DbUpgrade[] { new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.10", new DbUpgrade[] { new Upgrade2210to2211(), new Upgrade2211to2212(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.11", new DbUpgrade[] { new Upgrade2211to2212(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.12", new DbUpgrade[] { new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(),
+                new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.13", new DbUpgrade[] { new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("2.2.14", new DbUpgrade[] { new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("3.0.0", new DbUpgrade[] { new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("3.0.1", new DbUpgrade[] { new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("3.0.2", new DbUpgrade[] { new Upgrade302to40(), new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("4.0.0", new DbUpgrade[] { new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("4.0.1", new DbUpgrade[] { new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("4.0.2", new DbUpgrade[] { new Upgrade40to41(), new Upgrade410to420() });
+
+        _upgradeMap.put("4.1.0", new DbUpgrade[] { new Upgrade410to420() });
+    }
+
+    protected void runScript(Connection conn, File file) {
+        try {
+            FileReader reader = new FileReader(file);
+            ScriptRunner runner = new ScriptRunner(conn, false, true);
+            runner.runScript(reader);
+        } catch (FileNotFoundException e) {
+            s_logger.error("Unable to find upgrade script: " + file.getAbsolutePath(), e);
+            throw new CloudRuntimeException("Unable to find upgrade script: " + file.getAbsolutePath(), e);
+        } catch (IOException e) {
+            s_logger.error("Unable to read upgrade script: " + file.getAbsolutePath(), e);
+            throw new CloudRuntimeException("Unable to read upgrade script: " + file.getAbsolutePath(), e);
+        } catch (SQLException e) {
+            s_logger.error("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
+            throw new CloudRuntimeException("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
+        }
+    }
+
+    protected void upgrade(String dbVersion, String currentVersion) {
+        s_logger.info("Database upgrade must be performed from " + dbVersion + " to " + currentVersion);
+
+        String trimmedDbVersion = Version.trimToPatch(dbVersion);
+        String trimmedCurrentVersion = Version.trimToPatch(currentVersion);
+
+        DbUpgrade[] upgrades = _upgradeMap.get(trimmedDbVersion);
+        if (upgrades == null) {
+            s_logger.error("There is no upgrade path from " + dbVersion + " to " + currentVersion);
+            throw new CloudRuntimeException("There is no upgrade path from " + dbVersion + " to " + currentVersion);
+        }
+
+        if (Version.compare(trimmedCurrentVersion, upgrades[upgrades.length - 1].getUpgradedVersion()) != 0) {
+            s_logger.error("The end upgrade version is actually at " + upgrades[upgrades.length - 1].getUpgradedVersion() + " but our management server code version is at " + currentVersion);
+            throw new CloudRuntimeException("The end upgrade version is actually at " + upgrades[upgrades.length - 1].getUpgradedVersion() + " but our management server code version is at "
+                    + currentVersion);
+        }
+
+        boolean supportsRollingUpgrade = true;
+        for (DbUpgrade upgrade : upgrades) {
+            if (!upgrade.supportsRollingUpgrade()) {
+                supportsRollingUpgrade = false;
+                break;
+            }
+        }
+
+        if (!supportsRollingUpgrade && false) { // FIXME: Needs to detect if there are management servers running
+                                                // ClusterManagerImpl.arePeersRunning(null)) {
+            s_logger.error("Unable to run upgrade because the upgrade sequence does not support rolling update and there are other management server nodes running");
+            throw new CloudRuntimeException("Unable to run upgrade because the upgrade sequence does not support rolling update and there are other management server nodes running");
+        }
+
+        for (DbUpgrade upgrade : upgrades) {
+            s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1]
+                    + " to " + upgrade.getUpgradedVersion());
+            Transaction txn = Transaction.open("Upgrade");
+            txn.start();
+            try {
+                Connection conn;
+                try {
+                    conn = txn.getConnection();
+                } catch (SQLException e) {
+                    s_logger.error("Unable to upgrade the database", e);
+                    throw new CloudRuntimeException("Unable to upgrade the database", e);
+                }
+                File[] scripts = upgrade.getPrepareScripts();
+                if (scripts != null) {
+                    for (File script : scripts) {
+                        runScript(conn, script);
+                    }
+                }
+
+                upgrade.performDataMigration(conn);
+                boolean upgradeVersion = true;
+
+                if (upgrade.getUpgradedVersion().equals("2.1.8")) {
+                    // we don't have VersionDao in 2.1.x
+                    upgradeVersion = false;
+                } else if (upgrade.getUpgradedVersion().equals("2.2.4")) {
+                    try {
+                        // specifically for domain vlan update from 2.1.8 to 2.2.4
+                        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
+                        ResultSet rs = pstmt.executeQuery();
+                        if (rs.next()) {
+                            upgradeVersion = false;
+                        }
+                    } catch (SQLException e) {
+                        throw new CloudRuntimeException("Unable to update the version table", e);
+                    }
+                }
+
+                if (upgradeVersion) {
+                    VersionVO version = new VersionVO(upgrade.getUpgradedVersion());
+                    _dao.persist(version);
+                }
+
+                txn.commit();
+            } finally {
+                txn.close();
+            }
+        }
+
+        if (true) { // FIXME Needs to detect if management servers are running
+                    // !ClusterManagerImpl.arePeersRunning(trimmedCurrentVersion)) {
+            s_logger.info("Cleaning upgrades because all management server are now at the same version");
+            TreeMap<String, List<DbUpgrade>> upgradedVersions = new TreeMap<String, List<DbUpgrade>>();
+
+            for (DbUpgrade upgrade : upgrades) {
+                String upgradedVerson = upgrade.getUpgradedVersion();
+                List<DbUpgrade> upgradeList = upgradedVersions.get(upgradedVerson);
+                if (upgradeList == null) {
+                    upgradeList = new ArrayList<DbUpgrade>();
+                }
+                upgradeList.add(upgrade);
+                upgradedVersions.put(upgradedVerson, upgradeList);
+            }
+
+            for (String upgradedVersion : upgradedVersions.keySet()) {
+                List<DbUpgrade> versionUpgrades = upgradedVersions.get(upgradedVersion);
+                VersionVO version = _dao.findByVersion(upgradedVersion, Step.Upgrade);
+                s_logger.debug("Upgrading to version " + upgradedVersion + "...");
+
+                Transaction txn = Transaction.open("Cleanup");
+                try {
+                    if (version != null) {
+                        for (DbUpgrade upgrade : versionUpgrades) {
+                            s_logger.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-"
+                                    + upgrade.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
+
+                            txn.start();
+
+                            Connection conn;
+                            try {
+                                conn = txn.getConnection();
+                            } catch (SQLException e) {
+                                s_logger.error("Unable to cleanup the database", e);
+                                throw new CloudRuntimeException("Unable to cleanup the database", e);
+                            }
+
+                            File[] scripts = upgrade.getCleanupScripts();
+                            if (scripts != null) {
+                                for (File script : scripts) {
+                                    runScript(conn, script);
+                                    s_logger.debug("Cleanup script " + script.getAbsolutePath() + " is executed successfully");
+                                }
+                            }
+                            txn.commit();
+                        }
+
+                        txn.start();
+                        version.setStep(Step.Complete);
+                        s_logger.debug("Upgrade completed for version " + upgradedVersion);
+                        version.setUpdated(new Date());
+                        _dao.update(version.getId(), version);
+                        txn.commit();
+                    }
+                } finally {
+                    txn.close();
+                }
+            }
+        }
+
+    }
+
+    @Override
+    public void check() {
+        GlobalLock lock = GlobalLock.getInternLock("DatabaseUpgrade");
+        try {
+            s_logger.info("Grabbing lock to check for database upgrade.");
+            if (!lock.lock(20 * 60)) {
+                throw new CloudRuntimeException("Unable to acquire lock to check for database integrity.");
+            }
+
+            try {
+                String dbVersion = _dao.getCurrentVersion();
+                String currentVersion = this.getClass().getPackage().getImplementationVersion();
+                if (currentVersion == null) {
+                    currentVersion = this.getClass().getSuperclass().getPackage().getImplementationVersion();
+                }
+
+                if ( currentVersion == null )
+                    return;
+
+                s_logger.info("DB version = " + dbVersion + " Code Version = " + currentVersion);
+
+                if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch(currentVersion)) > 0) {
+                    throw new CloudRuntimeException("Database version " + dbVersion + " is higher than management software version " + currentVersion);
+                }
+
+                if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch(currentVersion)) == 0) {
+                    s_logger.info("DB version and code version matches so no upgrade needed.");
+                    return;
+                }
+
+                upgrade(dbVersion, currentVersion);
+            } finally {
+                lock.unlock();
+            }
+        } finally {
+            lock.releaseRef();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/PremiumDatabaseUpgradeChecker.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/PremiumDatabaseUpgradeChecker.java b/engine/schema/src/com/cloud/upgrade/PremiumDatabaseUpgradeChecker.java
new file mode 100755
index 0000000..bad3253
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/PremiumDatabaseUpgradeChecker.java
@@ -0,0 +1,148 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade;
+
+import javax.ejb.Local;
+
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Component;
+
+import com.cloud.upgrade.dao.DbUpgrade;
+import com.cloud.upgrade.dao.Upgrade217to218;
+import com.cloud.upgrade.dao.Upgrade218to224DomainVlans;
+import com.cloud.upgrade.dao.Upgrade218to22Premium;
+import com.cloud.upgrade.dao.Upgrade2210to2211;
+import com.cloud.upgrade.dao.Upgrade2211to2212Premium;
+import com.cloud.upgrade.dao.Upgrade2212to2213;
+import com.cloud.upgrade.dao.Upgrade2213to2214;
+import com.cloud.upgrade.dao.Upgrade2214to30;
+import com.cloud.upgrade.dao.Upgrade221to222Premium;
+import com.cloud.upgrade.dao.Upgrade222to224Premium;
+import com.cloud.upgrade.dao.Upgrade224to225;
+import com.cloud.upgrade.dao.Upgrade225to226;
+import com.cloud.upgrade.dao.Upgrade227to228Premium;
+import com.cloud.upgrade.dao.Upgrade228to229;
+import com.cloud.upgrade.dao.Upgrade229to2210;
+import com.cloud.upgrade.dao.Upgrade301to302;
+import com.cloud.upgrade.dao.Upgrade302to40;
+import com.cloud.upgrade.dao.Upgrade30to301;
+import com.cloud.upgrade.dao.Upgrade40to41;
+import com.cloud.upgrade.dao.UpgradeSnapshot217to224;
+import com.cloud.upgrade.dao.UpgradeSnapshot223to224;
+import com.cloud.upgrade.dao.VersionDaoImpl;
+
+import com.cloud.utils.component.SystemIntegrityChecker;
+
+@Local(value = { SystemIntegrityChecker.class })
+public class PremiumDatabaseUpgradeChecker extends DatabaseUpgradeChecker {
+    public PremiumDatabaseUpgradeChecker() {
+        _upgradeMap.put("2.1.7", new DbUpgrade[] { new Upgrade217to218(), new Upgrade218to22Premium(),
+                new Upgrade221to222Premium(), new UpgradeSnapshot217to224(), new Upgrade222to224Premium(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.1.8", new DbUpgrade[] { new Upgrade218to22Premium(), new Upgrade221to222Premium(),
+                new UpgradeSnapshot217to224(), new Upgrade222to224Premium(), new Upgrade218to224DomainVlans(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.1.9", new DbUpgrade[] { new Upgrade218to22Premium(), new Upgrade221to222Premium(),
+                new UpgradeSnapshot217to224(), new Upgrade222to224Premium(), new Upgrade218to224DomainVlans(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.1", new DbUpgrade[] { new Upgrade221to222Premium(), new Upgrade222to224Premium(),
+                new UpgradeSnapshot223to224(), new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(),
+                new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.2", new DbUpgrade[] { new Upgrade222to224Premium(), new UpgradeSnapshot223to224(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.3", new DbUpgrade[] { new Upgrade222to224Premium(), new UpgradeSnapshot223to224(),
+                new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.4", new DbUpgrade[] { new Upgrade224to225(), new Upgrade225to226(), new Upgrade227to228Premium(),
+                new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.5", new DbUpgrade[] { new Upgrade225to226(), new Upgrade227to228Premium(), new Upgrade228to229(),
+                new Upgrade229to2210(), new Upgrade2210to2211(), new Upgrade2211to2212Premium(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.6", new DbUpgrade[] { new Upgrade227to228Premium(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.7", new DbUpgrade[] { new Upgrade227to228Premium(), new Upgrade228to229(), new Upgrade229to2210(),
+                new Upgrade2210to2211(), new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.8", new DbUpgrade[] { new Upgrade228to229(), new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212Premium(), new Upgrade2212to2213(), new Upgrade2213to2214(),
+                new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.9", new DbUpgrade[] { new Upgrade229to2210(), new Upgrade2210to2211(),
+                new Upgrade2211to2212Premium(), new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(),
+                new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.10", new DbUpgrade[] { new Upgrade2210to2211(), new Upgrade2211to2212Premium(),
+                new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.11", new DbUpgrade[] { new Upgrade2211to2212Premium(), new Upgrade2212to2213(),
+                new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.12", new DbUpgrade[] { new Upgrade2212to2213(), new Upgrade2213to2214(), new Upgrade2214to30(),
+                new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.13", new DbUpgrade[] { new Upgrade2213to2214(), new Upgrade2214to30(), new Upgrade30to301(),
+                new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("2.2.14", new DbUpgrade[] { new Upgrade2214to30(), new Upgrade30to301(), new Upgrade301to302(),
+                new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("3.0.0", new DbUpgrade[] { new Upgrade30to301(), new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("3.0.1", new DbUpgrade[] { new Upgrade301to302(), new Upgrade302to40(), new Upgrade40to41()  });
+
+        _upgradeMap.put("3.0.2", new DbUpgrade[] { new Upgrade302to40(), new Upgrade40to41() });
+
+        _upgradeMap.put("4.0.0", new DbUpgrade[] { new Upgrade40to41() });
+
+        _upgradeMap.put("4.0.1", new DbUpgrade[] { new Upgrade40to41() });
+
+        _upgradeMap.put("4.0.2", new DbUpgrade[] { new Upgrade40to41() });    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/dao/DbUpgrade.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/DbUpgrade.java b/engine/schema/src/com/cloud/upgrade/dao/DbUpgrade.java
new file mode 100644
index 0000000..2fefa4e
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/dao/DbUpgrade.java
@@ -0,0 +1,45 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade.dao;
+
+import java.io.File;
+import java.sql.Connection;
+
+public interface DbUpgrade {
+    String[] getUpgradableVersionRange();
+    
+    String getUpgradedVersion();
+    
+    boolean supportsRollingUpgrade();
+
+    /**
+     * @return the script to prepare the database schema for the 
+     * data migration step.
+     */
+    File[] getPrepareScripts();
+    
+    /**
+     * Performs the actual data migration.
+     */
+    void performDataMigration(Connection conn);
+    
+    /**
+     * 
+     * @return
+     */
+    File[] getCleanupScripts();
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/dao/DbUpgradeUtils.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/DbUpgradeUtils.java b/engine/schema/src/com/cloud/upgrade/dao/DbUpgradeUtils.java
new file mode 100644
index 0000000..c37be3e
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/dao/DbUpgradeUtils.java
@@ -0,0 +1,106 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class DbUpgradeUtils {
+    final static Logger s_logger = Logger.getLogger(DbUpgradeUtils.class);
+
+    public static void dropKeysIfExist(Connection conn, String tableName, List<String> keys, boolean isForeignKey) {
+        for (String key : keys) {
+            PreparedStatement pstmt = null;
+            try {
+                if (isForeignKey) {
+                    pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key);
+                } else {
+                    pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key);
+                }
+                pstmt.executeUpdate();
+                s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName);
+            } catch (SQLException e) {
+                // do nothing here
+                
+                continue;
+            } finally {
+                try {
+                    if (pstmt != null) {
+                        pstmt.close();
+                    }
+                } catch (SQLException e) {
+                }
+            }
+        }
+    }
+    
+    
+    public static void dropPrimaryKeyIfExists(Connection conn, String tableName) {
+        PreparedStatement pstmt = null;
+        try {
+            pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY ");
+            pstmt.executeUpdate();
+            s_logger.debug("Primary key is dropped successfully from the table " + tableName);
+        } catch (SQLException e) {
+            // do nothing here
+        } finally {
+            try {
+                if (pstmt != null) {
+                    pstmt.close();
+                }
+            } catch (SQLException e) {
+            }
+        }
+    }
+    
+    
+    public static void dropTableColumnsIfExist(Connection conn, String tableName, List<String> columns) {
+        PreparedStatement pstmt = null;
+        try {
+            for (String column : columns) {
+                try {
+                    pstmt = conn.prepareStatement("SELECT " + column + " FROM " + tableName);
+                    pstmt.executeQuery();
+                } catch (SQLException e) {
+                    // if there is an exception, it means that field doesn't exist, so do nothing here
+                    s_logger.trace("Field " + column + " doesn't exist in " + tableName);
+                    continue;
+                }
+
+                pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + column);
+                pstmt.executeUpdate();
+                s_logger.debug("Column " + column + " is dropped successfully from the table " + tableName);
+            }
+        } catch (SQLException e) {
+            s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e);
+            throw new CloudRuntimeException("Unable to drop columns due to ", e);
+        } finally {
+            try {
+                if (pstmt != null) {
+                    pstmt.close();
+                }
+            } catch (SQLException e) {
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/572e71e5/engine/schema/src/com/cloud/upgrade/dao/Upgrade217to218.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade217to218.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade217to218.java
new file mode 100644
index 0000000..2cb6718
--- /dev/null
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade217to218.java
@@ -0,0 +1,66 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.upgrade.dao;
+
+import java.io.File;
+import java.sql.Connection;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.script.Script;
+
+public class Upgrade217to218 implements DbUpgrade {
+
+    @Override
+    public File[] getPrepareScripts() {
+        String schemaFile = Script.findScript("", "db/schema-217to218.sql");
+        if (schemaFile == null) {
+            throw new CloudRuntimeException("Unable to find the upgrade script, schema-217to218.sql");
+        }
+        
+        String dataFile = Script.findScript("", "db/data-217to218.sql");
+        if (dataFile == null) {
+            throw new CloudRuntimeException("Unable to find the upgrade script, data-217to218.sql");
+        }
+        
+        return new File[] {new File(schemaFile), new File(dataFile)};
+    }
+        
+    @Override
+    public void performDataMigration(Connection conn) {
+        
+    }
+
+    @Override
+    public File[] getCleanupScripts() {
+       return null;
+    }
+
+    @Override
+    public String[] getUpgradableVersionRange() {
+        return new String[] { "2.1.7", "2.1.7" };
+    }
+
+    @Override
+    public String getUpgradedVersion() {
+        return "2.1.8";
+    }
+    
+    @Override
+    public boolean supportsRollingUpgrade() {
+        return false;
+    }
+}


Mime
View raw message