diff --git a/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java b/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java index fc7bd981c67..9bf7c70a34a 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade2213to2214.java @@ -30,109 +30,119 @@ import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; public class Upgrade2213to2214 implements DbUpgrade { - final static Logger s_logger = Logger.getLogger(Upgrade2213to2214.class); + final static Logger s_logger = Logger.getLogger(Upgrade2213to2214.class); - @Override - public String[] getUpgradableVersionRange() { - return new String[] { "2.2.13", "2.2.14"}; - } + @Override + public String[] getUpgradableVersionRange() { + return new String[] { "2.2.13", "2.2.14"}; + } - @Override - public String getUpgradedVersion() { - return "2.2.14"; - } + @Override + public String getUpgradedVersion() { + return "2.2.14"; + } - @Override - public boolean supportsRollingUpgrade() { - return true; - } + @Override + public boolean supportsRollingUpgrade() { + return true; + } - @Override - public File[] getPrepareScripts() { - String script = Script.findScript("", "db/schema-2213to2214.sql"); - if (script == null) { - throw new CloudRuntimeException("Unable to find db/schema-2213to2214.sql"); - } - - return new File[] { new File(script) }; - } + @Override + public File[] getPrepareScripts() { + String script = Script.findScript("", "db/schema-2213to2214.sql"); + if (script == null) { + throw new CloudRuntimeException("Unable to find db/schema-2213to2214.sql"); + } - @Override - public void performDataMigration(Connection conn) { - fixIndexes(conn); - } + return new File[] { new File(script) }; + } - @Override - public File[] getCleanupScripts() { - return null; - } - + @Override + public void performDataMigration(Connection conn) { + fixIndexes(conn); + } - private void fixIndexes(Connection conn) { - //Drop i_usage_event__created key (if exists) and re-add it again - List keys = new ArrayList(); - keys.add("i_usage_event__created"); - DbUpgradeUtils.dropKeysIfExist(conn, "usage_event", keys, false); - try { - PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`usage_event` ADD INDEX `i_usage_event__created`(`created`)"); - pstmt.executeUpdate(); - pstmt.close(); - } catch (SQLException e) { - throw new CloudRuntimeException("Unable to execute usage_event 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"); - DbUpgradeUtils.dropKeysIfExist(conn, "cloud_usage.usage_event", keys, false); - try { - PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud_usage`.`usage_event` ADD INDEX `i_usage_event__created`(`created`)"); - pstmt.executeUpdate(); - pstmt.close(); - } catch (SQLException e) { - throw new CloudRuntimeException("Unable to execute cloud_usage usage_event table update", e); - } - - //Drop netapp_volume primary key and add it again - DbUpgradeUtils.dropPrimaryKeyIfExists(conn, "cloud.netapp_volume"); - try { - PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`netapp_volume` add PRIMARY KEY (`id`)"); - pstmt.executeUpdate(); - pstmt.close(); - } catch (SQLException e) { - throw new CloudRuntimeException("Unable to update primary key for netapp_volume", 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 i_op_vm_ruleset_log__instance_id, u_op_vm_ruleset_log__instance_id key (if exists) and re-add u_op_vm_ruleset_log__instance_id again - keys = new ArrayList(); - keys.add("i_op_vm_ruleset_log__instance_id"); - DbUpgradeUtils.dropKeysIfExist(conn, "cloud.op_vm_ruleset_log", keys, false); - - keys = new ArrayList(); - keys.add("u_op_vm_ruleset_log__instance_id"); - DbUpgradeUtils.dropKeysIfExist(conn, "cloud.op_vm_ruleset_log", keys, false); - try { - PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`op_vm_ruleset_log` ADD CONSTRAINT `u_op_vm_ruleset_log__instance_id` UNIQUE (`instance_id`)"); - pstmt.executeUpdate(); - pstmt.close(); - } catch (SQLException e) { - throw new CloudRuntimeException("Unable to execute changes for op_vm_ruleset_log", e); - } - + @Override + public File[] getCleanupScripts() { + return null; + } + private void fixIndexes(Connection conn) { + //Drop i_usage_event__created key (if exists) and re-add it again + List keys = new ArrayList(); + keys.add("i_usage_event__created"); + DbUpgradeUtils.dropKeysIfExist(conn, "usage_event", keys, false); + try { + PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`usage_event` ADD INDEX `i_usage_event__created`(`created`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute usage_event 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"); + DbUpgradeUtils.dropKeysIfExist(conn, "cloud_usage.usage_event", keys, false); + try { + PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud_usage`.`usage_event` ADD INDEX `i_usage_event__created`(`created`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute cloud_usage usage_event table update", e); + } + + //Drop netapp_volume primary key and add it again + DbUpgradeUtils.dropPrimaryKeyIfExists(conn, "cloud.netapp_volume"); + try { + PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`netapp_volume` add PRIMARY KEY (`id`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to update primary key for netapp_volume", 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 i_op_vm_ruleset_log__instance_id, u_op_vm_ruleset_log__instance_id key (if exists) and re-add u_op_vm_ruleset_log__instance_id again + keys = new ArrayList(); + keys.add("i_op_vm_ruleset_log__instance_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "cloud.op_vm_ruleset_log", keys, false); + + keys = new ArrayList(); + keys.add("u_op_vm_ruleset_log__instance_id"); + DbUpgradeUtils.dropKeysIfExist(conn, "cloud.op_vm_ruleset_log", keys, false); + try { + PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`op_vm_ruleset_log` ADD CONSTRAINT `u_op_vm_ruleset_log__instance_id` UNIQUE (`instance_id`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to execute changes for op_vm_ruleset_log", e); + } + + + //Drop i_async__removed, i_async_job__removed (if exists) and add i_async_job__removed + keys = new ArrayList(); + keys.add("i_async__removed"); + keys.add("i_async_job__removed"); + DbUpgradeUtils.dropKeysIfExist(conn, "cloud.async_job", keys, false); + try { + PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`async_job` ADD INDEX `i_async_job__removed`(`removed`)"); + pstmt.executeUpdate(); + pstmt.close(); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to insert index for removed column in async_job", e); + } + + } }