[hooks] Delete some properties cached on entities schema on schema updates 3.24
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 08 Nov 2016 18:34:22 +0100
branch3.24
changeset 11807 9d478b81f6d7
parent 11806 5fdc92be3ba7
child 11808 4a387179fa33
[hooks] Delete some properties cached on entities schema on schema updates cubicweb.schema add two additional @cachedproperty that should be cleared when in-memory schema is modified. This may leads to e.g. attempt to delete some already dropped relations. Related to #16130960
cubicweb/hooks/syncschema.py
cubicweb/hooks/test/unittest_syncschema.py
--- a/cubicweb/hooks/syncschema.py	Sun Nov 06 16:43:27 2016 +0100
+++ b/cubicweb/hooks/syncschema.py	Tue Nov 08 18:34:22 2016 +0100
@@ -201,6 +201,13 @@
         for eschema in self.cnx.repo.schema.entities():
             if not eschema.final:
                 clear_cache(eschema, 'ordered_relations')
+                # clear additional cached properties
+                try:
+                    # is_composite use composite_rdef_roles, hence one try except should be enough
+                    del eschema.composite_rdef_roles
+                    del eschema.is_composite
+                except AttributeError:
+                    pass
 
     def postcommit_event(self):
         repo = self.cnx.repo
--- a/cubicweb/hooks/test/unittest_syncschema.py	Sun Nov 06 16:43:27 2016 +0100
+++ b/cubicweb/hooks/test/unittest_syncschema.py	Tue Nov 08 18:34:22 2016 +0100
@@ -400,6 +400,23 @@
             # comparison on Python 2.
             self.assertEqual(set(rdef.constraints), set([cstr, cstr3]))
 
+    def test_eschema_composite_properties(self):
+        with self.admin_access.repo_cnx() as cnx:
+            part_eschema = self.schema['EmailPart']
+            email_eschema = self.schema['Email']
+            parts_rdef = email_eschema.rdef('parts')
+            self.assertEqual(part_eschema.composite_rdef_roles, [])
+            self.assertEqual(part_eschema.is_composite, False)
+            self.assertEqual(email_eschema.composite_rdef_roles,
+                             [(parts_rdef, 'subject')])
+            self.assertEqual(email_eschema.is_composite, True)
+            cnx.execute('DELETE CWRType X WHERE X name "parts"')
+            cnx.commit()
+            self.assertEqual(part_eschema.composite_rdef_roles, [])
+            self.assertEqual(part_eschema.is_composite, False)
+            self.assertEqual(email_eschema.composite_rdef_roles, [])
+            self.assertEqual(email_eschema.is_composite, False)
+
 
 if __name__ == '__main__':
     import unittest