Let's imagine you have a recipe book (analogous to a database) with specific instructions for each recipe (analogous to rules for a data type). Each recipe has a key ingredient (analogous to a key in a database table) that makes it unique. Now, suppose you realize that a particular recipe's key ingredient needs to be changed, and you've already written down all the specific instructions for that recipe.
If you catch this mistake right after creating the recipe, you can simply erase the instructions, change the key ingredient, and rewrite the instructions. However, if you discover this error later, after many copies of the recipe have been shared (analogous to rules being locked in an old version), things get trickier.
In the digital world, this is like having a database table with predefined rules (recipe instructions) and realizing that you need to change the key (key ingredient). It's not as simple as starting from scratch, especially when the rules are already locked in a certain version.
The challenge here is similar to going back to your original recipe book, crossing out the old key ingredient, and replacing it with a new one without messing up the existing instructions. Even if the original version is locked, you need to find a way to update it without causing chaos.
In the world of databases, this involves a careful process to modify the key without disrupting the existing data and rules. This ensures that the database remains consistent and accurate, just like making sure your recipes still make sense after changing a key ingredient.
So modifying the key of a database table is a common requirement in response to various reasons, and developers may find themselves needing to alter the key defined in a data type. If this change is required shortly after the data type is created, one can simply delete all the generated rules and redefine the data type from scratch. However, the situation becomes more complex when the request to change the key arises later, and all the rules are already locked in the old RSV (Rule Set Version). It's important to note that whether the rule is locked or not becomes inconsequential in the process of changing the key. In this post, I will outline the detailed steps involved in making such a key modification despite the rules being locked in the old RSV.
The concept of a primary key, or simply "key," is defined at two levels in Pega: at the Pega level and the database management system (DBMS) level. It's worth noting that the keys at these levels may differ. For instance, in Pega, the default key for a Work- derived class is "pyID," while at the DBMS level, the primary key is "pzInsKey." On the other hand, for a Blob table, regardless of whether it's derived from Work- or Data-, "pzInsKey" is used as the primary key at the DBMS level.
Since Pega 7.4, CustomerData has been adopted as the default database for Data Type. Consequently, the out-of-the-box table generated by a Data Type is Non-Blob. This means that not only "pzPVStream" but also "pzInsKey" does not exist. Instead, any custom property specified as a key in the Data Type configuration is set as the primary key in the DBMS. In the provided example, the MacAddress property has been configured as the key.
Upon inspecting the physical table in the DBMS, it is evident that the primary key is automatically configured on the "macaddress" property.
At the Pega level, the key "MacAddress" is located on the class form. It's important to note that the key field appears grayed-out, and this is due to the table being already populated. When a table has existing records, the key becomes non-editable.
How to change the key?
Suppose the Rule Set Version (RSV) is locked, and there's a change in requirements: you're tasked with switching the key from "MacAddress" to "SerialNo." Even though all the rules are locked, it is possible to accomplish this without directly modifying the rules. The following steps outline how this can be achieved.
1. As mentioned earlier, the key in Pega cannot be edited in the class form when the physical table has existing records. Therefore, the initial step is to truncate the table. If the table contains crucial records, it is advisable to manually create a backup. In this instance, the pgAdmin tool was utilized to export all records into a CSV file.
2.Access the class form through Dev Studio. The key field should now be editable since the table no longer contains records. It's essential to note that a Class is a unique type of rule instance, and unlike other rules, there is no check-in or check-out concept. Even when the Rule Set Version (RSV) is locked, this rule is always savable, similar to a Data instance. Replace "MacAddress" with "SerialNo" and proceed to save the changes.
3.Despite changing the key in the class form, this action does not automatically trigger a Primary key change at the DBMS level. Upon clicking the "Test Connection" button in the class form, an error message will be displayed, indicating a key mismatch. The error message may state, "There is a key mismatch: the keys defined for class MyCo-MyApp-Data-Computer do not match the primary key columns for the external data table data.pr_myco_myapp_data_computer in the CustomerData database. The class keys are [SerialNo], but the table's primary keys are [data.macaddress]." This highlights that modifying the key remains an irregular operation, and the change must be manually executed at the DBMS level.
4.In the DBMS, execute a Data Definition Language (DDL) command to drop the existing Primary key. Alternatively, you can use the pgAdmin tool for this task.
5.Recreate the Primary key.
6.Assign a name to the Primary key. It can be the same as the original one. In this instance, use the name "pr_myco_myapp_data_computer_pk."
7. Define "serialno" as the column for the Primary Key.
8.Verify that "serialno" is now set as the Primary Key in the DBMS.
9. Reattempt the "Test Connection" process and observe that the previously encountered mismatch error message is no longer present. This indicates that the key, which is now set as "SerialNo," is synchronized at both the Pega level and the DBMS level.
10. The final task remaining is to restore the backup records into the table. Utilize the pgAdmin tool and choose the "Import" option for this step.
11.Ensure that all records have been successfully restored.
Happy Learning :)
Comments
Post a Comment