Skip to content. | Skip to navigation

Personal tools

Navigation

You are here: Home / Blog / ExtensionClass ZODB Migration Script

ExtensionClass ZODB Migration Script

by Ross Patterson last modified Apr 03, 2010 12:38 AM
A "bin/instance run" script for updating ExtensionClass instances pickles in the ZODB. Useful to apply Hanno's pickle patch.

Python Source icon extensionclass_update.py — Python Source, 1 KB (1451 bytes)

File contents

import transaction
import ExtensionClass
from ZODB.utils import p64
from ZODB.serialize import referencesf
from logging import warn

referencesf  # pyflakes

from OFS.Uninstalled import BrokenClass

conn = app._p_jar
db = conn.db()
storage = db._storage
if hasattr(db, 'references'):
    referencesf = db.references
max_oid = len(conn._storage)
updated = 0
total = 0

BATCH = 1000

ext = set()
not_ext = set()

for oid in xrange(0, max_oid + 1):
    poid = p64(oid)
    try:
        data, serial = storage.load(poid, '')
    except KeyError:
        continue
    total += 1
    refs = referencesf(data)
    for ref in refs:
        if ref in not_ext:
            continue
        if not ref in ext:
            robj = conn[ref]
            if isinstance(robj, BrokenClass):
                break
            is_ext = isinstance(robj, ExtensionClass.Base)
            robj._p_deactivate()
            if not is_ext:
                not_ext.add(ref)
                continue
            ext.add(ref)
    else:
        obj = conn[poid]
        obj._p_changed = 1
        updated += 1
        if isinstance(obj, ExtensionClass.Base):
            ext.add(poid)
        else:
            not_ext.add(poid)
    if updated % BATCH == 0:
        transaction.commit()
        warn('Updated %s (%s/%s)' % (updated, oid, max_oid))

transaction.commit()
warn('Done. %s of %s updated.' % (updated, total))
Navigation
OpenID Login

Contact

me@rpatterson.net

IRC: zenwryly@irc.freenode.net
plone.org: rossp
GTalk: mer@patterson.net
Yahoo IM: patterson_ross
AIM: rosspatters
MSN: me@rpatterson.net
Skype: merpattersonnet

PO Box 7775 #10587
San Francisco, CA
94120-7775

Phone
+1 (415) 894-5323