Search in sources :

Example 1 with DbUpgrade

use of com.cloud.upgrade.dao.DbUpgrade in project cloudstack by apache.

the class DatabaseUpgradeChecker method calculateUpgradePath.

// Default visibility to support unit testing ...
DbUpgrade[] calculateUpgradePath(final CloudStackVersion dbVersion, final CloudStackVersion currentVersion) {
    checkArgument(dbVersion != null);
    checkArgument(currentVersion != null);
    checkArgument(currentVersion.compareTo(dbVersion) > 0);
    final DbUpgrade[] upgrades = _upgradeMap.containsKey(dbVersion) ? _upgradeMap.get(dbVersion) : findMostRecentUpgradePath(dbVersion);
    // When there is no upgrade defined for the target version, we assume that there were no schema changes or
    // data migrations required.  Based on that assumption, we add a noop DbUpgrade to the end of the list ...
    final CloudStackVersion tailVersion = upgrades.length > 0 ? CloudStackVersion.parse(upgrades[upgrades.length - 1].getUpgradedVersion()) : dbVersion;
    if (currentVersion.compareTo(tailVersion) != 0) {
        return concat(upgrades, new NoopDbUpgrade(tailVersion, currentVersion));
    }
    return upgrades;
}
Also used : CloudStackVersion(org.apache.cloudstack.utils.CloudStackVersion) DbUpgrade(com.cloud.upgrade.dao.DbUpgrade)

Example 2 with DbUpgrade

use of com.cloud.upgrade.dao.DbUpgrade in project cloudstack by apache.

the class DatabaseUpgradeCheckerTest method testCalculateUpgradePath490to4910.

@Test
public void testCalculateUpgradePath490to4910() {
    final CloudStackVersion dbVersion = CloudStackVersion.parse("4.9.0");
    assertNotNull(dbVersion);
    final CloudStackVersion currentVersion = CloudStackVersion.parse("4.9.1.0");
    assertNotNull(currentVersion);
    final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
    final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
    assertNotNull(upgrades);
    assertTrue(upgrades.length >= 1);
    assertTrue(upgrades[0] instanceof Upgrade490to4910);
    assertTrue(Arrays.equals(new String[] { "4.9.0", currentVersion.toString() }, upgrades[0].getUpgradableVersionRange()));
    assertEquals(currentVersion.toString(), upgrades[0].getUpgradedVersion());
}
Also used : Upgrade490to4910(com.cloud.upgrade.dao.Upgrade490to4910) CloudStackVersion(org.apache.cloudstack.utils.CloudStackVersion) DbUpgrade(com.cloud.upgrade.dao.DbUpgrade) Test(org.junit.Test)

Example 3 with DbUpgrade

use of com.cloud.upgrade.dao.DbUpgrade in project cloudstack by apache.

the class DatabaseUpgradeCheckerTest method testFindUpgradePath452to490.

@Test
public void testFindUpgradePath452to490() {
    final CloudStackVersion dbVersion = CloudStackVersion.parse("4.5.2");
    assertNotNull(dbVersion);
    final CloudStackVersion currentVersion = CloudStackVersion.parse("4.9.0");
    assertNotNull(currentVersion);
    final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
    final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
    assertNotNull(upgrades);
    assertTrue(upgrades[0] instanceof Upgrade452to460);
    assertTrue(upgrades[1] instanceof Upgrade460to461);
    assertTrue(upgrades[2] instanceof Upgrade461to470);
    assertTrue(upgrades[3] instanceof Upgrade470to471);
    assertTrue(upgrades[4] instanceof Upgrade471to480);
    assertTrue(upgrades[5] instanceof Upgrade480to481);
    assertTrue(Arrays.equals(new String[] { "4.8.1", currentVersion.toString() }, upgrades[6].getUpgradableVersionRange()));
    assertEquals(currentVersion.toString(), upgrades[6].getUpgradedVersion());
}
Also used : Upgrade461to470(com.cloud.upgrade.dao.Upgrade461to470) Upgrade452to460(com.cloud.upgrade.dao.Upgrade452to460) Upgrade480to481(com.cloud.upgrade.dao.Upgrade480to481) CloudStackVersion(org.apache.cloudstack.utils.CloudStackVersion) Upgrade460to461(com.cloud.upgrade.dao.Upgrade460to461) Upgrade471to480(com.cloud.upgrade.dao.Upgrade471to480) Upgrade470to471(com.cloud.upgrade.dao.Upgrade470to471) DbUpgrade(com.cloud.upgrade.dao.DbUpgrade) Test(org.junit.Test)

