[hooks/syncschema] drop/add check constraints as appropriate
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 18 May 2015 19:47:34 +0200
changeset 10445 f1773842077d
parent 10444 fb7c1013189e
child 10446 1e6655cff5ab
[hooks/syncschema] drop/add check constraints as appropriate Related to #5154406
hooks/syncschema.py
--- a/hooks/syncschema.py	Sun Mar 22 19:39:29 2015 +0100
+++ b/hooks/syncschema.py	Mon May 18 19:47:34 2015 +0200
@@ -27,6 +27,7 @@
 _ = unicode
 
 from copy import copy
+from hashlib import md5
 from yams.schema import (BASE_TYPES, BadSchemaDefinition,
                          RelationSchema, RelationDefinitionSchema)
 from yams import buildobjs as ybo, convert_default_value
@@ -705,6 +706,10 @@
         elif cstrtype == 'UniqueConstraint':
             syssource.update_rdef_unique(cnx, rdef)
             self.unique_changed = True
+        if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
+            cstrname = 'cstr' + md5(rdef.subject.type + rdef.rtype.type + cstrtype +
+                                    (self.oldcstr.serialize() or '')).hexdigest()
+            cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' % (SQL_PREFIX, rdef.subject.type, cstrname))
 
     def revertprecommit_event(self):
         # revert changes on in memory schema
@@ -752,6 +757,16 @@
         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
             syssource.update_rdef_unique(cnx, rdef)
             self.unique_changed = True
+        if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
+            if oldcstr is not None:
+                oldcstrname = 'cstr' + md5(rdef.subject.type + rdef.rtype.type + cstrtype +
+                                           (self.oldcstr.serialize() or '')).hexdigest()
+                cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' %
+                               (SQL_PREFIX, rdef.subject.type, oldcstrname))
+            cstrname, check = y2sql.check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
+                    newcstr, syssource.dbhelper, prefix=SQL_PREFIX)
+            cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
+                           (SQL_PREFIX, rdef.subject.type, cstrname, check))
 
 
 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):