hooks/syncschema.py
changeset 9950 3e5addc72575
parent 9949 e0f1c5697221
child 9960 6359f3121f3f
equal deleted inserted replaced
9949:e0f1c5697221 9950:3e5addc72575
    25 
    25 
    26 __docformat__ = "restructuredtext en"
    26 __docformat__ = "restructuredtext en"
    27 _ = unicode
    27 _ = unicode
    28 
    28 
    29 from copy import copy
    29 from copy import copy
    30 from yams.schema import BASE_TYPES, RelationSchema, RelationDefinitionSchema
    30 from yams.schema import (BASE_TYPES, BadSchemaDefinition,
       
    31                          RelationSchema, RelationDefinitionSchema)
    31 from yams import buildobjs as ybo, schema2sql as y2sql, convert_default_value
    32 from yams import buildobjs as ybo, schema2sql as y2sql, convert_default_value
    32 
    33 
    33 from logilab.common.decorators import clear_cache
    34 from logilab.common.decorators import clear_cache
    34 
    35 
    35 from cubicweb import validation_error
    36 from cubicweb import validation_error
   405         rdefdef = self.rdefdef = ybo.RelationDefinition(
   406         rdefdef = self.rdefdef = ybo.RelationDefinition(
   406             str(fromentity.name), entity.rtype.name, str(entity.otype.name),
   407             str(fromentity.name), entity.rtype.name, str(entity.otype.name),
   407             description=entity.description, cardinality=entity.cardinality,
   408             description=entity.description, cardinality=entity.cardinality,
   408             constraints=get_constraints(self.cnx, entity),
   409             constraints=get_constraints(self.cnx, entity),
   409             order=entity.ordernum, eid=entity.eid, **kwargs)
   410             order=entity.ordernum, eid=entity.eid, **kwargs)
   410         self.cnx.vreg.schema.add_relation_def(rdefdef)
   411         try:
       
   412             self.cnx.vreg.schema.add_relation_def(rdefdef)
       
   413         except BadSchemaDefinition:
       
   414             # rdef has been infered then explicitly added (current consensus is
       
   415             # not clear at all versus infered relation handling (and much
       
   416             # probably buggy)
       
   417             rdef = self.cnx.vreg.schema.rschema(rdefdef.name).rdefs[rdefdef.subject, rdefdef.object]
       
   418             assert rdef.infered
   411         self.cnx.execute('SET X ordernum Y+1 '
   419         self.cnx.execute('SET X ordernum Y+1 '
   412                              'WHERE X from_entity SE, SE eid %(se)s, X ordernum Y, '
   420                          'WHERE X from_entity SE, SE eid %(se)s, X ordernum Y, '
   413                              'X ordernum >= %(order)s, NOT X eid %(x)s',
   421                          'X ordernum >= %(order)s, NOT X eid %(x)s',
   414                              {'x': entity.eid, 'se': fromentity.eid,
   422                          {'x': entity.eid, 'se': fromentity.eid,
   415                               'order': entity.ordernum or 0})
   423                           'order': entity.ordernum or 0})
   416         return rdefdef
   424         return rdefdef
   417 
   425 
   418     def precommit_event(self):
   426     def precommit_event(self):
   419         cnx = self.cnx
   427         cnx = self.cnx
   420         entity = self.entity
   428         entity = self.entity