diff --git a/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java b/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java index 71549e51d41..62004b42e4b 100644 --- a/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -41,6 +41,7 @@ import com.cloud.upgrade.dao.Upgrade218to22; import com.cloud.upgrade.dao.Upgrade218to224DomainVlans; import com.cloud.upgrade.dao.Upgrade221to222; import com.cloud.upgrade.dao.Upgrade222to224; +import com.cloud.upgrade.dao.Upgrade224to225; import com.cloud.upgrade.dao.UpgradeSnapshot217to224; import com.cloud.upgrade.dao.UpgradeSnapshot223to224; import com.cloud.upgrade.dao.VersionDao; @@ -69,6 +70,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { _upgradeMap.put("2.2.1", new DbUpgrade[] { new Upgrade221to222(), new UpgradeSnapshot223to224(), new Upgrade222to224()}); _upgradeMap.put("2.2.2", new DbUpgrade[] { new Upgrade222to224(), new UpgradeSnapshot223to224() }); _upgradeMap.put("2.2.3", new DbUpgrade[] { new Upgrade222to224(), new UpgradeSnapshot223to224() }); + _upgradeMap.put("2.2.4", new DbUpgrade[] { new Upgrade224to225() }); } protected void runScript(File file) { @@ -100,12 +102,14 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { 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) { @@ -121,7 +125,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { } for (DbUpgrade upgrade : upgrades) { - s_logger.info("Running upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1] + 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(); diff --git a/server/src/com/cloud/upgrade/dao/Upgrade224to225.java b/server/src/com/cloud/upgrade/dao/Upgrade224to225.java new file mode 100644 index 00000000000..2a9e32f8dc6 --- /dev/null +++ b/server/src/com/cloud/upgrade/dao/Upgrade224to225.java @@ -0,0 +1,114 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +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 java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.script.Script; + +public class Upgrade224to225 implements DbUpgrade { + final static Logger s_logger = Logger.getLogger(Upgrade224to225.class); + + @Override + public File[] getPrepareScripts() { + String file = Script.findScript("", "db/schema-224to225.sql"); + if (file == null) { + throw new CloudRuntimeException("Unable to find the upgrade script, schema-224to225.sql"); + } + + return new File[] {new File(file)}; + } + + @Override + public void performDataMigration(Connection conn) { + //create security groups for existing accounts if not present + createSecurityGroups(conn); + } + + @Override + public File[] getCleanupScripts() { + return null; + } + + @Override + public String[] getUpgradableVersionRange() { + return new String[] { "2.2.4", "2.2.4"}; + } + + @Override + public String getUpgradedVersion() { + return "2.2.5"; + } + + @Override + public boolean supportsRollingUpgrade() { + return false; + } + + private void createSecurityGroups(Connection conn) { + s_logger.debug("Creating missing default security group as a part of 224-225 upgrade"); + try { + List accounts = new ArrayList(); + PreparedStatement pstmt = conn.prepareStatement("SELECT id FROM account WHERE removed IS NULL and id != 1"); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) { + accounts.add(rs.getLong(1)); + } + + for (Long accountId : accounts) { + //get default security group + pstmt = conn.prepareStatement("SELECT * FROM security_group WHERE name='default' and account_id=?"); + pstmt.setLong(1, accountId); + rs = pstmt.executeQuery(); + if (!rs.next()) { + s_logger.debug("Default security group is missing for account id=" + accountId + " so adding it"); + + //get accountName/domainId information + + pstmt = conn.prepareStatement("SELECT account_name, domain_id FROM account WHERE id=?"); + pstmt.setLong(1, accountId); + ResultSet rs1 = pstmt.executeQuery(); + if (!rs1.next()) { + throw new CloudRuntimeException("Unable to create default security group for account id=" + accountId + ": unable to get accountName/domainId info"); + } + String accountName = rs1.getString(1); + Long domainId = rs1.getLong(2); + + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`security_group` (name, description, account_name, account_id, domain_id) VALUES ('default', 'Default Security Group', ?, ?, ?)"); + pstmt.setString(1, accountName); + pstmt.setLong(2, accountId); + pstmt.setLong(3, domainId); + pstmt.executeUpdate(); + } + } + + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to create default security groups for existing accounts due to", e); + } + } +} diff --git a/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java b/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java index c9268b26d3b..8600a7f1b00 100644 --- a/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java +++ b/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java @@ -34,7 +34,7 @@ import com.cloud.utils.db.DbTestUtils; import com.cloud.utils.db.Transaction; public class Sanity222To224UpgradeTest extends TestCase { - private static final Logger s_logger = Logger.getLogger(PortForwarding218To224UpgradeTest.class); + private static final Logger s_logger = Logger.getLogger(Sanity222To224UpgradeTest.class); @Override @Before @@ -47,7 +47,7 @@ public class Sanity222To224UpgradeTest extends TestCase { public void tearDown() throws Exception { } - public void test217to22Upgrade() throws SQLException { + public void test222to224Upgrade() throws SQLException { s_logger.debug("Finding sample data from 2.2.2"); DbTestUtils.executeScript("PreviousDatabaseSchema/2.2.2/2.2.2_cloud_db_sample.sql", false, true); diff --git a/server/test/com/cloud/upgrade/Sanity224To225UpgradeTest.java b/server/test/com/cloud/upgrade/Sanity224To225UpgradeTest.java new file mode 100644 index 00000000000..1977929620e --- /dev/null +++ b/server/test/com/cloud/upgrade/Sanity224To225UpgradeTest.java @@ -0,0 +1,94 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.upgrade; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; + +import com.cloud.upgrade.dao.VersionDaoImpl; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.db.DbTestUtils; +import com.cloud.utils.db.Transaction; + +public class Sanity224To225UpgradeTest extends TestCase { + private static final Logger s_logger = Logger.getLogger(Sanity224To225UpgradeTest.class); + + @Override + @Before + public void setUp() throws Exception { + DbTestUtils.executeScript("PreviousDatabaseSchema/clean-db.sql", false, true); + } + + @Override + @After + public void tearDown() throws Exception { + } + + public void test224to225Upgrade() throws SQLException { + s_logger.debug("Finding sample data from 2.2.4"); + DbTestUtils.executeScript("PreviousDatabaseSchema/2.2.4/2.2.4_cloud_db_sample.sql", false, true); + + Connection conn; + PreparedStatement pstmt; + ResultSet rs; + + VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class); + DatabaseUpgradeChecker checker = ComponentLocator.inject(DatabaseUpgradeChecker.class); + + String version = dao.getCurrentVersion(); + + if (!version.equals("2.2.4")) { + s_logger.error("Version returned is not 2.2.4 but " + version); + } else { + s_logger.debug("Sanity 2.2.4 to 2.2.5 test version is " + version); + } + + checker.upgrade("2.2.4", "2.2.5"); + + conn = Transaction.getStandaloneConnection(); + try { + s_logger.debug("Starting tesing upgrade from 2.2.4 to 2.2.5..."); + + // Version check + pstmt = conn.prepareStatement("SELECT version FROM version"); + rs = pstmt.executeQuery(); + + if (!rs.next()) { + s_logger.error("ERROR: No version selected"); + } else if (!rs.getString(1).equals("2.2.5")) { + s_logger.error("ERROR: VERSION stored is not 2.2.5: " + rs.getString(1)); + } + rs.close(); + pstmt.close(); + + s_logger.debug("Sanity 2.2.4 to 2.2.5 DB upgrade test passed"); + + } finally { + conn.close(); + } + } + +} diff --git a/setup/db/db/schema-224to225.sql b/setup/db/db/schema-224to225.sql new file mode 100644 index 00000000000..8e9af53eb3f --- /dev/null +++ b/setup/db/db/schema-224to225.sql @@ -0,0 +1,5 @@ +--; +-- Schema upgrade from 2.2.4 to 2.2.5; +--; + +ALTER TABLE `cloud`.`security_group` add UNIQUE KEY (`name`, `account_id`);