diff --git a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java index ba185653079..8c719a31c62 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -34,6 +34,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.cloud.upgrade.dao.DbUpgrade; +import com.cloud.upgrade.dao.DbUpgradeSystemVmTemplate; import com.cloud.upgrade.dao.Upgrade217to218; import com.cloud.upgrade.dao.Upgrade218to22; import com.cloud.upgrade.dao.Upgrade218to224DomainVlans; @@ -235,11 +236,42 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { } + private void updateSystemVmTemplates(DbUpgrade[] upgrades) { + for (int i = upgrades.length - 1; i >= 0; i--) { + DbUpgrade upgrade = upgrades[i]; + if (upgrade instanceof DbUpgradeSystemVmTemplate) { + 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); + } + ((DbUpgradeSystemVmTemplate)upgrade).updateSystemVmTemplates(conn); + txn.commit(); + break; + } catch (CloudRuntimeException e) { + String errorMessage = "Unable to upgrade the database"; + s_logger.error(errorMessage, e); + throw new CloudRuntimeException(errorMessage, e); + } finally { + txn.close(); + } + } + } + } + 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); + updateSystemVmTemplates(upgrades); + for (DbUpgrade upgrade : upgrades) { VersionVO version; s_logger.debug("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java new file mode 100644 index 00000000000..4211898adc7 --- /dev/null +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeSystemVmTemplate.java @@ -0,0 +1,25 @@ +// 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; + +public interface DbUpgradeSystemVmTemplate { + + void updateSystemVmTemplates(Connection conn); +} diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41400to41500.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41400to41500.java index e3fe6022331..0ee9babc46d 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41400to41500.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41400to41500.java @@ -35,7 +35,7 @@ import org.apache.log4j.Logger; import com.cloud.hypervisor.Hypervisor; import com.cloud.utils.exception.CloudRuntimeException; -public class Upgrade41400to41500 implements DbUpgrade { +public class Upgrade41400to41500 implements DbUpgrade, DbUpgradeSystemVmTemplate { final static Logger LOG = Logger.getLogger(Upgrade41400to41500.class); @@ -67,12 +67,12 @@ public class Upgrade41400to41500 implements DbUpgrade { @Override public void performDataMigration(Connection conn) { - updateSystemVmTemplates(conn); addRolePermissionsForNewReadOnlyAndSupportRoles(conn); } + @Override @SuppressWarnings("serial") - private void updateSystemVmTemplates(final Connection conn) { + public void updateSystemVmTemplates(final Connection conn) { LOG.debug("Updating System Vm template IDs"); final Set hypervisorsListInUse = new HashSet(); try (PreparedStatement pstmt = conn.prepareStatement("select distinct(hypervisor_type) from `cloud`.`cluster` where removed is null"); ResultSet rs = pstmt.executeQuery()) {