[hooks] don't insert an owned_by relation for deleted entities
authorJulien Cristau <julien.cristau@logilab.fr>
Thu, 15 Jan 2015 15:45:09 +0100
changeset 10182 116b24efad0e
parent 10181 215403b9798f
child 10183 ff93dad2ae3a
[hooks] don't insert an owned_by relation for deleted entities If, in the same transaction, a composed entity is created and deleted, we'll have scheduled a SyncOwnersOp which would end up creating an owned_by relation for the deleted entity. Closes #4846883
hooks/metadata.py
hooks/test/unittest_hooks.py
--- a/hooks/metadata.py	Thu Jan 29 16:18:23 2015 +0100
+++ b/hooks/metadata.py	Thu Jan 15 15:45:09 2015 +0100
@@ -93,6 +93,10 @@
 class SyncOwnersOp(hook.DataOperationMixIn, hook.Operation):
     def precommit_event(self):
         for compositeeid, composedeid in self.get_data():
+            if self.cnx.deleted_in_transaction(compositeeid):
+                continue
+            if self.cnx.deleted_in_transaction(composedeid):
+                continue
             self.cnx.execute('SET X owned_by U WHERE C owned_by U, C eid %(c)s,'
                                  'NOT EXISTS(X owned_by U, X eid %(x)s)',
                                  {'c': compositeeid, 'x': composedeid})
--- a/hooks/test/unittest_hooks.py	Thu Jan 29 16:18:23 2015 +0100
+++ b/hooks/test/unittest_hooks.py	Thu Jan 15 15:45:09 2015 +0100
@@ -184,6 +184,17 @@
                                            'U login "toto", X address A')[0][0],
                               'toto@logilab.fr')
 
+    def test_user_composite_no_owner_on_deleted_entity(self):
+        with self.admin_access.repo_cnx() as cnx:
+            u = self.create_user(cnx, 'toto').eid
+            cnx.commit()
+            e = cnx.create_entity('EmailAddress', address=u'toto@logilab.fr', reverse_use_email=u)
+            e.cw_delete()
+            cnx.commit()
+            self.assertFalse(cnx.system_sql(
+                'SELECT * FROM owned_by_relation '
+                'WHERE eid_from NOT IN (SELECT eid FROM entities)').fetchall())
+
     def test_no_created_by_on_deleted_entity(self):
         with self.admin_access.repo_cnx() as cnx:
             eid = cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0]