ExtensionClass ZODB Migration Script
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))
