[migration] fix addition of attribute to a parent class stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 30 Sep 2009 16:05:43 +0200
branchstable
changeset 3526 dfb2ebb765e2
parent 3525 2dc3908f667f
child 3527 efeb16ff93f3
[migration] fix addition of attribute to a parent class
server/schemahooks.py
server/test/unittest_hooks.py
--- a/server/schemahooks.py	Tue Sep 29 15:10:12 2009 -0700
+++ b/server/schemahooks.py	Wed Sep 30 16:05:43 2009 +0200
@@ -307,10 +307,11 @@
         default = entity.defaultval
         if default is not None:
             default = TYPE_CONVERTER[entity.otype.name](default)
-        rdef = self.init_rdef(default=default,
-                              indexed=entity.indexed,
-                              fulltextindexed=entity.fulltextindexed,
-                              internationalizable=entity.internationalizable)
+        props = {'default': default,
+                 'indexed': entity.indexed,
+                 'fulltextindexed': entity.fulltextindexed,
+                 'internationalizable': entity.internationalizable}
+        rdef = self.init_rdef(**props)
         sysource = session.pool.source('system')
         attrtype = type_from_constraints(sysource.dbhelper, rdef.object,
                                          rdef.constraints)
@@ -343,6 +344,20 @@
             except Exception, ex:
                 self.error('error while creating index for %s.%s: %s',
                            table, column, ex)
+        # final relations are not infered, propagate
+        rschema = self.schema.rschema(rdef.name)
+        eschema = self.schema.eschema(rdef.subject)
+        props.update({'constraints': rdef.constraints,
+                      'description': rdef.description,
+                      'cardinality': rdef.cardinality,
+                      'constraints': rdef.constraints,
+                      'order': rdef.order})
+        for specialization in eschema.specialized_by(False):
+            if rschema.has_rdef(specialization, rdef.object):
+                continue
+            for rql, args in ss.frdef2rql(rschema, str(specialization),
+                                          rdef.object, props):
+                session.execute(rql, args)
 
 
 class SourceDbCWRelationAdd(SourceDbCWAttributeAdd):
--- a/server/test/unittest_hooks.py	Tue Sep 29 15:10:12 2009 -0700
+++ b/server/test/unittest_hooks.py	Wed Sep 30 16:05:43 2009 +0200
@@ -502,5 +502,15 @@
                      'RT name "prenom", E name "Personne"')
         self.commit()
 
+
+    def test_add_attribute_to_base_class(self):
+        self.execute('INSERT CWAttribute X: X cardinality "11", X defaultval "noname", X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F '
+                     'WHERE RT name "nom", E name "BaseTransition", F name "String"')
+        self.commit()
+        self.schema.rebuild_infered_relations()
+        self.failUnless('Transition' in self.schema['nom'].subjects())
+        self.failUnless('WorkflowTransition' in self.schema['nom'].subjects())
+        self.execute('Any X WHERE X is_instance_of BaseTransition, X nom "hop"')
+
 if __name__ == '__main__':
     unittest_main()