hooks/syncschema.py
changeset 10445 f1773842077d
parent 10250 582301c2c8ca
child 10646 45671fb330f5
equal deleted inserted replaced
10444:fb7c1013189e 10445:f1773842077d
    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 hashlib import md5
    30 from yams.schema import (BASE_TYPES, BadSchemaDefinition,
    31 from yams.schema import (BASE_TYPES, BadSchemaDefinition,
    31                          RelationSchema, RelationDefinitionSchema)
    32                          RelationSchema, RelationDefinitionSchema)
    32 from yams import buildobjs as ybo, convert_default_value
    33 from yams import buildobjs as ybo, convert_default_value
    33 
    34 
    34 from logilab.common.decorators import clear_cache
    35 from logilab.common.decorators import clear_cache
   703             syssource.update_rdef_column(cnx, rdef)
   704             syssource.update_rdef_column(cnx, rdef)
   704             self.size_cstr_changed = True
   705             self.size_cstr_changed = True
   705         elif cstrtype == 'UniqueConstraint':
   706         elif cstrtype == 'UniqueConstraint':
   706             syssource.update_rdef_unique(cnx, rdef)
   707             syssource.update_rdef_unique(cnx, rdef)
   707             self.unique_changed = True
   708             self.unique_changed = True
       
   709         if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
       
   710             cstrname = 'cstr' + md5(rdef.subject.type + rdef.rtype.type + cstrtype +
       
   711                                     (self.oldcstr.serialize() or '')).hexdigest()
       
   712             cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' % (SQL_PREFIX, rdef.subject.type, cstrname))
   708 
   713 
   709     def revertprecommit_event(self):
   714     def revertprecommit_event(self):
   710         # revert changes on in memory schema
   715         # revert changes on in memory schema
   711         if self.newcstr is not None:
   716         if self.newcstr is not None:
   712             self.rdef.constraints.remove(self.newcstr)
   717             self.rdef.constraints.remove(self.newcstr)
   750             syssource.update_rdef_column(cnx, rdef)
   755             syssource.update_rdef_column(cnx, rdef)
   751             self.size_cstr_changed = True
   756             self.size_cstr_changed = True
   752         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   757         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   753             syssource.update_rdef_unique(cnx, rdef)
   758             syssource.update_rdef_unique(cnx, rdef)
   754             self.unique_changed = True
   759             self.unique_changed = True
       
   760         if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
       
   761             if oldcstr is not None:
       
   762                 oldcstrname = 'cstr' + md5(rdef.subject.type + rdef.rtype.type + cstrtype +
       
   763                                            (self.oldcstr.serialize() or '')).hexdigest()
       
   764                 cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' %
       
   765                                (SQL_PREFIX, rdef.subject.type, oldcstrname))
       
   766             cstrname, check = y2sql.check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
       
   767                     newcstr, syssource.dbhelper, prefix=SQL_PREFIX)
       
   768             cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
       
   769                            (SQL_PREFIX, rdef.subject.type, cstrname, check))
   755 
   770 
   756 
   771 
   757 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
   772 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
   758     entity = None # make pylint happy
   773     entity = None # make pylint happy
   759 
   774