diff --git a/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java b/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java new file mode 100644 index 00000000000..574bc79fba9 --- /dev/null +++ b/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java @@ -0,0 +1,59 @@ +package com.cloud.upgrade.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.utils.exception.CloudRuntimeException; + +public class DbUpgradeUtils { + final static Logger s_logger = Logger.getLogger(DbUpgradeUtils.class); + + public static void dropKeysIfExist(Connection conn, String tableName, List keys, boolean isForeignKey) { + for (String key : keys) { + try { + PreparedStatement pstmt = null; + if (isForeignKey) { + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key); + } else { + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key); + } + pstmt.executeUpdate(); + s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName); + pstmt.close(); + } catch (SQLException e) { + // do nothing here + continue; + } + } + } + + + public static void dropTableColumnsIfExist(Connection conn, String tableName, List columns) { + PreparedStatement pstmt = null; + try { + for (String column : columns) { + try { + pstmt = conn.prepareStatement("SELECT " + column + " FROM " + tableName); + pstmt.executeQuery(); + + } catch (SQLException e) { + // if there is an exception, it means that field doesn't exist, so do nothing here + s_logger.trace("Field " + column + " doesn't exist in " + tableName); + continue; + } + + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + column); + pstmt.executeUpdate(); + s_logger.debug("Column " + column + " is dropped successfully from the table " + tableName); + pstmt.close(); + } + } catch (SQLException e) { + s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e); + throw new CloudRuntimeException("Unable to drop columns due to ", e); + } + } +} diff --git a/server/src/com/cloud/upgrade/dao/Upgrade224to225.java b/server/src/com/cloud/upgrade/dao/Upgrade224to225.java index 69b03234615..bff1b461e8c 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade224to225.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade224to225.java @@ -209,32 +209,7 @@ public class Upgrade224to225 implements DbUpgrade { s_logger.debug("Dropping columns that don't exist in 2.2.5 version of the DB..."); for (String tableName : tablesToModify.keySet()) { - dropTableColumnsIfExist(conn, tableName, tablesToModify.get(tableName)); - } - } - - private void dropTableColumnsIfExist(Connection conn, String tableName, List columns) { - PreparedStatement pstmt = null; - try { - for (String column : columns) { - try { - pstmt = conn.prepareStatement("SELECT " + column + " FROM " + tableName); - pstmt.executeQuery(); - - } catch (SQLException e) { - // if there is an exception, it means that field doesn't exist, so do nothing here - s_logger.trace("Field " + column + " doesn't exist in " + tableName); - continue; - } - - pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + column); - pstmt.executeUpdate(); - s_logger.debug("Column " + column + " is dropped successfully from the table " + tableName); - pstmt.close(); - } - } catch (SQLException e) { - s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e); - throw new CloudRuntimeException("Unable to drop columns due to ", e); + DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, tablesToModify.get(tableName)); } } @@ -303,31 +278,12 @@ public class Upgrade224to225 implements DbUpgrade { // drop all foreign keys first s_logger.debug("Dropping keys that don't exist in 2.2.5 version of the DB..."); for (String tableName : foreignKeys.keySet()) { - dropKeysIfExist(conn, tableName, foreignKeys.get(tableName), true); + DbUpgradeUtils.dropKeysIfExist(conn, tableName, foreignKeys.get(tableName), true); } // drop indexes now for (String tableName : indexes.keySet()) { - dropKeysIfExist(conn, tableName, indexes.get(tableName), false); - } - } - - private void dropKeysIfExist(Connection conn, String tableName, List keys, boolean isForeignKey) { - for (String key : keys) { - try { - PreparedStatement pstmt = null; - if (isForeignKey) { - pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key); - } else { - pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key); - } - pstmt.executeUpdate(); - s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName); - pstmt.close(); - } catch (SQLException e) { - // do nothing here - continue; - } + DbUpgradeUtils.dropKeysIfExist(conn, tableName, indexes.get(tableName), false); } } diff --git a/server/src/com/cloud/upgrade/dao/Upgrade225to226.java b/server/src/com/cloud/upgrade/dao/Upgrade225to226.java new file mode 100644 index 00000000000..a9e7382adf9 --- /dev/null +++ b/server/src/com/cloud/upgrade/dao/Upgrade225to226.java @@ -0,0 +1,109 @@ +/** + * 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.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.script.Script; + +public class Upgrade225to226 implements DbUpgrade { + final static Logger s_logger = Logger.getLogger(Upgrade225to226.class); + + @Override + public File[] getPrepareScripts() { + String file = Script.findScript("", "db/schema-225to226.sql"); + if (file == null) { + throw new CloudRuntimeException("Unable to find the upgrade script, schema-225to226.sql"); + } + + return new File[] { new File(file) }; + } + + @Override + public void performDataMigration(Connection conn) { + dropKeysIfExist(conn); + dropTableColumnsIfExist(conn); + } + + @Override + public File[] getCleanupScripts() { + return null; + } + + @Override + public String[] getUpgradableVersionRange() { + return new String[] { "2.2.5", "2.2.5" }; + } + + @Override + public String getUpgradedVersion() { + return "2.2.6"; + } + + @Override + public boolean supportsRollingUpgrade() { + return false; + } + + private void dropTableColumnsIfExist(Connection conn) { + HashMap> tablesToModify = new HashMap>(); + + // domain router table + List columns = new ArrayList(); + columns.add("account_id"); + columns.add("domain_id"); + tablesToModify.put("domain_router", columns); + + s_logger.debug("Dropping columns that don't exist in 2.2.6 version of the DB..."); + for (String tableName : tablesToModify.keySet()) { + DbUpgradeUtils.dropTableColumnsIfExist(conn, tableName, tablesToModify.get(tableName)); + } + } + + private void dropKeysIfExist(Connection conn) { + HashMap> foreignKeys = new HashMap>(); + HashMap> indexes = new HashMap>(); + + // domain router table + List keys = new ArrayList(); + keys.add("fk_domain_router__account_id"); + foreignKeys.put("domain_router", keys); + + keys = new ArrayList(); + keys.add("i_domain_router__account_id"); + indexes.put("domain_router", keys); + + // drop all foreign keys first + s_logger.debug("Dropping keys that don't exist in 2.2.6 version of the DB..."); + for (String tableName : foreignKeys.keySet()) { + DbUpgradeUtils.dropKeysIfExist(conn, tableName, foreignKeys.get(tableName), true); + } + + // drop indexes now + for (String tableName : indexes.keySet()) { + DbUpgradeUtils.dropKeysIfExist(conn, tableName, indexes.get(tableName), false); + } + } +} diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 6babffaaac6..5ea17ed5f32 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -1198,7 +1198,7 @@ CREATE TABLE `cloud`.`storage_pool` ( `cluster_id` bigint unsigned COMMENT 'foreign key to cluster', `available_bytes` bigint unsigned, `capacity_bytes` bigint unsigned, - `host_address` char(40) NOT NULL COMMENT 'FQDN or IP of storage server', + `host_address` varchar(255) NOT NULL COMMENT 'FQDN or IP of storage server', `path` varchar(255) NOT NULL COMMENT 'Filesystem path that is shared', `created` datetime COMMENT 'date the pool created', `removed` datetime COMMENT 'date removed if not null', diff --git a/setup/db/db/schema-21to22-cleanup.sql b/setup/db/db/schema-21to22-cleanup.sql index 2d7d7a7f623..a0a2788f75f 100644 --- a/setup/db/db/schema-21to22-cleanup.sql +++ b/setup/db/db/schema-21to22-cleanup.sql @@ -26,9 +26,6 @@ ALTER TABLE `cloud`.`user_vm` DROP COLUMN `service_offering_id`; ALTER TABLE `cloud`.`user_vm` DROP COLUMN `account_id`; ALTER TABLE `cloud`.`user_vm` DROP COLUMN `domain_id`; -ALTER TABLE `cloud`.`domain_router` DROP FOREIGN KEY `fk_domain_router__account_id`; -ALTER TABLE `cloud`.`domain_router` DROP INDEX `i_domain_router__account_id`; - #ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `guid`; #ALTER TABLE `cloud`.`vlan` ADD CONSTRAINT `fk_vlan__network_id` FOREIGN KEY `fk_vlan__network_id`(`network_id`) REFERENCES `networks`(`id`); diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index d2bfe99a019..697461c07bb 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -953,7 +953,7 @@ INSERT INTO `cloud`.`vm_template` (id, unique_name, name, public, created, type, INSERT INTO `cloud`.`vm_template` (id, unique_name, name, public, created, type, hvm, bits, account_id, url, checksum, enable_password, display_text, format, guest_os_id, featured, cross_zones, hypervisor_type, extractable) VALUES (7, 'centos53-x64', 'CentOS 5.3(64-bit) no GUI (vSphere)', 1, now(), 'BUILTIN', 0, 64, 1, 'http://download.cloud.com/releases/2.2.0/CentOS5.3-x86_64.ova', 'f6f881b7f2292948d8494db837fe0f47', 0, 'CentOS 5.3(64-bit) no GUI (vSphere)', 'OVA', 12, 1, 1, 'VMware', 1); UPDATE vm_instance SET guest_os_id=15 where vm_template_id=1; -UPDATE vm_instance SET vm_template_id=(SELECT id FROM vm_template WHERE unique_name='systemvm-xenserver-2.2.4' AND removed IS NULL) where vm_template_id=1; +UPDATE vm_instance SET vm_template_id=(SELECT id FROM vm_template WHERE name='systemvm-xenserver-2.2.4' AND removed IS NULL) where vm_template_id=1; ALTER TABLE `cloud`.`instance_group` ADD CONSTRAINT `fk_instance_group__account_id` FOREIGN KEY `fk_instance_group__account_id` (`account_id`) REFERENCES `account` (`id`); diff --git a/setup/db/db/schema-225to226.sql b/setup/db/db/schema-225to226.sql new file mode 100644 index 00000000000..0a2953f4600 --- /dev/null +++ b/setup/db/db/schema-225to226.sql @@ -0,0 +1,7 @@ +--; +-- Schema upgrade from 2.2.5 to 2.2.6; +--; + +ALTER TABLE `cloud`.`storage_pool` MODIFY `host_address` varchar(255) NOT NULL; + +