[Grok-dev] Grok schema migrations (was Re: Getting the application in views and viewlets)

Kevin Smith kevin at mcweekly.com
Fri Jun 20 20:32:11 EDT 2008

Hey Kevin,

I cargo-culted this one awhile back, it goes something like this....


from generations import schemaManager
from zope.app.generations.interfaces import ISchemaManager

#register schema_manager for generations of this content
grok.global_utility(schemaManager, provides=ISchemaManager,
                    name=u'generations')  # name may be optional


from zope.app.generations.generations import SchemaManager

pkg = 'myproject.generations'

def schemaManager(*args, **kw):
    return SchemaManager(minimum_generation=1,
# can't remember if you need to start with generation 0 or 1 whatever 
the number  is, it is a convention and will generations look for the 
module "evolve%s.py"  % SchemaManager.generation as defined above

#so in this example evolve1.py is sought in the generations package
#this is tied to the preceding generation number
from zope.app.zopeappgenerations import getRootFolder
from myproject.app import MyModel

def evolve(context):
    """ Upgrade schemas
    root_folder = getRootFolder(context)
    from zope.app.generations.utility import findObjectsMatching
    for myobj in findObjectsMatching(root_folder,
                                     lambda ob:isinstance(ob, MyModel )):
        myobj.myattr = True

# you'll definately want to have a look at findObjectsMatching, it makes 
it easy to iterate through the entire zodb

Somone correct me if I'm wrong, but it's possible that grok may not 
define the utility early enough, if that is the case you may need to use 



Kevin Smith

Kevin Teague wrote:
>> Well, it's content upgrades in general, and yes, it would be possible 
>> to build a nicer solution that what's available right now with 
>> zope.generations. It's just it needs some time investment of a few 
>> people to design it and implement it. It'd make a good sprint topic too.
> I am trying to get zope.app.generations working in a Grok app ATM. Has 
> anyone done this? Anyone have any example code? (I'm finding the 
> zope.app.generations docs a bit tough to get started with). Any 
> comment on what kind of improvements could be made over the existing 
> generations package?
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev

More information about the Grok-dev mailing list