This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BZ 1069545] Add a ManyToMany relationship between resource type and …
…proper subcategories. Also, add new table, upgrade schema, and upgrade tasks to move to the new model.
- Loading branch information
Stefan Negrea
committed
Apr 22, 2014
1 parent
fc72cdf
commit f49e400
Showing
4 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
modules/core/dbutils/src/main/java/org/rhq/core/db/upgrade/SubcategoryUpgradeTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.rhq.core.db.upgrade; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import mazz.i18n.Logger; | ||
|
||
import org.rhq.core.db.DatabaseType; | ||
import org.rhq.core.db.DbUtilsI18NFactory; | ||
import org.rhq.core.db.DbUtilsI18NResourceKeys; | ||
|
||
/** | ||
* Updates the address field of storage node entities to ensure we are storing IP addresses and not hostnames. We want | ||
* to store the IP address since that is what Cassandra uses for inter-node communication. JMX operations that return | ||
* nodes will return the node IP addresses and not hostnames. | ||
* | ||
* @author John Sanda | ||
*/ | ||
public class SubcategoryUpgradeTask implements DatabaseUpgradeTask { | ||
|
||
private final Logger log = DbUtilsI18NFactory.getLogger(StorageNodeAddressUpgradeTask.class); | ||
|
||
@Override | ||
public void execute(DatabaseType databaseType, Connection connection) throws SQLException { | ||
String sql = "SELECT id, name, resource_type_id FROM rhq_resource_subcat"; | ||
|
||
log.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, sql); | ||
List<Object[]> results = databaseType.executeSelectSql(connection, sql); | ||
|
||
Integer primaryId; | ||
Integer duplicateId; | ||
Integer resourceTypeId; | ||
String name = null; | ||
Map<String, Object[]> primaryNameMap = new HashMap<String, Object[]>(); | ||
|
||
for (Object[] row : results) { | ||
name = (String) row[1]; | ||
if (!primaryNameMap.containsKey(name)) { | ||
primaryNameMap.put(name, row); | ||
primaryId = databaseType.getInteger(row[0]); | ||
|
||
if (row[2] != null) { | ||
resourceTypeId = databaseType.getInteger(row[2]); | ||
|
||
//Create the linking entry to link resource to it's proper subcategories | ||
log.debug(DbUtilsI18NResourceKeys.MESSAGE, | ||
"Create subcategory to parent resource entry for resource type [id= " + resourceTypeId | ||
+ "] with subcategory [id= " + primaryId + "]"); | ||
String insert = "INSERT INTO rhq_resource_type_subcat ( RESOURCE_TYPE_ID, RESOURCE_SUBCAT_ID) values" | ||
+ " ( " + resourceTypeId + " , " + primaryId + " )"; | ||
log.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, insert); | ||
databaseType.executeSql(connection, insert); | ||
} | ||
} else { | ||
duplicateId = databaseType.getInteger(row[0]); | ||
primaryId = databaseType.getInteger(primaryNameMap.get(name)[0]); | ||
|
||
if (row[2] != null) { | ||
resourceTypeId = databaseType.getInteger(row[2]); | ||
|
||
//Create the linking entry to link resource to it's proper subcategories | ||
log.debug(DbUtilsI18NResourceKeys.MESSAGE, | ||
"Create subcategory to parent resource entry for resource type [id= " + resourceTypeId | ||
+ "] with subcategory [id= " + primaryId + "]"); | ||
String insert = "INSERT INTO rhq_resource_type_subcat ( RESOURCE_TYPE_ID, RESOURCE_SUBCAT_ID) values" | ||
+ " ( " + resourceTypeId + " , " + primaryId + " )"; | ||
log.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, insert); | ||
databaseType.executeSql(connection, insert); | ||
} | ||
|
||
//Make resources that were pointing to the duplicate subcategory to this other subcategory | ||
log.debug(DbUtilsI18NResourceKeys.MESSAGE, "Replacing subcategory [id= " + duplicateId + "] with [id= " | ||
+ primaryId + "]"); | ||
String update = "UPDATE rhq_resource_type SET subcategory = " + primaryId + " WHERE subcategory = " | ||
+ duplicateId; | ||
log.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, update); | ||
databaseType.executeSql(connection, update); | ||
|
||
//delete duplicate subcategory | ||
log.debug(DbUtilsI18NResourceKeys.MESSAGE, "Delete subcategory [id= " + duplicateId | ||
+ "] because it is duplicated."); | ||
String delete = "DELETE from rhq_resource_subcat WHERE id = " + duplicateId; | ||
log.debug(DbUtilsI18NResourceKeys.EXECUTING_SQL, delete); | ||
databaseType.executeSql(connection, delete); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters