[migration] test if entity type has been in the deleted in the transaction should protect both clauses
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 12 Feb 2016 10:39:24 +0100
changeset 11106 012e6c7d02ef
parent 11105 2a8884f3be3d
child 11107 df1f2d853d40
[migration] test if entity type has been in the deleted in the transaction should protect both clauses else we may attempt to execute the UPDATE query on a table which has been deleted. Closes #10692374
hooks/syncschema.py
server/test/data-migractions/migratedapp/schema.py
server/test/unittest_migractions.py
--- a/hooks/syncschema.py	Fri Feb 12 10:39:14 2016 +0100
+++ b/hooks/syncschema.py	Fri Feb 12 10:39:24 2016 +0100
@@ -606,21 +606,22 @@
         # relations, but only if it's the last instance for this relation type
         # for other relations
         if (rschema.final or rschema.inlined):
-            rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R, '
-                           'R eid %%(r)s, X from_entity E, E eid %%(e)s'
-                           % rdeftype,
-                           {'r': rschema.eid, 'e': rdef.subject.eid})
-            if rset[0][0] == 0 and not cnx.deleted_in_transaction(rdef.subject.eid):
-                ptypes = cnx.transaction_data.setdefault('pendingrtypes', set())
-                ptypes.add(rschema.type)
-                DropColumn.get_instance(cnx).add_data((str(rdef.subject), str(rschema)))
-            elif rschema.inlined:
-                cnx.system_sql('UPDATE %s%s SET %s%s=NULL WHERE '
-                               'EXISTS(SELECT 1 FROM entities '
-                               '       WHERE eid=%s%s AND type=%%(to_etype)s)'
-                               % (SQL_PREFIX, rdef.subject, SQL_PREFIX, rdef.rtype,
-                                  SQL_PREFIX, rdef.rtype),
-                               {'to_etype': rdef.object.type})
+            if not cnx.deleted_in_transaction(rdef.subject.eid):
+                rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R, '
+                               'R eid %%(r)s, X from_entity E, E eid %%(e)s'
+                               % rdeftype,
+                               {'r': rschema.eid, 'e': rdef.subject.eid})
+                if rset[0][0] == 0:
+                    ptypes = cnx.transaction_data.setdefault('pendingrtypes', set())
+                    ptypes.add(rschema.type)
+                    DropColumn.get_instance(cnx).add_data((str(rdef.subject), str(rschema)))
+                elif rschema.inlined:
+                    cnx.system_sql('UPDATE %s%s SET %s%s=NULL WHERE '
+                                   'EXISTS(SELECT 1 FROM entities '
+                                   '       WHERE eid=%s%s AND type=%%(to_etype)s)'
+                                   % (SQL_PREFIX, rdef.subject, SQL_PREFIX, rdef.rtype,
+                                      SQL_PREFIX, rdef.rtype),
+                                   {'to_etype': rdef.object.type})
         elif lastrel:
             DropRelationTable(cnx, str(rschema))
         else:
--- a/server/test/data-migractions/migratedapp/schema.py	Fri Feb 12 10:39:14 2016 +0100
+++ b/server/test/data-migractions/migratedapp/schema.py	Fri Feb 12 10:39:24 2016 +0100
@@ -211,3 +211,8 @@
 class same_as(RelationDefinition):
     subject = ('Societe',)
     object = 'ExternalUri'
+
+class inlined_rel(RelationDefinition):
+    subject = object = 'Folder2'
+    inlined = True
+    cardinality = '??'
--- a/server/test/unittest_migractions.py	Fri Feb 12 10:39:14 2016 +0100
+++ b/server/test/unittest_migractions.py	Fri Feb 12 10:39:24 2016 +0100
@@ -276,7 +276,7 @@
                                'description', 'description_format',
                                'eid',
                                'filed_under2', 'has_text',
-                               'identity', 'in_basket', 'is', 'is_instance_of',
+                               'identity', 'in_basket', 'inlined_rel', 'is', 'is_instance_of',
                                'modification_date', 'name', 'owned_by'])
             self.assertCountEqual([str(rs) for rs in self.schema['Folder2'].object_relations()],
                                   ['filed_under2', 'identity'])