[querier] Allow usage of computed relation into WHERE clause of write queries 3.25
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 03 Nov 2017 15:02:03 +0100
branch3.25
changeset 12228 8fa3131583ec
parent 12217 6ab1793f6f83
child 12246 b8567725c473
[querier] Allow usage of computed relation into WHERE clause of write queries This used to crash because of an attempt to access unexisting table. Closes #17113286
cubicweb/server/ssplanner.py
cubicweb/server/test/unittest_querier.py
--- a/cubicweb/server/ssplanner.py	Mon Oct 30 09:04:34 2017 +0100
+++ b/cubicweb/server/ssplanner.py	Fri Nov 03 15:02:03 2017 +0100
@@ -24,7 +24,7 @@
 
 from cubicweb import QueryError
 from cubicweb.schema import VIRTUAL_RTYPES
-from cubicweb.rqlrewrite import add_types_restriction
+from cubicweb.rqlrewrite import add_types_restriction, RQLRelationRewriter
 from cubicweb.server.edition import EditedEntity
 
 READ_ONLY_RTYPES = set(('eid', 'has_text', 'is', 'is_instance_of', 'identity'))
@@ -296,6 +296,9 @@
         union.append(select)
         select.clean_solutions(solutions)
         add_types_restriction(self.schema, select)
+        # Rewrite computed relations
+        rewriter = RQLRelationRewriter(plan.cnx)
+        rewriter.rewrite(union, plan.args)
         self.rqlhelper.annotate(union)
         return self.build_select_plan(plan, union)
 
--- a/cubicweb/server/test/unittest_querier.py	Mon Oct 30 09:04:34 2017 +0100
+++ b/cubicweb/server/test/unittest_querier.py	Fri Nov 03 15:02:03 2017 +0100
@@ -1399,6 +1399,16 @@
             self.assertEqual(len(rset.rows), 1)
             self.assertEqual(rset.description, [('CWUser',)])
 
+    # computed relation tests ##################################################
+
+    def test_computed_relation_write_queries(self):
+        """Ensure we can use computed relation in WHERE clause of write queries"""
+        with self.admin_access.cnx() as cnx:
+            cnx.execute('INSERT Personne P: P nom "user", P login_user U WHERE NOT U user_login P')
+            cnx.execute('DELETE P login_user U WHERE U user_login P')
+            cnx.execute('DELETE Personne P WHERE U user_login P')
+            cnx.execute('SET U login "people" WHERE U user_login P')
+
     # ZT datetime tests ########################################################
 
     def test_tz_datetime(self):