From 58865c60c2e48fae3fd50a418ba8c603de35721e Mon Sep 17 00:00:00 2001 From: frank Date: Thu, 2 Feb 2012 16:37:55 -0800 Subject: [PATCH] add upgrade script for: add missing 's' to fk_ssh_keypair__account_id and fk_ssh_keypair__domain_id making the name match constraint name reviewed-by: alena --- .../com/cloud/upgrade/dao/DbUpgradeUtils.java | 23 +++++++++++ .../cloud/upgrade/dao/Upgrade2213to2214.java | 38 ++++++++++++------- 2 files changed, 48 insertions(+), 13 deletions(-) mode change 100644 => 100755 server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java mode change 100644 => 100755 server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java diff --git a/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java b/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java old mode 100644 new mode 100755 index e355f4ef696..499a9859eba --- a/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java +++ b/server/src/com/cloud/upgrade/dao/DbUpgradeUtils.java @@ -12,6 +12,29 @@ import com.cloud.utils.exception.CloudRuntimeException; public class DbUpgradeUtils { final static Logger s_logger = Logger.getLogger(DbUpgradeUtils.class); + public static boolean dropKeysIfExistAndReturnValue(Connection conn, String tableName, String key, boolean isForeignKey) { + PreparedStatement pstmt = null; + try { + 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); + } catch (SQLException e) { + return true; + } finally { + try { + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException e) { + } + } + return false; + } + public static void dropKeysIfExist(Connection conn, String tableName, List keys, boolean isForeignKey) { for (String key : keys) { PreparedStatement pstmt = null; diff --git a/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java b/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java old mode 100644 new mode 100755 index 254b9f4d0e4..2060c902e5d --- a/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java @@ -80,7 +80,31 @@ public class Upgrade2213to2214 implements DbUpgrade { } catch (SQLException e) { throw new CloudRuntimeException("Unable to execute usage_event table update", e); } - + + if (DbUpgradeUtils.dropKeysIfExistAndReturnValue(conn, "ssh_keypairs", "fk_ssh_keypair__account_id", false)) { + PreparedStatement pstmt; + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`ssh_keypairs` ADD KEY `fk_ssh_keypairs__account_id`(`account_id`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute ssh_keypairs table update", e); + } + + } + + if (DbUpgradeUtils.dropKeysIfExistAndReturnValue(conn, "ssh_keypairs", "fk_ssh_keypair__domain_id", false)) { + PreparedStatement pstmt; + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`ssh_keypairs` ADD KEY `fk_ssh_keypairs__domain_id`(`domain_id`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute ssh_keypairs table update", e); + } + + } + //In cloud_usage DB, drop i_usage_event__created key (if exists) and re-add it again keys = new ArrayList(); keys.add("i_usage_event__created"); @@ -93,18 +117,6 @@ public class Upgrade2213to2214 implements DbUpgrade { throw new CloudRuntimeException("Unable to execute cloud_usage usage_event table update", e); } - //Drop i_snapshots__removed key (if exists) and re-add it again - keys = new ArrayList(); - keys.add("i_snapshots__removed"); - DbUpgradeUtils.dropKeysIfExist(conn, "cloud.snapshots", keys, false); - try { - PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`snapshots` ADD INDEX `i_snapshots__removed`(`removed`)"); - pstmt.executeUpdate(); - pstmt.close(); - } catch (SQLException e) { - throw new CloudRuntimeException("Unable to insert index for removed column in snapshots", e); - } - //Drop netapp_volume primary key and add it again DbUpgradeUtils.dropPrimaryKeyIfExists(conn, "cloud.netapp_volume"); try {