[Zope-dev] Product upgrades

Max M maxm@mxm.dk
Wed, 23 Jan 2002 09:10:25 +0000


brian.r.brinegar.1 wrote:

things created with the first constructor must be removed and recreated if
'horse' is to be added. Is there a way to go through and update old
instances of a product to a newer version? Or should backwards
compatibility be designed into the product?

Generally Zope uses pickle to save the objects. And pickel will not save 
the methods, nor the class attributes::

class Person:

    meta_type = 'person'

    def __init__(self):
        self.name =  'Max Rasmussen'

    def hello(self, text):
        print 'hello %s' % text

In this class '__init__()', 'hello()' and 'meta_type' will be 
automatically updated whenever you update you class, as they are shared 
globally.

But the 'self.name' attribute is part if the object instance, not the 
class. So it will be saved in the ZODB. If you then want to change you 
object so that it has firstName and lastName attributes you must do it 
manually.

Write an external script that walks through the objects one by one and 
then changes the attributes::


def updatePersons(self):
    # the ZopeFind() methods finds all of the objects with a given metatype
    # or other attributes.
    # It is defined in <zopesite>\lib\python\OFS\FindSupport.py
    persons = self.ZopeFind(self, obj_metatypes=['person'], search_sub=1)
    for person in persons:
        person.firstName = ''
        person.lastName  = ''
        del(person.name)


def updatePersons(self):
    # Or if you want to use the name value to create first and last name 
from
    from string import rfind
    persons = self.ZopeFind(self, obj_metatypes=['person'], search_sub=1)
    for person in persons:
        lastSpace = rfind(person.name, ' ')
        fName, lName = person.name[:lastSpace], person.name[lastSpace+1:]
        person.firstName = fName
        person.lastName  = lName
        del(person.name)
        
So as you can see, it is really hard to make a standard way of updating 
a product. But it is actually not hard to write a method to do it for you.

But one thing that I wonder about is how do the rest of you make shure 
that you old products are updated? Shouldn't there be some kind of hook 
in Zope that calls an update method if it was present, and the version 
has changed?

regards Max M