# HG changeset patch # User Aurelien Campeas # Date 1363009569 -3600 # Node ID ab0cd076507635e36879c3a157feca5c5eb389f0 # Parent b70295aaed9de222058d1848f29b86d470751ad1 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643) The previous version assumed META_RTYPES provide relation definitions for all entity types. diff -r b70295aaed9d -r ab0cd0765076 hooks/syncschema.py --- a/hooks/syncschema.py Tue Jan 29 16:57:44 2013 +0100 +++ b/hooks/syncschema.py Mon Mar 11 14:46:09 2013 +0100 @@ -244,7 +244,7 @@ * create the necessary table * set creation_date and modification_date by creating the necessary CWAttribute entities - * add owned_by relation by creating the necessary CWRelation entity + * add relation by creating the necessary CWRelation entity """ entity = None # make pylint happy @@ -270,9 +270,16 @@ except KeyError: self.critical('rtype %s was not handled at cwetype creation time', rtype) continue + if not rschema.rdefs: + self.warning('rtype %s has no relation definition yet', rtype) + continue sampletype = rschema.subjects()[0] desttype = rschema.objects()[0] - rdef = copy(rschema.rdef(sampletype, desttype)) + try: + rdef = copy(rschema.rdef(sampletype, desttype)) + except KeyError: + # this combo does not exist because this is not a universal META_RTYPE + continue rdef.subject = _MockEntity(eid=entity.eid) mock = _MockEntity(eid=None) ss.execschemarql(session.execute, mock, ss.rdef2rql(rdef, cmap, gmap)) diff -r b70295aaed9d -r ab0cd0765076 hooks/test/unittest_syncschema.py --- a/hooks/test/unittest_syncschema.py Tue Jan 29 16:57:44 2013 +0100 +++ b/hooks/test/unittest_syncschema.py Mon Mar 11 14:46:09 2013 +0100 @@ -20,10 +20,12 @@ from logilab.common.testlib import TestCase, unittest_main from cubicweb import ValidationError +from cubicweb.schema import META_RTYPES from cubicweb.devtools.testlib import CubicWebTC from cubicweb.server.sqlutils import SQL_PREFIX from cubicweb.devtools.repotest import schema_eids_idx, restore_schema_eids_idx + def tearDownModule(*args): del SchemaModificationHooksTC.schema_eids @@ -116,6 +118,33 @@ self.assertFalse(schema.has_entity('concerne2')) self.assertFalse('concerne2' in schema['CWUser'].subject_relations()) + def test_metartype_with_nordefs(self): + META_RTYPES.add('custom_meta') + self.execute('INSERT CWRType X: X name "custom_meta", X description "", ' + 'X final FALSE, X symmetric FALSE') + self.commit() + eeid = self.execute('INSERT CWEType X: X name "NEWEtype", ' + 'X description "", X final FALSE')[0][0] + self._set_perms(eeid) + self.commit() + META_RTYPES.remove('custom_meta') + + def test_metartype_with_somerdefs(self): + META_RTYPES.add('custom_meta') + self.execute('INSERT CWRType X: X name "custom_meta", X description "", ' + 'X final FALSE, X symmetric FALSE') + self.commit() + rdefeid = self.execute('INSERT CWRelation X: X cardinality "**", X relation_type RT, ' + ' X from_entity E, X to_entity E ' + 'WHERE RT name "custom_meta", E name "CWUser"')[0][0] + self._set_perms(rdefeid) + self.commit() + eeid = self.execute('INSERT CWEType X: X name "NEWEtype", ' + 'X description "", X final FALSE')[0][0] + self._set_perms(eeid) + self.commit() + META_RTYPES.remove('custom_meta') + def test_is_instance_of_insertions(self): seid = self.execute('INSERT Transition T: T name "subdiv"')[0][0] is_etypes = [etype for etype, in self.execute('Any ETN WHERE X eid %s, X is ET, ET name ETN' % seid)]