Changing DB Structures

What exactly happens if I add a new (e.g. +Aux) relation? Will the DB engine recognize that it's new and create the index to include all concerned objects?

When the DB model (the "er.l" file) is changed, it is not automatically detected at runtime. This would be difficult, as it might require a scan of the whole database. Also, a program is allowed to create arbitrary data, possibly outside the DB model, so such an automatism would be dangerous.

The programmer must understand the implications of a change, and run an explicit rebuild-script. Adding or changing an index (like adding an +Aux relation) is one of the most typical cases.

If I recall correctly it will simply work from now on but existing objects won't be included in the index.

Yes, the affected objects may not be found in searches. But fear thee not, for there are tools to rebuild the DB! You can use 'rebuild' (in "lib/too.l").

Sometimes, such modifications can be rather complex, and involve changes in the structures or types of objects. I strongly recommend testing it first on a local copy of the database.

It is tricky if not only an index, but the type of indexed data changes. For example, I had the case last week that the "Land" property of several object classes changed from a plain string

   (+Ref +String)

to an entity

   (+Ref +Link) NIL (+Land))



Simply changing the string in all objects to a '+Link' does not work, because the new index has a different structure. Therefore, removing the old string must happen in the old model, while setting the +Link to the land object must happen in the new model. I achieved this by switching the class of that relation daemon with 'bind' during each change:

   (for (This ...)   # Iterate all involved objects
      (let Str (: lnd)  # Keep the string value
         (bind  # Temporarily set old relation classes
            (list
               (cons (get '+Cls1 'lnd) '(+Need +Sn +Idx +String))
               (cons (get '+Cls2 'lnd) '(+Ref +String))
               ... )
            (put> This 'lnd NIL) )   # Clear old value and index
         (put> This 'lnd  # Set new (entity) value and build index
            (request '(+Land) ... Str ..) ) ) )



This is just an example how complex such changes can be. But, on the other hand, there is almost no limit on what can be done.

https://picolisp.com/wiki/?changingdb

19mar16   erik