diff --git a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java index c633e25ef26..933172e1189 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade222to224.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade222to224.java @@ -67,6 +67,8 @@ public class Upgrade222to224 implements DbUpgrade { dropIndexIfExists(conn); fixBasicZoneNicCount(conn); updateTotalCPUInOpHostCapacity(conn); + upgradeGuestOs(conn); + upgradeAccountVlanMap(conn); } @Override @@ -291,23 +293,23 @@ public class Upgrade222to224 implements DbUpgrade { throw new CloudRuntimeException("Unable to drop 'path' index for 'domain' table due to:", e); } } - + private void updateTotalCPUInOpHostCapacity(Connection conn) { PreparedStatement pstmt = null; ResultSet rs = null; PreparedStatement pstmtUpdate = null; try { // Load all Routing hosts - s_logger.debug("Updating total CPU capacity entries in op_host_capacity"); + s_logger.debug("Updating total CPU capacity entries in op_host_capacity"); pstmt = conn.prepareStatement("SELECT id, cpus, speed FROM host WHERE type = 'Routing'"); rs = pstmt.executeQuery(); while (rs.next()) { long hostId = rs.getLong(1); int cpus = rs.getInt(2); long speed = rs.getLong(3); - + long totalCapacity = cpus * speed; - + String updateSQL = "UPDATE op_host_capacity SET total_capacity = ? WHERE host_id = ? AND capacity_type = 1"; pstmtUpdate = conn.prepareStatement(updateSQL); pstmtUpdate.setLong(1, totalCapacity); @@ -338,5 +340,49 @@ public class Upgrade222to224 implements DbUpgrade { } } - } + } + + private void upgradeGuestOs(Connection conn) { + try { + PreparedStatement pstmt = conn.prepareStatement("SELECT * from guest_os WHERE id=138"); + ResultSet rs = pstmt.executeQuery(); + + if (!rs.next()) { + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`guest_os` (id, category_id, display_name) VALUES (138, 7, 'None')"); + pstmt.executeUpdate(); + s_logger.debug("Inserted NONE category to guest_os table"); + } + + rs.close(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unalbe to insert NONE guest category to guest_os table due to:", e); + } + } + + private void upgradeAccountVlanMap(Connection conn) { + try { + PreparedStatement pstmt = conn.prepareStatement("SELECT domain_id FROM account_vlan_map"); + try { + pstmt.executeQuery(); + } catch (SQLException e) { + s_logger.debug("Assuming that domain_id field doesn't exist in account_vlan_map table, no need to upgrade"); + return; + } + + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`account_vlan_map` DROP FOREIGN KEY `fk_account_vlan_map__domain_id`"); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`account_vlan_map` DROP COLUMN `domain_id`"); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("DELETE FROM `cloud`.`account_vlan_map` WHERE account_id IS NULL"); + pstmt.executeUpdate(); + + pstmt.close(); + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to delete domain_id field from account_vlan_map table due to:", e); + } + } } diff --git a/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java b/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java index 2950dc95c3a..19fbc6c5bf9 100644 --- a/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java +++ b/server/src/com/cloud/upgrade/dao/VersionDaoImpl.java @@ -104,21 +104,51 @@ public class VersionDaoImpl extends GenericDaoBase implements V } else { rs.close(); pstmt.close(); - s_logger.debug("No version table but has nics table, returning 2.2.1 or 2.2.2"); - - // Use is_static_nat field from firewall_rules table to indicate that the version is 2.2.2 - pstmt = conn.prepareStatement("SELECT is_static_nat FROM firewall_rules LIMIT 1"); - try { - pstmt.executeQuery(); - return "2.2.1"; - } catch (SQLException e) { - s_logger.debug("Assuming the exception means is_static_nat field doesn't exist in firewall_rules table; returning 2.2.2"); - return "2.2.2"; - } finally { - pstmt.close(); - } + s_logger.debug("No version table but has nics table, returning 2.2.1"); + return "2.2.1"; } } + + SearchCriteria sc = CurrentVersionSearch.create(); + + sc.setParameters("step", Step.Complete); + Filter filter = new Filter(VersionVO.class, "id", false, 0l, 1l); + List upgradedVersions = customSearch(sc, filter); + + if (upgradedVersions.isEmpty()) { + + // Check if there are records in Version table + filter = new Filter(VersionVO.class, "id", false, 0l, 1l); + sc = CurrentVersionSearch.create(); + List vers = customSearch(sc, filter); + if (!vers.isEmpty()) { + throw new CloudRuntimeException("Version table contains records for which upgrade wasn't completed"); + } + + // Use nics table information and is_static_nat field from firewall_rules table to determine version information + try { + s_logger.debug("Version table exists, but it's empty; have to confirm that version is 2.2.2"); + pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'"); + rs = pstmt.executeQuery(); + if (!rs.next()) { + throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, nics table doesn't exist"); + } else { + pstmt = conn.prepareStatement("SELECT is_static_nat from firewall_rules"); + pstmt.executeQuery(); + throw new CloudRuntimeException( + "Unable to determine the current version, version table exists and empty, nics table doesn't exist, is_static_nat field exists in firewall_rules table"); + } + } catch (SQLException e) { + s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2"); + return "2.2.2"; + } finally { + rs.close(); + pstmt.close(); + } + } else { + return upgradedVersions.get(0); + } + } catch (SQLException e) { throw new CloudRuntimeException("Unable to get the current version", e); } finally { @@ -128,12 +158,5 @@ public class VersionDaoImpl extends GenericDaoBase implements V } } - SearchCriteria sc = CurrentVersionSearch.create(); - - sc.setParameters("step", Step.Complete); - Filter filter = new Filter(VersionVO.class, "id", false, 0l, 1l); - - List vers = customSearch(sc, filter); - return vers.get(0); } } diff --git a/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java b/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.java new file mode 100644 index 00000000000..c9268b26d3b --- /dev/null +++ b/server/test/com/cloud/upgrade/Sanity222To224UpgradeTest.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 Sanity222To224UpgradeTest extends TestCase { + private static final Logger s_logger = Logger.getLogger(PortForwarding218To224UpgradeTest.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 test217to22Upgrade() 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); + + 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.2")) { + s_logger.error("Version returned is not 2.2.2 but " + version); + } else { + s_logger.debug("Sanity 2.2.2 to 2.2.4 test version is " + version); + } + + checker.upgrade("2.2.2", "2.2.4"); + + conn = Transaction.getStandaloneConnection(); + try { + s_logger.debug("Starting tesing upgrade from 2.2.2 to 2.2.4..."); + + // 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.4")) { + s_logger.error("ERROR: VERSION stored is not 2.2.4: " + rs.getString(1)); + } + rs.close(); + pstmt.close(); + + s_logger.debug("Sanity 2.2.2 to 2.2.4 DB upgrade test passed"); + + } finally { + conn.close(); + } + } + +} diff --git a/setup/db/db/schema-21to22-cleanup.sql b/setup/db/db/schema-21to22-cleanup.sql index 4f879f7b59c..c93c9049dab 100644 --- a/setup/db/db/schema-21to22-cleanup.sql +++ b/setup/db/db/schema-21to22-cleanup.sql @@ -121,3 +121,5 @@ ALTER TABLE `cloud`.`template_spool_ref` ADD CONSTRAINT `fk_template_spool_ref__ ALTER TABLE `cloud`.`volumes` MODIFY COLUMN `state` VARCHAR(32) NOT NULL; +ALTER TABLE op_dc_ip_address_alloc ADD CONSTRAINT `fk_op_dc_ip_address_alloc__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE; + diff --git a/setup/db/db/schema-222to224-cleanup.sql b/setup/db/db/schema-222to224-cleanup.sql index 67f58184d7c..f10a2f6ec9f 100644 --- a/setup/db/db/schema-222to224-cleanup.sql +++ b/setup/db/db/schema-222to224-cleanup.sql @@ -1,6 +1,3 @@ -ALTER TABLE `cloud`.`account_vlan_map` DROP FOREIGN KEY `fk_account_vlan_map__domain_id`; -ALTER TABLE `cloud`.`account_vlan_map` DROP COLUMN `domain_id`; -DELETE FROM `cloud`.`account_vlan_map` WHERE account_id IS NULL; ALTER TABLE `cloud`.`data_center` DROP COLUMN `enable`; ALTER TABLE `cloud`.`host_pod_ref` DROP COLUMN `enabled`; DELETE FROM `cloud`.`configuration` WHERE name in ('direct.attach.security.groups.enabled', 'direct.attach.untagged.vlan.enabled', 'hypervisor.type', 'management-server', 'max.volume.size.gb', 'multicast.throttling.rate', 'network.type', 'xen.preallocated.lun.size.range'); diff --git a/setup/db/db/schema-222to224.sql b/setup/db/db/schema-222to224.sql index ccde72bd023..757a750d469 100644 --- a/setup/db/db/schema-222to224.sql +++ b/setup/db/db/schema-222to224.sql @@ -45,7 +45,6 @@ ALTER TABLE `cloud`.`cluster` ADD COLUMN `removed` datetime COMMENT 'date remov ALTER TABLE `cloud`.`cluster` MODIFY `name` varchar(255) COMMENT 'name for the cluster'; ALTER TABLE `cloud`.`network_offerings` MODIFY `guest_type` char(32); -INSERT INTO `cloud`.`guest_os` (id, category_id, display_name) VALUES (138, 7, 'None'); UPDATE `cloud`.`network_offerings` SET `nw_rate`=0, `mc_rate`=0 WHERE system_only=1 and guest_type IS NULL; @@ -123,7 +122,6 @@ INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Account Defaults','DEFAULT','management-server','max.account.volumes','20','The default maximum number of volumes that can be created for an account'); ALTER TABLE `cloud`.`op_dc_ip_address_alloc` CHANGE COLUMN `instance_id` `nic_id` bigint unsigned DEFAULT NULL; -ALTER TABLE `cloud`.`op_dc_ip_address_alloc` ADD CONSTRAINT `fk_op_dc_ip_address_alloc__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`op_dc_link_local_ip_address_alloc` CHANGE COLUMN `instance_id` `nic_id` bigint unsigned DEFAULT NULL;