From fb94a67cca9275e01e2dadf6f02d71a00fa46b18 Mon Sep 17 00:00:00 2001 From: Abhinandan Prateek Date: Wed, 12 Aug 2015 15:52:57 +0530 Subject: [PATCH] quota: to maintain idempotency the alter table statement is moved to java upgrade code. In case the column already exists the code will ignore the error. --- .../cloud/upgrade/dao/Upgrade451to452.java | 23 ++++++++++++++++--- setup/db/db/schema-451to452.sql | 2 -- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade451to452.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade451to452.java index 870e534fb7f..16b8a0dd799 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade451to452.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade451to452.java @@ -19,17 +19,20 @@ package com.cloud.upgrade.dao; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; + import org.apache.log4j.Logger; import java.io.File; import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; public class Upgrade451to452 implements DbUpgrade { final static Logger s_logger = Logger.getLogger(Upgrade451to452.class); @Override public String[] getUpgradableVersionRange() { - return new String[] {"4.5.1", "4.5.2"}; + return new String[] { "4.5.1", "4.5.2" }; } @Override @@ -48,11 +51,24 @@ public class Upgrade451to452 implements DbUpgrade { if (script == null) { throw new CloudRuntimeException("Unable to find db/schema-451to452.sql"); } - return new File[] {new File(script)}; + return new File[] { new File(script) }; } @Override public void performDataMigration(Connection conn) { + PreparedStatement pstmt = null; + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `quota_calculated` tinyint(1) DEFAULT 0 NOT NULL COMMENT 'quota calculation status'"); + pstmt.executeUpdate(); + pstmt.close(); + s_logger.debug("Altered usage table and added column quota_calculated"); + } catch (SQLException e) { + if (e.getMessage().contains("quota_calculated")) { + s_logger.debug("Usage table already has a column called quota_calculated"); + } else { + throw new CloudRuntimeException("Unable to create column quota_calculated", e); + } + } } @Override @@ -62,6 +78,7 @@ public class Upgrade451to452 implements DbUpgrade { throw new CloudRuntimeException("Unable to find db/schema-451to452-cleanup.sql"); } - return new File[] {new File(script)}; + return new File[] { new File(script) }; } + } diff --git a/setup/db/db/schema-451to452.sql b/setup/db/db/schema-451to452.sql index c0c3e928ee3..90e55271f23 100644 --- a/setup/db/db/schema-451to452.sql +++ b/setup/db/db/schema-451to452.sql @@ -38,8 +38,6 @@ CREATE TABLE `cloud`.`saml_token` ( -- Quota -ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `quota_calculated` tinyint(1) DEFAULT 0 COMMENT "quota calculation status"; - CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_account` ( `account_id` int(11) NOT NULL, `quota_balance` decimal(15,2) NULL,