Extending the JIRA database with a custom schema

Now that we know that JIRA schema definitions are maintained in WEB-INF/classes/ entitydefs/entitygroup.xml and entitymodel.xml, let us have a look at extending the existing schema definitions. How would you extend the JIRA scheme if you wanted to add one or two custom tables into JIRA? Is it just about creating the new tables in our database? We will see that in this recipe.

Note

For plugins, it is recommended to use Active Objects technology to persist data. The JIRA schema itself should be modified only when it is absolutely necessary to have pre-defined tables created in the JIRA database.

If the JIRA schema is modified, care must be taken during upgrades to port those changes to the new version.

How to do it...

JIRA uses the schema definitions entered in the WEB-INF/classes/entitydefs/ entitygroup.xml and entitymodel.xml files. It makes use of these files not only to validate and create the schema but also during the import and export of the JIRA data backup. JIRA also uses these entity definitions to read and write to a database, using OFBizDelegator (https://docs.atlassian.com/jira/latest/com/atlassian/jira/ofbiz/OfBizDelegator.html), details of which we will see in the upcoming recipes.

The following are the steps to add a new table into the JIRA schema. Let us assume we are adding a table to hold the details of an employee:

  1. Identify an entity name for the table. This could be the same as the table name or different from it. This name will be used in the XML backups and also by OFBizDelegator to read or write the data.

    In our example, let us choose Employee as the entity name.

  2. Modify the WEB-INF/classes/entitydefs/entitygroup.xml file to include the new entity group definition:
              <entity-group group="default" entity="Employee"/>

    Here, the group attribute refers to the group name the delegator is associated with. You can find it in the WEB-INF/classes/entityengine.xml file, as shown in the following code snippet:

             <delegator name="default" entity-model-reader="main" 
               entity-group-reader="main">
               <group-map group-name="default" datasource-name="defaultDS"/>
             </delegator>

    The entity attribute holds the name of the entity.

  3. Modify the WEB-INF/classes/entitydefs/entitymodel.xml file to include the new entity definition:
          <entity entity-name="Employee" table-name="employee" 
          package-name="">
            <field name="id" type="numeric"/>
            <field name="name" type="long-varchar"/>
            <field name="address" col-name="empaddress" 
             type="long-varchar"/>
            <field name="company" type="long-varchar"/>
            <prim-key field="id"/>
            <index name="emp_entity_name">
            <index-field name="name"/>
            </index>
          </entity>

    Here, the entity-name attribute holds the name of the entity we have used in the previous step. The table-name attribute holds the name of the table; it is optional and will be derived from entity-name, if not present. package-name can be used if you want to organize and structure the entities' definitions into different packages.

    The entity element contains one field element for each column in the table that needs to be created. The field element has a name attribute that holds the name of the field. If the column name of the field is different, the col-name attribute can be used, as in the case with employee address. If col-name is missing, the name of the field is used. The next important attribute is type. In our example, id is numeric whereas name and address are long-varchar.

    These types of definitions of a field are mapped to the appropriate column type for each database type. The field-type mappings are stored under WEB-INF/classes/entitydefs/ and are declared in entityengine.xml, as shown in the following code snippet:

          <field-type name="h2" loader="maincp" 
          location="entitydefs/fieldtype-h2.xml"/>

    If you look inside fieldtype-h2.xml, you will notice that numeric is mapped to BIGINT and long-varchar is mapped to VARCHAR. You can find out the various mappings and even the related Java data type from the same file.

    The prim-key element is used to define the primarykey constraint for the table, as shown previously. In our case, id is the primary key. It is mandatory to name the primary key as id for all the new tables we are creating.

    The index element creates a DB index for the field specified for that table. We can specify the index name and the group of the fields that needs to be indexed underneath it.

    You can also define the relationship between entities using the element relation as shown in the following code snippet:

            <relation type="one"  rel-entity-name="Company">
                  <key-map field-name="company" rel-field-name="id"/>
           </relation>

    Here, we are adding a relationship between the Employee entity and the Company entity by saying that an employee can have only one company. In the preceding case, Employee should have a field company that points to the id field of a company's record. In other words, the company field in an employee's record will be the foreign key to the company's record.

    More details on entity definitions can be found at https://cwiki.apache.org/confluence/display/OFBIZ/Entity+Engine+Guide#EntityEngineGuide-EntityModeling.

  4. Restart JIRA after the changes have been made.

How works...

When JIRA is restarted with the previous changes, you will notice that a warning message appear in the logs during startup, as shown in the following screenshot:

How works...

Once JIRA recognizes that there is no table corresponding to the new entity name employee in the database, it will create one, as shown in the following screenshot:

How works...

Even the index information is stored, as highlighted in the following screenshot:

How works...

If you want to add a new column to an existing table, you can add an additional field definition to the entity and, on restarting JIRA, the table will be updated to include the column.

You will notice an error message in the JIRA logs if the database has a table, or a column in the table, that doesn't have a valid entity or field definition in the entitymodel.xml file.

Tip

Care must be taken to update the entitygroup.xml and entitymodel.xml files when JIRA is upgraded or else the changes will be lost.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.231.26