hooks/syncschema.py
changeset 10640 17bdc485c5b2
parent 10074 ab956b780d4e
parent 10586 22f330f829ae
child 10646 45671fb330f5
--- a/hooks/syncschema.py	Thu Oct 08 18:24:09 2015 +0200
+++ b/hooks/syncschema.py	Thu Oct 08 18:38:16 2015 +0200
@@ -696,6 +696,16 @@
         syssource = cnx.repo.system_source
         cstrtype = self.oldcstr.type()
         if cstrtype == 'SizeConstraint':
+            # if the size constraint is being replaced with a new max size, we'll
+            # call update_rdef_column in CWConstraintAddOp, skip it here
+            for cstr in cnx.transaction_data.get('newsizecstr', ()):
+                rdefentity = cstr.reverse_constrained_by[0]
+                cstrrdef = cnx.vreg.schema.schema_by_eid(rdefentity.eid)
+                if cstrrdef == rdef:
+                    return
+
+            # we found that the size constraint for this rdef is really gone,
+            # not just replaced by another
             syssource.update_rdef_column(cnx, rdef)
             self.size_cstr_changed = True
         elif cstrtype == 'UniqueConstraint':
@@ -775,6 +785,13 @@
     entity = cstrname = None # for pylint
     cols = () # for pylint
 
+    def insert_index(self):
+        # We need to run before CWConstraintDelOp: if a size constraint is
+        # removed and the column is part of a unique_together constraint, we
+        # remove the unique_together index before changing the column's type.
+        # SQL Server does not support unique indices on unlimited text columns.
+        return 0
+
     def precommit_event(self):
         cnx = self.cnx
         prefix = SQL_PREFIX
@@ -1205,6 +1222,11 @@
     events = ('after_add_entity', 'after_update_entity')
 
     def __call__(self):
+        if self.entity.cstrtype[0].name == 'SizeConstraint':
+            txdata = self._cw.transaction_data
+            if 'newsizecstr' not in txdata:
+                txdata['newsizecstr'] = set()
+            txdata['newsizecstr'].add(self.entity)
         CWConstraintAddOp(self._cw, entity=self.entity)