[CalendarServer-changes] [14246] twext/trunk/twext/enterprise/dal/model.py

source_changes at macosforge.org source_changes at macosforge.org
Tue Jan 6 08:42:48 PST 2015


Revision: 14246
          http://trac.calendarserver.org//changeset/14246
Author:   cdaboo at apple.com
Date:     2015-01-06 08:42:48 -0800 (Tue, 06 Jan 2015)
Log Message:
-----------
Better schema/model comparisons.

Modified Paths:
--------------
    twext/trunk/twext/enterprise/dal/model.py

Modified: twext/trunk/twext/enterprise/dal/model.py
===================================================================
--- twext/trunk/twext/enterprise/dal/model.py	2015-01-06 16:20:22 UTC (rev 14245)
+++ twext/trunk/twext/enterprise/dal/model.py	2015-01-06 16:42:48 UTC (rev 14246)
@@ -128,7 +128,23 @@
         self.name = name
 
 
+    def __repr__(self):
+        return "<Constraint: ({} {} {})>".format(self.type, [c.name for c in self.affectsColumns], self.name)
 
+
+    def __hash__(self):
+        return hash(self.__repr__())
+
+
+    def __eq__(self, other):
+        return self.__repr__() == other.__repr__()
+
+
+    def __ne__(self, other):
+        return self.__repr__() != other.__repr__()
+
+
+
 class Check(Constraint):
     """
     A C{check} constraint, which evaluates an SQL expression.
@@ -386,6 +402,12 @@
             if myRows != otherRows:
                 results.append("Table: %s, mismatched schema rows: %s" % (self.name, myRows))
 
+        # Compare psuedo-constraints - ones which include implicit primary key and unique
+        # index items.
+        diff_constraints = set(self.pseudoConstraints()) ^ set(other.pseudoConstraints())
+        if diff_constraints:
+            results.append("Table: %s, mismatched constraints: %s" % (self.name, diff_constraints))
+
         return results
 
 
@@ -493,7 +515,30 @@
                 yield list(constraint.affectsColumns)
 
 
+    def pseudoConstraints(self):
+        """
+        Get constraints and pseudo constraints (ones for implicit not null
+        of a primary key or unique indexes).
 
+        @return: an iterable of C{list}s of C{Constraints}s which are related to
+            this table.
+        """
+        constraints = set(self.constraints)
+
+        if self.primaryKey:
+            for column in self.primaryKey:
+                constraints.add(Constraint(Constraint.NOT_NULL, [column, ]))
+            constraints.add(Constraint(Constraint.UNIQUE, self.primaryKey))
+
+        for idx in self.schema.indexes:
+            if idx.unique and idx.table is self:
+                if self.primaryKey is None or idx.columns != self.primaryKey:
+                    constraints.add(Constraint(Constraint.UNIQUE, idx.columns))
+
+        return (constraint for constraint in constraints)
+
+
+
 class Index(object):
     """
     An L{Index} is an SQL index.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150106/e75b1bbb/attachment.html>


More information about the calendarserver-changes mailing list