Skip to content

Instantly share code, notes, and snippets.

@do3cc
Last active November 25, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save do3cc/3ae985653b23ac63be0a to your computer and use it in GitHub Desktop.
Save do3cc/3ae985653b23ac63be0a to your computer and use it in GitHub Desktop.
Hacky script to try to repair your database after problems with interfaces and the relation catalog
# It was hard to figure out, it might now be appropriate to use in your case.
# If you are not feeling comfortable to work on your database like this,
# don't run this gist.
from BTrees.OIBTree import OITreeSet
from ZODB.broken import Broken
from zc.relation.interfaces import ICatalog
from zope.component import getUtility
import transaction
cat = getUtility(ICatalog)
def is_broken(obj):
if not obj:
return False
try:
iterator = iter(obj)
except TypeError:
iterator = [obj]
for item in iterator:
try:
if issubclass(item, Broken):
return True
except TypeError:
continue
return False
for key, value in cat._reltoken_name_TO_objtokenset.items():
if not is_broken(value):
# This is here because I was stupid once 6 lines later. Sorry.
if isinstance(value, list):
cat._reltoken_name_TO_objtokenset[key] = OITreeSet(value)
continue
if not isinstance(value, OITreeSet):
continue
cat._reltoken_name_TO_objtokenset[key] = OITreeSet(filter(lambda x:not is_broken(x), value))
for name in ('from_interfaces_flattened', 'to_interfaces_flattened'):
for key, (ignore, intids) in cat._name_TO_mapping[name].items():
if is_broken(key):
cat._name_TO_mapping.pop(key)
for intid in intids:
# I am not sure if it is an error that this can occur. Sorry.
if (intid, name) not in cat._reltoken_name_TO_objtokenset:
cat._reltoken_name_TO_objtokenset[(intid, name)] = OITreeSet([key])
if key not in cat._reltoken_name_TO_objtokenset[(intid, name)]:
cat._reltoken_name_TO_objtokenset[(intid, name)].add(key)
if not transaction.get()._resources[0]._registered_objects:
print "Too bad, nothing changed"
# transaction.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment