}
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 (!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();
}
}
}
}