[repo] closes #1821172: fix source deletion
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 12 Jul 2011 11:27:42 +0200
changeset 7650 278fe9c1f3ad
parent 7649 ede740bd7077
child 7651 7c0af7ef3325
[repo] closes #1821172: fix source deletion
dbapi.py
hooks/syncsources.py
misc/migration/3.12.9_Any.py
server/sources/pyrorql.py
server/test/unittest_datafeed.py
server/test/unittest_multisources.py
--- a/dbapi.py	Tue Jul 12 10:36:22 2011 +0200
+++ b/dbapi.py	Tue Jul 12 11:27:42 2011 +0200
@@ -677,11 +677,10 @@
     @check_not_closed
     def describe(self, eid, asdict=False):
         metas = self._repo.describe(self.sessionid, eid, **self._txid())
+        if len(metas) == 3: # backward compat
+            metas = list(metas)
+            metas.append(metas[1])
         if asdict:
-            if len(metas) == 3:
-                d = dict(zip(('type', 'source', 'extid'), metas))
-                d['asource'] = d['source']
-                return d
             return dict(zip(('type', 'source', 'extid', 'asource'), metas))
         # XXX :-1 for cw compat, use asdict=True for full information
         return metas[:-1]
--- a/hooks/syncsources.py	Tue Jul 12 10:36:22 2011 +0200
+++ b/hooks/syncsources.py	Tue Jul 12 11:27:42 2011 +0200
@@ -53,7 +53,7 @@
 
 
 class SourceRemovedOp(hook.Operation):
-    def precommit_event(self):
+    def postcommit_event(self):
         self.session.repo.remove_source(self.uri)
 
 class SourceRemovedHook(SourceHook):
@@ -71,8 +71,9 @@
     def precommit_event(self):
         self.__processed = []
         for source in self.get_data():
-            conf = source.repo_source.check_config(source)
-            self.__processed.append( (source, conf) )
+            if not self.session.deleted_in_transaction(source.eid):
+                conf = source.repo_source.check_config(source)
+                self.__processed.append( (source, conf) )
 
     def postcommit_event(self):
         for source, conf in self.__processed:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/migration/3.12.9_Any.py	Tue Jul 12 11:27:42 2011 +0200
@@ -0,0 +1,1 @@
+sync_schema_props_perms('cw_source')
--- a/server/sources/pyrorql.py	Tue Jul 12 10:36:22 2011 +0200
+++ b/server/sources/pyrorql.py	Tue Jul 12 11:27:42 2011 +0200
@@ -443,6 +443,10 @@
 
     def delete_entity(self, session, entity):
         """delete an entity from the source"""
+        if session.deleted_in_transaction (self.eid):
+            # source is being deleted, don't propagate
+            self._query_cache.clear()
+            return
         cu = session.cnxset[self.uri]
         cu.execute('DELETE %s X WHERE X eid %%(x)s' % entity.__regid__,
                    {'x': self.eid2extid(entity.eid, session)})
@@ -460,6 +464,10 @@
 
     def delete_relation(self, session, subject, rtype, object):
         """delete a relation from the source"""
+        if session.deleted_in_transaction (self.eid):
+            # source is being deleted, don't propagate
+            self._query_cache.clear()
+            return
         cu = session.cnxset[self.uri]
         cu.execute('DELETE X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype,
                    {'x': self.eid2extid(subject, session),
--- a/server/test/unittest_datafeed.py	Tue Jul 12 10:36:22 2011 +0200
+++ b/server/test/unittest_datafeed.py	Tue Jul 12 11:27:42 2011 +0200
@@ -94,6 +94,14 @@
         self.assertTrue(dfsource.latest_retrieval)
         self.assertTrue(dfsource.fresh())
 
+        # test_delete_source
+        req = self.request()
+        with self.debugged('DBG_RQL'):
+            req.execute('DELETE CWSource S WHERE S name "myfeed"')
+            self.commit()
+        self.failIf(self.execute('Card X WHERE X title "cubicweb.org"'))
+        self.failIf(self.execute('Any X WHERE X has_text "cubicweb.org"'))
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()
--- a/server/test/unittest_multisources.py	Tue Jul 12 10:36:22 2011 +0200
+++ b/server/test/unittest_multisources.py	Tue Jul 12 11:27:42 2011 +0200
@@ -381,6 +381,13 @@
     def test_nonregr3(self):
         self.sexecute('DELETE Card X WHERE X eid %(x)s, NOT X multisource_inlined_rel Y', {'x': self.ic1})
 
+    def test_delete_source(self):
+        req = self.request()
+        req.execute('DELETE CWSource S WHERE S name "extern"')
+        self.commit()
+        cu = self.session.system_sql("SELECT * FROM entities WHERE source='extern'")
+        self.failIf(cu.fetchall())
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()