Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
[BZ 1069545] Add a ManyToMany relationship between resource type and …
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
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);
}
}
}
}
14 changes: 14 additions & 0 deletions modules/core/dbutils/src/main/scripts/dbsetup/inventory-schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@
</constraint>
</table>

<!-- Many To Many mapping for resource type to its proper subcategories -->
<table name="RHQ_RESOURCE_TYPE_SUBCATS">
<column name="RESOURCE_TYPE_ID" required="true" type="INTEGER" references="RHQ_RESOURCE_TYPE"/>
<column name="RESOURCE_SUBCAT_ID" required="true" type="INTEGER" references="RHQ_RESOURCE_SUBCAT"/>

<!-- not using full words to fit index name length -->
<constraint name="RHQ_RES_TYPE_SUBCATS_KEY">
<primaryKey>
<field ref="RESOURCE_TYPE_ID"/>
<field ref="RESOURCE_SUBCAT_ID"/>
</primaryKey>
</constraint>
</table>

<table name="RHQ_PROCESS_SCAN">
<column name="ID" default="sequence-only" initial="10001"
primarykey="true" required="true" type="INTEGER"/>
Expand Down
38 changes: 37 additions & 1 deletion modules/core/dbutils/src/main/scripts/dbupgrade/db-upgrade.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@
<schemaSpec version="2.143">
<schema-alterColumn table="RHQ_RAW_CONFIG" column="config_ID" nullable="true"/>
</schemaSpec>

<schemaSpec version="2.144">
<!-- RHQ_AGENT_INSTALL -->
<schema-createSequence name="RHQ_AGENT_INSTALL_SEQ" initial="10001" />
Expand Down Expand Up @@ -2325,6 +2325,42 @@
</statement>
</schema-directSQL>
</schemaSpec>

<schemaSpec version="2.147">
<schema-directSQL>
<statement desc="Creating table RHQ_RESOURCE_TYPE_SUBCATS">
CREATE TABLE RHQ_RESOURCE_TYPE_SUBCATS (
RESOURCE_TYPE_ID INTEGER,
RESOURCE_SUBCAT_ID INTEGER)
</statement>
</schema-directSQL>

<schema-alterColumn table="RHQ_RESOURCE_TYPE_SUBCATS" column="RESOURCE_TYPE_ID" nullable="FALSE" />
<schema-alterColumn table="RHQ_RESOURCE_TYPE_SUBCATS" column="RESOURCE_SUBCAT_ID" nullable="FALSE" />

<schema-directSQL>
<statement>
ALTER TABLE RHQ_RESOURCE_TYPE_SUBCATS
ADD CONSTRAINT RHQ_RES_TYPE_SUBCATS_KEY
PRIMARY KEY ( RESOURCE_TYPE_ID, RESOURCE_SUBCAT_ID )
</statement>
<statement desc="Creating RHQ_RESOURCE_TYPE_SUBCATS foreign key to RHQ_RESOURCE_TYPE">
ALTER TABLE RHQ_RESOURCE_TYPE_SUBCATS
ADD CONSTRAINT RHQ_RTS_RESOURCE_TYPE_ID_FK
FOREIGN KEY (RESOURCE_TYPE_ID)
REFERENCES RHQ_RESOURCE_TYPE (ID)
</statement>
<statement desc="Creating RHQ_ROLE_BUNDLE_GROUP_MAP foreign key to RHQ_BUNDLE_GROUP">
ALTER TABLE RHQ_RESOURCE_TYPE_SUBCATS
ADD CONSTRAINT RHQ_RTS_RESOURCE_SUBCAT_ID_FK
FOREIGN KEY (RESOURCE_SUBCAT_ID)
REFERENCES RHQ_RESOURCE_SUBCAT (ID)
</statement>
</schema-directSQL>

<schema-javaTask className="SubcategoryUpgradeTask" />

</schemaSpec>
</dbupgrade>
</target>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ public class ResourceType implements Serializable, Comparable<ResourceType> {
@OneToMany(mappedBy = "resourceType", cascade = CascadeType.ALL)
private Set<PackageType> packageTypes;

@OneToMany(mappedBy = "resourceType", cascade = CascadeType.ALL)
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
@JoinTable(name = "RHQ_RESOURCE_TYPE_SUBCATS", joinColumns = { @JoinColumn(name = "RESOURCE_TYPE_ID") }, inverseJoinColumns = { @JoinColumn(name = "RESOURCE_SUBCAT_ID") })
@OrderBy
//@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
private List<ResourceSubCategory> subCategories;

@OneToMany(mappedBy = "resourceType", cascade = CascadeType.REMOVE)
Expand Down Expand Up @@ -844,7 +847,6 @@ public void addChildSubCategory(ResourceSubCategory subCategory) {
if (this.subCategories == null) {
this.subCategories = new ArrayList<ResourceSubCategory>();
}
subCategory.setResourceType(this);
this.subCategories.add(subCategory);
}

Expand Down

0 comments on commit f49e400

Please sign in to comment.