Extension files allow you to extend the life of your database without having to completely re-engineer it.
As business programmers, we spend most of our time working with the database, one way or another. Unless we're lucky enough to be in on the ground floor of a new installation of a modern package, chances are that database will require modifications to support changing business requirements. Even if it's an entirely custom application suite built specifically for your business model, the database probably has become a bit long in the tooth and needs to be updated to take advantage of newer features. Rafael Victoria-Pereira has written several excellent articles on database modernization, and I invite you to spend some time with them. Today, though, I'm going to talk about extending your existing IBM i database without modernizing it.
But Isn't That Wrong?
Wrong is a strong word. How about "imperfect"? In a perfect world, we'd love to completely modernize our applications, but my very first mentor drilled one simple concept into my head: Don't let the perfect be the enemy of the good. That means sometimes making a conscious decision to take a less-than-perfect approach in order to provide a significant benefit to the business. Because remember, at the end of the day, we can best measure ourselves as business programmers not by how leading-edge our code is, but by how much better the business runs.
That's not to say the two can't coincide! We should try to find ways to incorporate modern techniques into our business solutions whenever possible. However, there are times when a more prosaic approach is required. The situation I have in mind is one we've all encountered: adding one or more data points to an existing file.
I'm in the process of doing this very thing for a number of files in our database. The primary files are part of a packaged ERP system, and much of the code is quite old. The beauty of our platform is that code can run literally for decades, migrating from hardware to hardware without changes. The downside of our platform is much the same; those old programs resist change, and things that were absolutely a necessity 10 (or 20 or 30) years ago now prevent us from moving forward.
The Genesis of the Extension File
Let's take a simple case: the customer master file. Customer processing requirements grow over time, whether through new regulations, new technology requirements, or just plain new business. One very simple case I encountered was the county. We needed to be able to identify the county for some tax reporting functions, and while our customer database file had traditional city, state, and postal code, even country code, it didn't have a place for county. (In case you're wondering, there is no foolproof way to go from ZIP code to county. I tried. ZIP codes span counties. In fact, several ZIP codes actually cross state boundaries. If you don't believe me, check out ZIP code 97635.) In any case, the decision was made to include the county as part of the customer definition. At that point, a decision had to be made: modify the customer master or extend?
We all know the difficulties encountered when modifying a highly used master file. One of the biggest benefits of the IBM midrange is the concept of the externally described file, but it did not come without a price: changing a database file has historically been the most difficult task in any development effort. If you change a file, you have to recompile every program that uses that file or else you encounter the deadly CPF4131, format level check. And I'm going to draw a line in the sand here; I don't even support the LVLCHK(*NO) workaround. It works in a subset of cases when used with extreme caution, but to me the potential for catastrophe far outweighs the short-term benefits.
In my case, the developers used as forward-thinking a technique as possible for the time: an extension file. An extension file is a file with exactly the same primary key as the master file it is extending. The record contains at a minimum the unqiue key fields from the master file and fields for the new data points that you need. When designing an extension file, you must address two main issues:
- Do you always create an extension record even if none of the fields are populated?
- Do you include audit fields in the extension file, or just piggyback on the extended record?
The answers aren’t critical to the general idea of extension files, but you should still consider them and then apply the answers you decide upon consistently throughout your organization. My personal preference is to always have an extension record but with no audit fields. For example, if you use a last-changed timestamp and user ID on your files, then have those fields on the master record only and update them even if you only update a field in the extension record. That's because I consider the fields in the extension file just to be more columns for the master record.
Master Files with No Unique Key
“But what if the file has no primary key?” you might ask. Excellent question! And my response is that you must absolutely get a unique key on that file. With today’s databases, you can even let the system generate the value for that field itself, although I’m just as happy with using a data area or some other global entity to hold the last key value. I can tell you though that I’m in the middle of exactly that situation. I'm currently dealing with a history file whose only “key” is the date and time the record was written, along with a small 3-digit suffix that I suppose was always unique back when the original code was written. For various reasons, though, that suffix is no longer unique, so I end up with transactions with literally no unique key.
So instead, I'm using a multi-step approach. First, I created an extension file. The primary key on the extension file uses a large numeric ID field. My file can contain roughly a billion records. For files of that size, I'd go with at least a 10- or 11-digit numeric ID field (this allows for plenty of growth). Then I do something really, really bad. Close your eyes while you read this part.
Seriously, it's a real kludge. Whenever I write a record to the history file, I use the relative record number as the key for the extension file. It's functional. When I'm getting data via SQL (something we'll talk about a lot more in future installments), I can do something like this:
SELECT FLDA, FLDB, XFLDC FROM MYHIST JOIN MYHISTX on RRN(MYHIST) = XID
FLDA and FLDB are fields from the original MYHIST file, where XFLDC is a field from the extension file MYHISTX. The JOIN uses the relative record number of the original file to join to the ID field (XID) of the extension file. As I said, it's not pretty. And if we ever reorganize the main file, we' re in deep trouble. This isn't anything new; in fact, it's rather ancient and, as noted, potentially very fragile. The only reason I can get away with it right now is that the main file is a transaction history file where records are not typically deleted.
Things can't stay this way. The master file needs a true unique key. So my next goal is to follow a simple strategy:
- Add a new index field to the original history file.
- Run a program that will populate that field.
- Update existing extension records to the new ID field.
- Change programs using the extension file to use the new ID field instead of RRN.
When I say simple, I mean straightforward but not necessarily easy. Since the primary file is used by literally hundreds of programs, I'll need to schedule a time when the system is completely down to perform some of these steps. But that's exactly the reason we want to use extension files; once I've made this modification, I won't have to make any more changes to the original file. Any changes will be to the extension file, and in a subsequent article we'll talk about how to minimize the impact of changes to that file as well.
Please stay tuned!