Omnis Technical Note TNDM0005

Synchronizing a Schema and a corresponding Table in a datafile

For Omnis Studio
By Rudolf Bargholz, Online Travel Services AG, Zurich (www.travel.ch)

Sometimes the structure of a schema changes during the evolution of an application: we add new columns to a schema, need to change the size of some of the columns. When this happens, the structure of the server table no longer corresponds to that of the schema. The following code is a very simple method to synchronize the schema definitions of a table and the actual table structure. Essentially a file format is created with the same name as that of the server table, the datafile is opened and an "Update data dictionary" is performed. The "Update data dictionary" command synchronizes the server table with the temporary file format created.

Parameters:
 pDataFilePath
 pLibName
 pSchemaName

Local Variables:
 Name: Char
 Type: Char
 Subtype: Char
 Length: Char
 TableName: Char
 ListSchema: List
 Ref: Item Reference
 ClassRef: Item Reference

If FileOps.$doesfileexist(pDataFilePath)
  Set reference ClassRef to $libs.[pLibName].$schemas.[pSchemaName]
  Calculate TableName as ClassRef.$servertablename
  Delete class {[TableName]}
  Do $files.$add(TableName)

  Do ClassRef.$objs.$makelist($ref.$name,$ref.$coltype,
     $ref.$colsubtype,$ref.$colsublen,$ref.$length) Returns ListSchema
  Set current list ListSchema
  Redefine list {Name,Type,Subtype,Length}
  For each line in list from 1 to #LN step 1
    Load from list
    Do $files.[TableName].$objs.$add(Name,Type,Subtype,Length) Returns Ref
    Calculate Ref.$nonull as kFalse
    Calculate Ref.$isindexed as kFalse
    Calculate Ref.$initnull as kTrue
  End For

  Open data file (Do not close other data) {[pDataFilePath],TEMPDATAFILE}
  Set current data file {TEMPDATAFILE}
  Test for only one user
  If flag true
    Set main file {[TableName]}
    Update data dictionary {[TableName]}
  End If
  Close data file {TEMPDATAFILE}
  Delete class {[TableName]}
End If

Ideally, the code ought to be placed in a separate library, as the name of the table, consequently the name of the temporary file class created, may not conflict with any already existing class with the same name. The schema also needs to have it's $external property set to kTrue so that it can be referenced from another library.