Example 4 with DbUpgrade

use of com.cloud.upgrade.dao.DbUpgrade in project cloudstack by apache.

the class DatabaseUpgradeChecker method upgrade.

protected void upgrade(CloudStackVersion dbVersion, CloudStackVersion currentVersion) {
    s_logger.info("Database upgrade must be performed from " + dbVersion + " to " + currentVersion);
    final DbUpgrade[] upgrades = calculateUpgradePath(dbVersion, 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)) {
        String errorMessage = "Unable to run upgrade because the upgrade sequence does not support rolling update and there are other management server nodes running";
        s_logger.error(errorMessage);
        throw new CloudRuntimeException(errorMessage);
    }
    for (DbUpgrade upgrade : upgrades) {
        VersionVO version;
        s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion());
        TransactionLegacy txn = TransactionLegacy.open("Upgrade");
        txn.start();
        try {
            Connection conn;
            try {
                conn = txn.getConnection();
            } catch (SQLException e) {
                String errorMessage = "Unable to upgrade the database";
                s_logger.error(errorMessage, e);
                throw new CloudRuntimeException(errorMessage, e);
            }
            File[] scripts = upgrade.getPrepareScripts();
            if (scripts != null) {
                for (File script : scripts) {
                    runScript(conn, script);
                }
            }
            upgrade.performDataMigration(conn);
            version = new VersionVO(upgrade.getUpgradedVersion());
            version = _dao.persist(version);
            txn.commit();
        } catch (CloudRuntimeException e) {
            String errorMessage = "Unable to upgrade the database";
            s_logger.error(errorMessage, e);
            throw new CloudRuntimeException(errorMessage, e);
        } finally {
            txn.close();
        }
        // Run the corresponding '-cleanup.sql' script
        txn = TransactionLegacy.open("Cleanup");
        try {
            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);
            version.setUpdated(new Date());
            _dao.update(version.getId(), version);
            txn.commit();
            s_logger.debug("Upgrade completed for version " + version.getVersion());
        } finally {
            txn.close();
        }
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) VersionVO(com.cloud.upgrade.dao.VersionVO) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(java.sql.Connection) File(java.io.File) Date(java.util.Date) DbUpgrade(com.cloud.upgrade.dao.DbUpgrade)

Example 5 with DbUpgrade

use of com.cloud.upgrade.dao.DbUpgrade in project cloudstack by apache.

the class DatabaseUpgradeCheckerTest method testCalculateUpgradePath480to481.

@Test
public void testCalculateUpgradePath480to481() {
    final CloudStackVersion dbVersion = CloudStackVersion.parse("4.8.0");
    assertNotNull(dbVersion);
    final CloudStackVersion currentVersion = CloudStackVersion.parse("4.8.1");
    assertNotNull(currentVersion);
    final DatabaseUpgradeChecker checker = new DatabaseUpgradeChecker();
    final DbUpgrade[] upgrades = checker.calculateUpgradePath(dbVersion, currentVersion);
    assertNotNull(upgrades);
    assertTrue(upgrades.length >= 1);
    assertTrue(upgrades[0] instanceof Upgrade480to481);
}
Also used : Upgrade480to481(com.cloud.upgrade.dao.Upgrade480to481) CloudStackVersion(org.apache.cloudstack.utils.CloudStackVersion) DbUpgrade(com.cloud.upgrade.dao.DbUpgrade) Test(org.junit.Test)

Aggregations

DbUpgrade (com.cloud.upgrade.dao.DbUpgrade)6 CloudStackVersion (org.apache.cloudstack.utils.CloudStackVersion)5 Test (org.junit.Test)4 Upgrade480to481 (com.cloud.upgrade.dao.Upgrade480to481)3 Upgrade470to471 (com.cloud.upgrade.dao.Upgrade470to471)2 Upgrade471to480 (com.cloud.upgrade.dao.Upgrade471to480)2 Upgrade452to460 (com.cloud.upgrade.dao.Upgrade452to460)1 Upgrade460to461 (com.cloud.upgrade.dao.Upgrade460to461)1 Upgrade461to470 (com.cloud.upgrade.dao.Upgrade461to470)1 Upgrade490to4910 (com.cloud.upgrade.dao.Upgrade490to4910)1 VersionVO (com.cloud.upgrade.dao.VersionVO)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 File (java.io.File)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1