# HG changeset patch # User Julien Cristau # Date 1387470095 -3600 # Node ID 15c695d8d8654b1598989249b8bfbbacfd64e0fe # Parent 176c1edf51b0f3298f44da59e625259a7b77eb50 [migration/3.18] add sanity checks before changing symmetric relations Delete supposedly impossible relations from the db. Related to #3259713. diff -r 176c1edf51b0 -r 15c695d8d865 misc/migration/3.18.0_Any.py --- a/misc/migration/3.18.0_Any.py Thu Dec 19 17:09:16 2013 +0100 +++ b/misc/migration/3.18.0_Any.py Thu Dec 19 17:21:35 2013 +0100 @@ -86,6 +86,17 @@ for rschema in schema.relations(): if rschema.symmetric: + subjects = set(repr(e.type) for e in rschema.subjects()) + objects = set(repr(e.type) for e in rschema.objects()) + assert subjects == objects + martians = set(str(eid) for eid, in sql('SELECT eid_to FROM %s_relation, entities WHERE eid_to = eid AND type NOT IN (%s)' % + (rschema.type, ','.join(subjects)))) + martians |= set(str(eid) for eid, in sql('SELECT eid_from FROM %s_relation, entities WHERE eid_from = eid AND type NOT IN (%s)' % + (rschema.type, ','.join(subjects)))) + if martians: + martians = ','.join(martians) + print 'deleting broken relations %s for eids %s' % (rschema.type, martians) + sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians)) with session.deny_all_hooks_but(): rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type}) commit()