[OT] OODBs and schema, was Re: [linux-audio-user] Re: sound & midi applications page, was Re: [Consortium] linuxaudio.org

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: [OT] OODBs and schema, was Re: [linux-audio-user] Re: sound & midi applications page, was Re: [Consortium] linuxaudio.org
From: Paul Winkler (pw_lists_AT_slinkp.com)
Date: Mon Feb 02 2004 - 18:15:43 EET


On Mon, Feb 02, 2004 at 02:55:02PM +0000, Steve Harris wrote:
> On Mon, Feb 02, 2004 at 09:36:02 -0500, Paul Winkler wrote:
> > > and wether you are willing to change.
> >
> > I do web development with Zope. My site code never depends on
> > the data store :-)
>
> I'm not sure if thats a joke or not ;) Zope is based on an OODB IIRC.
> thier not very good at schema independence in my experience.

I'm not sure what "schema independence" means, so I will
assume it means the ability to extend or modify the
schema after you've already got live instances.

I'm curious what languages you've used with an OODB.
I imagine it would be a major pain if you didn't have as much
runtime flexibility as python (e.g. ruby would be just fine,
C would be insane, dunno about java).

It's been a non-issue in the 3 years I've been doing Zope.
Content types in zope are generally defined with classes.
Adding an attribute to a class requires me to add a
couple of lines to the class definition, including
one to provide a default class-level value, then I tell zope
to reload the class by clicking a button in the zope management
interface. Live instances immediately see the default
value. This requires no deep magic, it's just how python works.

An untested-but-should-work example (feel free to skip
the rest of this message):

from ZODB import Persistence

class SoftwareResource(Persistence.Persistent):

    meta_type='Software Resource'

    def __init__(self, name='', author='', download_url=''):
        self.name=name
        self.author=author

    def setAuthor(self, author):
        # setter methods are totally optional in python,
        # but we usually use them in zope so we can
        # declare security restrictions on them,
        # which I won't bother with in this example.
        self.author = author

    def setName(self, name):
        self.name = name

Now let's add a release_number attribute with a default
value of '':
 
class SoftwareResource(Persistence.Persistent):
    release_number = ''
    ...
    def __init__(self, name='', author='', release_number=''):
        ...
        self.release_number=release_number
    ...

    def setRelease(self, release_number):
        self.release_number=release_number

Reload the class and you're done.

Changing the 'author' attribute to 'author_name'
is only barely more difficult. You can embed self-upgrading
code in the class, but such code has a way of sticking around
long after it should be deleted. Another way to do it is with a
trivial upgrade script like this, to be run once after
reloading the relevant class changes:

# A bit like find . -type ...

for obj in context.ZopeFind(meta_type='Software Resource'):
    if not hasattr(obj, 'author_name'):
        obj.setAuthor(obj.author)
        del(obj.author)

-- 

Paul Winkler http://www.slinkp.com Look! Up in the sky! It's THE FISHY ANUS! (random hero from isometric.spaceninja.com)


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Mon Feb 02 2004 - 18:17:22 EET