DEV Community

Cover image for DDS to SQL DDL: migrating a physical file without breaking programs
Rainierio.dev
Rainierio.dev

Posted on Originally published at rainierio.dev

DDS to SQL DDL: migrating a physical file without breaking programs

DDS to SQL DDL: migrating a physical file without breaking programs

Converting a DDS physical file to an SQL table is mostly safe, and mostly boring if you plan it. The danger is a changed format level that makes every program fail with a level-check error at 2 a.m. This article is the order of operations I use. If the file formats still look strange, start with reading fixed-format RPG.

Why migrate

  • Identity columns and sequences replace hand-rolled "next number" files.
  • Constraints: primary key, foreign key, check and unique constraints are enforced by the database, not by every program.
  • Newer features target SQL objects. Both DDS and SQL files can be queried, but do not expect migration alone to make queries faster.
  • Features: long names, ALTER TABLE, temporal tables and row/column permissions need an SQL table.
  • SQL services and tooling (including embedded SQL in RPG) treat DDL-defined objects as first-class citizens.

You do not have to migrate everything. Do it for files you are changing anyway.

  1. Generate the DDL: Let Db2 write CREATE TABLE from the existing DDS file
  2. Keep the record format name: RCDFMT so programs still compile
  3. Understand the level check: Format level IDs decide if programs break
  4. Create, copy, test: Build the new table, copy data, compare
  5. Logicals and indexes: Keep the LF, or replace with an SQL index or view

Step 1: Generate the DDL

Use the ACS "Generate SQL" action on the file (Schemas > right-click > Generate SQL). From a program or script use the QSYS2.GENERATE_SQL procedure or the QSQGNDDL API; both can write to a source file member or an IFS stream file (stream file output arrived with 7.4 TR 13 and 7.3 TR 24). ACS is easier for a one-off.

CREATE TABLE MYLIB.CUSTPF (
  CUSTID  DECIMAL(9, 0) NOT NULL DEFAULT 0,
  CUSTNM  CHAR(30) NOT NULL DEFAULT '',
  BALANCE DECIMAL(11, 2) NOT NULL DEFAULT 0,
  OPENED  DATE NOT NULL DEFAULT CURRENT_DATE
)
RCDFMT CUSTR;
Enter fullscreen mode Exit fullscreen mode

Review the output, do not just run it. Check each field below.

Step 2: Keep the record format name

RPG file I/O refers to the record format name, so keep it. RCDFMT after the column list sets it. If you omit it, the format name equals the table's system name and any program that does WRITE CUSTR breaks at compile time. RENAME TABLE later does not change the format name, so renaming is no workaround for a missing RCDFMT.

Keep column names to 10 characters, matching DDS field names. If you want long names, use FOR COLUMN to keep the short system name:

CUSTOMER_NAME FOR COLUMN CUSTNM CHAR(30) NOT NULL DEFAULT ''
Enter fullscreen mode Exit fullscreen mode

Step 3: Understand level checks

Each file format has a record format level identifier. IBM lists its inputs as the record format name, the field names, the total record length, the number of fields and the field attributes (such as length and decimal positions). A compiled program stores the ID it saw. At open, the system compares; a mismatch gives CPF4131 (level check) unless the file or program uses LVLCHK(*NO).

If the new table has exactly the same format, the identifier is identical and you do not need to recompile. You must recompile if any of these changed:

  • a field added, removed, renamed or reordered
  • a type, length or decimal position changed
  • the format name changed

Check with DSPFD FILE(MYLIB/CUSTPF) TYPE(*RCDFMT), which shows the format level identifier. Compare old and new before cutover. If they differ, find out why before you recompile; do not disable LVLCHK as a shortcut.

Step 4: Create, copy, test

Work in a test library first (see the mental model of libraries and objects), then build the table under a new name so the old file stays untouched.

CRTDUPOBJ OBJ(CUSTPF) FROMLIB(MYLIB) OBJTYPE(*FILE) TOLIB(BACKUP) DATA(*YES)
Enter fullscreen mode Exit fullscreen mode

Then create the SQL table (as CUSTNEW, in the test library) and copy the data:

CPYF FROMFILE(MYLIB/CUSTPF) TOFILE(TESTLIB/CUSTNEW) MBROPT(*REPLACE) FMTOPT(*MAP *DROP)
Enter fullscreen mode Exit fullscreen mode

Or in SQL: INSERT INTO TESTLIB.CUSTNEW SELECT * FROM MYLIB.CUSTPF;. *MAP matches fields by name and *DROP discards fields that exist only in the old file, so CPYF can hide a field you forgot to add. Compare counts and a checksum or two afterwards.

Step 5: Logical files and indexes

Logical files depend on the physical file. You have three options:

  1. Keep the LFs. Recreate them with CRTLF from the existing DDS over the new table. Programs do not change.
  2. Replace with SQL indexes for keyed access: CREATE INDEX MYLIB.CUSTPF_I1 ON MYLIB.CUSTPF (CUSTNM). RPG can open an index as a keyed file. By default an index shares the table's record format. It gets its own format if you use RCDFMT, derived keys, key columns renamed with AS, a WHERE clause or an INCLUDE clause; after that, an ALTER TABLE on the table is not reflected in the index's format.
  3. Replace with views for select/omit logicals: CREATE VIEW ... AS SELECT ... WHERE .... A view cannot have ORDER BY, so it has no key; native I/O over a view reads in arrival sequence.

The easy first pass is option 1. Retire them later, one at a time.

Remember that renaming a physical file moves its logicals with it. If you rename the old PF out of the way, the LFs follow it, so delete or recreate them deliberately.

The surrogate approach

A less risky route for big or busy files: leave the old file in place, create the new SQL table alongside, and use INSERT triggers or a nightly INSERT ... SELECT to keep it synchronised while you move programs over. When the last program points to the new table, retire the old. It costs time but removes the cutover moment.

Pitfalls

  • Null-capable fields. SQL columns allow null unless you say NOT NULL. DDS fields are not null-capable unless ALWNULL was set. If you forget, RPG programs may read nulls they cannot represent. Use NOT NULL WITH DEFAULT.
  • Date formats. A DDS date field can carry any DATFMT and DATSEP. A DATE column created with CREATE TABLE is always *ISO. If the DDS used another format, the field attributes differ, so the level identifier will too. Check with DSPFFD and test every program that reads the field. Zoned fields of YYYYMMDD numbers should stay numeric; do not "fix" them to DATE during this migration.
  • Hidden columns. An identity column or a row-change timestamp added to the table changes the format level. Add those in a second step, after the straight migration works, then recompile deliberately.
  • Record format ID. The record format name is not the level identifier. Check both.
  • Journaling. If the old file was journaled, start journaling the new table with STRJRNPF before cutover. A table created in a schema made by CREATE SCHEMA is journaled automatically; one in a plain library is not.
  • Authority and triggers. Copy object authority (GRTOBJAUT ... REFOBJ), and recreate triggers and constraints.

Rollback plan

Before cutover, have:

  1. The CRTDUPOBJ or SAVOBJ backup of the old PF and its LFs.
  2. A written list of the steps in reverse: rename new PF away, rename old back, recreate LFs.
  3. A copy of data changes made after cutover if you may need to keep them (CPYF back from the new table).
  4. A time window where you stop writers: ALCOBJ on the file for the swap, then DLCOBJ.

Test the rollback in the test library at least once. A rollback you have never run is a guess.

Try it on PUB400

  1. Create a small DDS PF in your library on PUB400 (a CUSTPF source with R CUSTR and three fields) and compile it with CRTPF.
  2. Write some rows with UPDDTA or STRSQL.
  3. Write the equivalent CREATE TABLE ... RCDFMT CUSTR in Run SQL Scripts, as CUSTNEW.
  4. Run DSPFD ... TYPE(*RCDFMT) on both and compare the format level IDs. Then change a field length and see the ID change.
  5. Compile a tiny RPG program over the old file, point it at the new one with OVRDBF, and check for CPF4131.

Best resources

Next

Next: ILE modules, service programs and the binder

Related


Originally published on rainierio.dev. The full Learn IBM i track, with an Indonesian version, is at rainierio.dev/learn.

Top comments (0)