Skip to content

Commit fd424d5

Browse files
committed
move adding indexes to java class
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent bb5a296 commit fd424d5

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

engine/schema/src/main/java/com/cloud/upgrade/dao/DatabaseAccessObject.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public void createIndex(Connection conn, String tableName, String indexName, Str
113113
}
114114
}
115115

116+
public void renameIndex(Connection conn, String tableName, String oldName, String newName) {
117+
String stmt = String.format("ALTER TABLE %s RENAME INDEX %s TO %s", tableName, oldName, newName);
118+
s_logger.debug(String.format("Statement: %s", stmt));
119+
try (PreparedStatement pstmt = conn.prepareStatement(stmt)) {
120+
pstmt.execute();
121+
s_logger.debug(String.format("Renamed index %s to %s", oldName, newName));
122+
} catch (SQLException e) {
123+
s_logger.warn(String.format("Unable to rename index %s to %s", oldName, newName), e);
124+
}
125+
}
126+
116127
protected static void closePreparedStatement(PreparedStatement pstmt, String errorMessage) {
117128
try {
118129
if (pstmt != null) {

engine/schema/src/main/java/com/cloud/upgrade/dao/DbUpgradeUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public static void addIndexIfNeeded(Connection conn, String tableName, String...
3131
}
3232
}
3333

34+
public static void renameIndexIfNeeded(Connection conn, String tableName, String oldName, String newName) {
35+
if (!dao.indexExists(conn, tableName, oldName)) {
36+
dao.renameIndex(conn, tableName, oldName, newName);
37+
}
38+
}
39+
3440
public static void addForeignKey(Connection conn, String tableName, String tableColumn, String foreignTableName, String foreignColumnName) {
3541
dao.addForeignKey(conn, tableName, tableColumn, foreignTableName, foreignColumnName);
3642
}

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41800to41810.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,42 @@ private void addIndexes(Connection conn) {
258258
DbUpgradeUtils.addIndexIfNeeded(conn, "cluster_details", "name");
259259

260260
DbUpgradeUtils.addIndexIfNeeded(conn, "vm_stats", "vm_id");
261+
262+
DbUpgradeUtils.addIndexIfNeeded(conn, "host", "mgmt_server_id");
263+
DbUpgradeUtils.addIndexIfNeeded(conn, "host", "resource");
264+
DbUpgradeUtils.addIndexIfNeeded(conn, "host", "resource_state");
265+
DbUpgradeUtils.addIndexIfNeeded(conn, "host", "type");
266+
267+
DbUpgradeUtils.renameIndexIfNeeded(conn, "user_ip_address", "public_ip_address", "uk_public_ip_address");
268+
DbUpgradeUtils.addIndexIfNeeded(conn, "user_ip_address", "public_ip_address");
269+
DbUpgradeUtils.addIndexIfNeeded(conn, "user_ip_address", "data_center_id");
270+
DbUpgradeUtils.addIndexIfNeeded(conn, "user_ip_address", "vlan_db_id");
271+
DbUpgradeUtils.addIndexIfNeeded(conn, "user_ip_address", "removed");
272+
273+
DbUpgradeUtils.addIndexIfNeeded(conn, "vlan", "vlan_type");
274+
DbUpgradeUtils.addIndexIfNeeded(conn, "vlan", "data_center_id");
275+
DbUpgradeUtils.addIndexIfNeeded(conn, "vlan", "removed");
276+
277+
DbUpgradeUtils.addIndexIfNeeded(conn, "network_offering_details", "name");
278+
279+
DbUpgradeUtils.addIndexIfNeeded(conn, "network_offering_details", "resource_id", "resource_type");
280+
281+
DbUpgradeUtils.addIndexIfNeeded(conn, "service_offering", "cpu");
282+
DbUpgradeUtils.addIndexIfNeeded(conn, "service_offering", "speed");
283+
DbUpgradeUtils.addIndexIfNeeded(conn, "service_offering", "ram_size");
284+
285+
DbUpgradeUtils.addIndexIfNeeded(conn, "op_host_planner_reservation", "resource_usage");
286+
287+
DbUpgradeUtils.addIndexIfNeeded(conn, "storage_pool", "pool_type");
288+
DbUpgradeUtils.addIndexIfNeeded(conn, "storage_pool", "data_center_id", "status", "scope", "hypervisor");
289+
290+
DbUpgradeUtils.addIndexIfNeeded(conn, "router_network_ref", "guest_type");
291+
292+
DbUpgradeUtils.addIndexIfNeeded(conn, "domain_router", "role");
293+
294+
DbUpgradeUtils.addIndexIfNeeded(conn, "async_job", "instance_type", "job_status");
295+
296+
DbUpgradeUtils.addIndexIfNeeded(conn, "cluster", "managed_state");
261297
}
262298

263299
private void checkAndUpdateAffinityGroupNameCharSetToUtf8mb4(Connection conn) {

engine/schema/src/main/resources/META-INF/db/schema-41800to41810.sql

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -591,44 +591,6 @@ ALTER TABLE `cloud`.`vpc_offerings` MODIFY `display_text` VARCHAR(255) CHARACTER
591591

592592
-- Scalability and DB optimisations
593593

594-
-- Host additions and listing
595-
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__mgmt_server_id` (`mgmt_server_id`);
596-
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__resource` (`resource`);
597-
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__resource_state` (`resource_state`);
598-
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__type` (`type`);
599-
600-
-- IP address and VM view listing speed ups
601-
ALTER TABLE `cloud`.`user_ip_address` RENAME INDEX `public_ip_address` TO `uk_public_ip_address`;
602-
ALTER TABLE `cloud`.`user_ip_address` ADD INDEX `i_user_ip_address__public_ip_address` (`public_ip_address`);
603-
ALTER TABLE `cloud`.`user_ip_address` ADD INDEX `i_user_ip_address__data_center_id` (`data_center_id`);
604-
ALTER TABLE `cloud`.`user_ip_address` ADD INDEX `i_user_ip_address__vlan_db_id` (`vlan_db_id`);
605-
ALTER TABLE `cloud`.`user_ip_address` ADD INDEX `i_user_ip_address__removed` (`removed`);
606-
607-
ALTER TABLE `cloud`.`vlan` ADD INDEX `i_vlan__vlan_type` (`vlan_type`);
608-
ALTER TABLE `cloud`.`vlan` ADD INDEX `i_vlan__data_center_id` (`data_center_id`);
609-
ALTER TABLE `cloud`.`vlan` ADD INDEX `i_vlan__removed` (`removed`);
610-
611-
ALTER TABLE `cloud`.`network_offering_details` ADD INDEX `i_network_offering_details__name` (`name`);
612-
613-
ALTER TABLE `cloud`.`resource_tags` ADD INDEX `i_resource_tags__resource_id__resource_type` (`resource_id`, `resource_type`);
614-
615-
ALTER TABLE `cloud`.`service_offering` ADD INDEX `i_service_offering__cpu` (`cpu`);
616-
ALTER TABLE `cloud`.`service_offering` ADD INDEX `i_service_offering__speed` (`speed`);
617-
ALTER TABLE `cloud`.`service_offering` ADD INDEX `i_service_offering__ram_size` (`ram_size`);
618-
619-
ALTER TABLE `cloud`.`op_host_planner_reservation` ADD INDEX `i_op_host_planner_reservation__resource_usage`(`resource_usage`);
620-
621-
ALTER TABLE `cloud`.`storage_pool` ADD INDEX `i_storage_pool__pool_type` (`pool_type`);
622-
ALTER TABLE `cloud`.`storage_pool` ADD INDEX `i_storage_pool__data_center_id_status_scope_hypervisor` (`data_center_id`, `status`, `scope`, `hypervisor`);
623-
624-
ALTER TABLE `cloud`.`router_network_ref` ADD INDEX `i_router_network_ref__guest_type` (`guest_type`);
625-
626-
ALTER TABLE `cloud`.`domain_router` ADD INDEX `i_domain_router__role` (`role`);
627-
628-
ALTER TABLE `cloud`.`async_job` ADD INDEX `i_async_job__instance_type_job_status` (`instance_type`, `job_status`);
629-
630-
ALTER TABLE `cloud`.`cluster` ADD INDEX `i_cluster__managed_state` (`managed_state`);
631-
632594
-- speeds up user_vm_view (listVM) queries by forcing index on user_ip_address table
633595
DROP VIEW IF EXISTS `cloud`.`user_vm_view`;
634596
CREATE VIEW `user_vm_view` AS

0 commit comments

Comments
 (0)