From c9989a7ddf887da24a14f0d167a22ea39943a425 Mon Sep 17 00:00:00 2001 From: alena Date: Wed, 13 Apr 2011 14:29:43 -0700 Subject: [PATCH] bug 9420: update existing user_statistics records with device_id and device_type status 9420: resolved fixed --- .../com/cloud/upgrade/dao/Upgrade218to22.java | 48 +++++++++++++++++++ setup/db/db/schema-21to22.sql | 2 - 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index bc3a4aff6c4..6edb221ad30 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -1010,11 +1010,59 @@ public class Upgrade218to22 implements DbUpgrade { pstmt = conn.prepareStatement("UPDATE vm_instance SET account_id=1, domain_id=1 WHERE type='ConsoleProxy' or type='SecondaryStorageVm'"); pstmt.executeUpdate(); pstmt.close(); + + // Update user statistics + upadteUserStats(conn); + } catch (SQLException e) { throw new CloudRuntimeException("Can't update data center ", e); } } + private void upadteUserStats(Connection conn) { + try { + + // update device_type information + PreparedStatement pstmt = conn.prepareStatement("UPDATE user_statistics SET device_type='DomainRouter'"); + pstmt.executeUpdate(); + pstmt.close(); + s_logger.debug("Upgraded userStatistcis with device_type=DomainRouter"); + + // update device_id infrormation + pstmt = conn.prepareStatement("SELECT id, account_id, data_center_id FROM user_statistics"); + ResultSet rs = pstmt.executeQuery(); + + while (rs.next()) { + Long id = rs.getLong(1); // user stats id + Long accountId = rs.getLong(2); // account id + Long dataCenterId = rs.getLong(3); // zone id + + pstmt = conn.prepareStatement("SELECT id from vm_instance where account_id=? AND data_center_id=? AND type='DomainRouter'"); + pstmt.setLong(1, accountId); + pstmt.setLong(2, dataCenterId); + ResultSet rs1 = pstmt.executeQuery(); + + if (!rs1.next()) { + throw new CloudRuntimeException("Unable to update user_statistics table with device id"); + } + + Long deviceId = rs1.getLong(1); + + pstmt = conn.prepareStatement("UPDATE user_statistics SET device_id=? where id=?"); + pstmt.setLong(1, deviceId); + pstmt.setLong(2, id); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement(""); + + } + s_logger.debug("Upgraded userStatistcis with deviceId(s)"); + + } catch (Exception e) { + throw new CloudRuntimeException("Failed to migrate usage events: ", e); + } + } + public void upgradePortForwardingRules(Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("SELECT id, public_ip_address, public_port, private_ip_address, private_port, protocol FROM ip_forwarding WHERE forwarding=1"); diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index 188d6fa0c54..2e6271d4c0c 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -379,8 +379,6 @@ ALTER TABLE `cloud`.`user_statistics` ADD COLUMN `public_ip_address` varchar(15) ALTER TABLE `cloud`.`user_statistics` ADD COLUMN `device_id` bigint unsigned NOT NULL default 0; ALTER TABLE `cloud`.`user_statistics` ADD COLUMN `device_type` varchar(32) NOT NULL default 'DomainRouter'; -INSERT INTO user_statistics ( account_id, data_center_id, device_id, device_type ) SELECT VM.account_id, VM.data_center_id, DR.id,'DomainRouter' FROM vm_instance VM, domain_router DR WHERE VM.id = DR.id; - CREATE TABLE `cloud`.`remote_access_vpn` ( `vpn_server_addr_id` bigint unsigned UNIQUE NOT NULL, `account_id` bigint unsigned NOT NULL,