[Zope] Upgrading a product

Michel Pelletier michel@digicool.com
Wed, 15 Dec 1999 15:53:38 -0500


> -----Original Message-----
> From: rgines@purina.com [mailto:rgines@purina.com]
> Sent: Wednesday, December 15, 1999 1:03 PM
> To: Michel Pelletier
> Cc: zope@zope.org
> Subject: RE: [Zope] Upgrading a product
> 
> 
> >Good Question!  The answer is with __setstate__.
> 
> Hmmm.   This just might work.   Thanks.  I'm assuming then that this
> 'upgradablility' must be designed into the 'version 1' of the product
> in order to let it essentially be self upgrading?

It's allways there in every persistent object, it's defined by the
Persistence class.  If you have special behavior your want to add to
that, you can override it.

> Are you aware of any working examples of this in action?

Yes, the Catalog in 2.1 upgraded itself from the Catalog in 2.0:

    def __setstate__(self, state):
        """ initialize your brains.  This method is called when the
        catalog is first activated (from the persistent storage) """
        Persistent.__setstate__(self, state)
        self.useBrains(self._v_brains)
        if not hasattr(self, 'lexicon'):
            self.lexicon = Lexicon()

The Persistent setstate must be called, then you are free to mess with
the object as you please.  In this case, the brains get initialized
(because they are not persistent objects and must be re initialized
every time the catalog comes into memory) and we sniff for a 'lexicon'
attribute, which, if there isn't one, is assigned a new lexicon object.
These last two lines are the 'upgrading'.

-Michel