[hooks/syncschema] do not crash when adding a new entity type (closes #2741643) stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 11 Mar 2013 14:46:09 +0100
branchstable
changeset 8715 ab0cd0765076
parent 8714 b70295aaed9d
child 8716 dd53d8d63b4b
[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.
hooks/syncschema.py
hooks/test/unittest_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 <meta rtype> 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))
--- 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)]