Repository: cloudstack
Updated Branches:
  refs/heads/useraccount-refactoring [created] eb8fba3e0


Upgrade450to460: Add user_id column in vm_instance

Signed-off-by: Rohit Yadav <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/cb018b84
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/cb018b84
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/cb018b84

Branch: refs/heads/useraccount-refactoring
Commit: cb018b840b08a7c43586895c02d74f1209403d8a
Parents: 92d4a41
Author: Rohit Yadav <[email protected]>
Authored: Fri Nov 14 13:57:04 2014 +0530
Committer: Rohit Yadav <[email protected]>
Committed: Fri Nov 14 13:57:04 2014 +0530

----------------------------------------------------------------------
 .../com/cloud/upgrade/dao/Upgrade450to460.java  | 39 ++++++++++++++++++++
 setup/db/db/schema-450to460.sql                 |  4 +-
 2 files changed, 42 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/cb018b84/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java 
b/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java
index 990371c..a0fdb54 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade450to460.java
@@ -19,6 +19,9 @@ package com.cloud.upgrade.dao;
 
 import java.io.File;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
 import org.apache.log4j.Logger;
 
@@ -55,6 +58,42 @@ public class Upgrade450to460 implements DbUpgrade {
 
     @Override
     public void performDataMigration(Connection conn) {
+        updateVMInstanceUserId(conn);
+    }
+
+    public void updateVMInstanceUserId(Connection conn) {
+        // For schemas before this, copy first user from an account_id which 
deployed already running VMs
+        s_logger.debug("Updating vm_instance column user_id using first user 
in vm_instance's account_id");
+        String vmInstanceSql = "SELECT id, account_id FROM 
`cloud`.`vm_instance`";
+        String userSql = "SELECT id FROM `cloud`.`user` where account_id=?";
+        String userIdUpdateSql = "update `cloud`.`vm_instance` set user_id=? 
where id=?";
+        try(PreparedStatement selectStatement = 
conn.prepareStatement(vmInstanceSql)) {
+            ResultSet results = selectStatement.executeQuery();
+            while (results.next()) {
+                long vmId = results.getLong(1);
+                long accountId = results.getLong(2);
+                try (PreparedStatement selectUserStatement = 
conn.prepareStatement(userSql)) {
+                    selectUserStatement.setLong(1, accountId);
+                    ResultSet userResults = selectUserStatement.executeQuery();
+                    if (userResults.next()) {
+                        long userId = userResults.getLong(1);
+                        try (PreparedStatement updateStatement = 
conn.prepareStatement(userIdUpdateSql)) {
+                            updateStatement.setLong(1, userId);
+                            updateStatement.setLong(2, vmId);
+                            updateStatement.executeUpdate();
+                        } catch (SQLException e) {
+                            throw new CloudRuntimeException("Unable to update 
user ID " + userId + " on vm_instance id=" + vmId, e);
+                        }
+                    }
+
+                } catch (SQLException e) {
+                    throw new CloudRuntimeException("Unable to update user ID 
using accountId " + accountId + " on vm_instance id=" + vmId, e);
+                }
+            }
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Unable to update user Ids for 
previously deployed VMs", e);
+        }
+        s_logger.debug("Done updating user Ids for previously deployed VMs");
     }
 
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/cb018b84/setup/db/db/schema-450to460.sql
----------------------------------------------------------------------
diff --git a/setup/db/db/schema-450to460.sql b/setup/db/db/schema-450to460.sql
index 8480c85..21a747d 100644
--- a/setup/db/db/schema-450to460.sql
+++ b/setup/db/db/schema-450to460.sql
@@ -19,4 +19,6 @@
 -- Schema upgrade from 4.5.0 to 4.6.0
 --
 
-INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 
'management-server', "stats.output.uri", "", "URI to additionally send 
StatsCollector statistics to", "", NULL, NULL, 0);
\ No newline at end of file
+INSERT IGNORE INTO `cloud`.`configuration` VALUES ("Advanced", 'DEFAULT', 
'management-server', "stats.output.uri", "", "URI to additionally send 
StatsCollector statistics to", "", NULL, NULL, 0);
+
+ALTER TABLE `cloud`.`vm_instance` ADD COLUMN `user_id` bigint unsigned NOT 
NULL COMMENT 'user id of VM deployer';

Reply via email to