[CalendarServer-changes] [14416] twext/branches/users/cdaboo

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 16 12:33:55 PST 2015


Revision: 14416
          http://trac.calendarserver.org//changeset/14416
Author:   cdaboo at apple.com
Date:     2015-02-16 12:33:55 -0800 (Mon, 16 Feb 2015)
Log Message:
-----------
Branch for pod-to-pod migration work.

Modified Paths:
--------------
    twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py
    twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py

Added Paths:
-----------
    twext/branches/users/cdaboo/pod2pod-migration/


Property changes on: twext/branches/users/cdaboo/pod2pod-migration
___________________________________________________________________
Added: svn:ignore
   + .develop
.project
.pydevproject
.trial
.trial.lock
dist
twextpy.egg-info

Added: svn:mergeinfo
   + /twext/branches/users/cdaboo/jobqueue-3:13444-13471
/twext/branches/users/cdaboo/jobs:12742-12780
/twext/branches/users/sagen/recordtypes:13647-13658
/twext/branches/users/sagen/recordtypes-2:13659

Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py
===================================================================
--- twext/trunk/twext/enterprise/dal/record.py	2015-02-05 20:12:23 UTC (rev 14376)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/record.py	2015-02-16 20:33:55 UTC (rev 14416)
@@ -198,7 +198,7 @@
         words = columnName.lower().split("_")
 
         def cap(word):
-            if word.lower() == "id":
+            if word.lower() in ("id", "uid",):
                 return word.upper()
             else:
                 return word.capitalize()
@@ -246,41 +246,42 @@
 
             MyRecord.create(transaction, column1=1, column2=u"two")
         """
+        self = cls.make(**k)
+        yield self.insert(transaction)
+        returnValue(self)
+
+
+    @classmethod
+    def make(cls, **k):
+        """
+        Make a record without creating one in the database - this will not have an
+        associated L{transaction}. When the record is ready to be written to the database
+        use L{SerializeableRecord.insert} to add it. Before it gets written to the DB, the
+        attributes can be changed.
+        """
         self = cls()
-        colmap = {}
         attrtocol = cls.__attrmap__
-        needsCols = []
-        needsAttrs = []
 
         for attr in attrtocol:
             col = attrtocol[attr]
             if attr in k:
-                setattr(self, attr, k[attr])
-                colmap[col] = k.pop(attr)
+                value = k.pop(attr)
+                setattr(self, attr, value)
             else:
                 if col.model.needsValue():
                     raise TypeError(
                         "required attribute {0!r} not passed"
                         .format(attr)
                     )
-                else:
-                    needsCols.append(col)
-                    needsAttrs.append(attr)
 
         if k:
             raise TypeError("received unknown attribute{0}: {1}".format(
                 "s" if len(k) > 1 else "", ", ".join(sorted(k))
             ))
-        result = yield (Insert(colmap, Return=needsCols if needsCols else None)
-                        .on(transaction))
-        if needsCols:
-            self._attributesFromRow(zip(needsAttrs, result[0]))
 
-        self.transaction = transaction
+        return self
 
-        returnValue(self)
 
-
     def _attributesFromRow(self, attributeList):
         """
         Take some data loaded from a row and apply it to this instance,
@@ -296,6 +297,46 @@
             setattr(self, setAttribute, setValue)
 
 
+    @inlineCallbacks
+    def insert(self, transaction):
+        """
+        Insert a new a row for an existing record that was not initially created in the database.
+        """
+
+        # Cannot do this if a transaction has already been assigned because that means
+        # the record already exists in the DB.
+
+        if self.transaction is not None:
+            raise ReadOnly(self.__class__.__name__, "Cannot insert")
+
+        colmap = {}
+        attrtocol = self.__attrmap__
+        needsCols = []
+        needsAttrs = []
+
+        for attr in attrtocol:
+            col = attrtocol[attr]
+            v = getattr(self, attr)
+            if not isinstance(v, ColumnSyntax):
+                colmap[col] = v
+            else:
+                if col.model.needsValue():
+                    raise TypeError(
+                        "required attribute {0!r} not passed"
+                        .format(attr)
+                    )
+                else:
+                    needsCols.append(col)
+                    needsAttrs.append(attr)
+
+        result = yield (Insert(colmap, Return=needsCols if needsCols else None)
+                        .on(transaction))
+        if needsCols:
+            self._attributesFromRow(zip(needsAttrs, result[0]))
+
+        self.transaction = transaction
+
+
     def delete(self):
         """
         Delete this row from the database.
@@ -435,6 +476,46 @@
         @param noWait: include NOWAIT with the FOR UPDATE
         @type noWait: L{bool}
         """
+        return cls._rowsFromQuery(
+            transaction,
+            cls.queryExpr(
+                expr,
+                order=order,
+                ascending=ascending,
+                group=group,
+                forUpdate=forUpdate,
+                noWait=noWait,
+                limit=limit,
+            ),
+            None
+        )
+
+
+    @classmethod
+    def queryExpr(cls, expr, attributes=None, order=None, ascending=True, group=None, forUpdate=False, noWait=False, limit=None):
+        """
+        Query expression that corresponds to C{cls}. Used in cases where a sub-select
+        on this record's table is needed.
+
+        @param expr: An L{ExpressionSyntax} that constraints the results of the
+            query.  This is most easily produced by accessing attributes on the
+            class; for example, C{MyRecordType.query((MyRecordType.col1 >
+            MyRecordType.col2).And(MyRecordType.col3 == 7))}
+
+        @param order: A L{ColumnSyntax} to order the resulting record objects
+            by.
+
+        @param ascending: A boolean; if C{order} is not C{None}, whether to
+            sort in ascending or descending order.
+
+        @param group: a L{ColumnSyntax} to group the resulting record objects
+            by.
+
+        @param forUpdate: do a SELECT ... FOR UPDATE
+        @type forUpdate: L{bool}
+        @param noWait: include NOWAIT with the FOR UPDATE
+        @type noWait: L{bool}
+        """
         kw = {}
         if order is not None:
             kw.update(OrderBy=order, Ascending=ascending)
@@ -446,15 +527,13 @@
                 kw.update(NoWait=True)
         if limit is not None:
             kw.update(Limit=limit)
-        return cls._rowsFromQuery(
-            transaction,
-            Select(
-                list(cls.table),
-                From=cls.table,
-                Where=expr,
-                **kw
-            ),
-            None
+        if attributes is None:
+            attributes = list(cls.table)
+        return Select(
+            attributes,
+            From=cls.table,
+            Where=expr,
+            **kw
         )
 
 
@@ -495,6 +574,21 @@
 
 
     @classmethod
+    def deletematch(cls, transaction, **kw):
+        """
+        Delete all rows matching the specified attribute/values from the table that corresponds to C{cls}.
+        """
+        where = None
+        for k, v in kw.iteritems():
+            subexpr = (cls.__attrmap__[k] == v)
+            if where is None:
+                where = subexpr
+            else:
+                where = where.And(subexpr)
+        return cls.deletesome(transaction, where)
+
+
+    @classmethod
     @inlineCallbacks
     def _rowsFromQuery(cls, transaction, qry, rozrc):
         """
@@ -521,3 +615,45 @@
             self.transaction = transaction
             selves.append(self)
         returnValue(selves)
+
+
+
+class SerializableRecord(Record):
+    """
+    An L{Record} that serializes/deserializes its attributes for a text-based
+    transport (e.g., JSON-over-HTTP) to allow records to be transferred from
+    one system to another (with potentially mismatched schemas).
+    """
+
+    def serialize(self):
+        """
+        Create an L{dict} of each attribute with L{str} values for each attribute
+        value. Sub-classes may need to override this to specialize certain value
+        conversions.
+
+        @return: mapping of attribute to string values
+        @rtype: L{dict} of L{str}:L{str}
+        """
+
+        result = dict([(attr, getattr(self, attr),) for attr in self.__attrmap__])
+        return result
+
+
+    @classmethod
+    def deserialize(cls, attrmap):
+        """
+        Given an L{dict} mapping attributes to values, create an L{Record} with
+        the specified values. Sub-classes may need to override this to handle special
+        values that need to be converted to specific types. They also need to override
+        this to handle possible schema mismatches (attributes no longer used, new
+        attributes not present in the map).
+
+        @param attrmap: serialized representation of a record
+        @type attrmap: L{dict} of L{str}:L{str}
+
+        @return: a newly created, but not inserted, record
+        @rtype: L{SerializableRecord}
+        """
+
+        record = cls.make(**attrmap)
+        return record

Modified: twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py
===================================================================
--- twext/trunk/twext/enterprise/dal/test/test_record.py	2015-02-05 20:12:23 UTC (rev 14376)
+++ twext/branches/users/cdaboo/pod2pod-migration/twext/enterprise/dal/test/test_record.py	2015-02-16 20:33:55 UTC (rev 14416)
@@ -24,8 +24,8 @@
 from twisted.trial.unittest import TestCase, SkipTest
 
 from twext.enterprise.dal.record import (
-    Record, fromTable, ReadOnly, NoSuchRecord
-)
+    Record, fromTable, ReadOnly, NoSuchRecord,
+    SerializableRecord)
 from twext.enterprise.dal.test.test_parseschema import SchemaTestHelper
 from twext.enterprise.dal.syntax import SchemaSyntax
 from twext.enterprise.fixtures import buildConnectionPool
@@ -67,6 +67,13 @@
 
 
 
+class TestSerializeRecord(SerializableRecord, Alpha):
+    """
+    A sample test serializable record with default values specified.
+    """
+
+
+
 class TestAutoRecord(Record, Delta):
     """
     A sample test record with default values specified.
@@ -335,6 +342,48 @@
 
 
     @inlineCallbacks
+    def test_deletesome(self):
+        """
+        L{Record.deletesome} will delete all instances of the matching records.
+        """
+        txn = self.pool.connection()
+        data = [(123, u"one"), (456, u"four"), (345, u"three"),
+                (234, u"two"), (356, u"three")]
+        for beta, gamma in data:
+            yield txn.execSQL("insert into ALPHA values (:1, :2)",
+                              [beta, gamma])
+
+        yield TestRecord.deletesome(txn, TestRecord.gamma == u"three")
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((123, 456, 234,)))
+
+        yield TestRecord.deletesome(txn, (TestRecord.gamma == u"one").Or(TestRecord.gamma == u"two"))
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((456,)))
+
+
+    @inlineCallbacks
+    def test_deletematch(self):
+        """
+        L{Record.deletematch} will delete all instances of the matching records.
+        """
+        txn = self.pool.connection()
+        data = [(123, u"one"), (456, u"four"), (345, u"three"),
+                (234, u"two"), (356, u"three")]
+        for beta, gamma in data:
+            yield txn.execSQL("insert into ALPHA values (:1, :2)",
+                              [beta, gamma])
+
+        yield TestRecord.deletematch(txn, gamma=u"three")
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((123, 456, 234,)))
+
+        yield TestRecord.deletematch(txn, beta=123, gamma=u"one")
+        all = yield TestRecord.all(txn)
+        self.assertEqual(set([record.beta for record in all]), set((456, 234)))
+
+
+    @inlineCallbacks
     def test_repr(self):
         """
         The C{repr} of a L{Record} presents all its values.
@@ -458,3 +507,59 @@
         rec = yield TestRecord.load(txn, 234)
         result = yield rec.trylock()
         self.assertTrue(result)
+
+
+    @inlineCallbacks
+    def test_serialize(self):
+        """
+        A L{SerializableRecord} may be serialized.
+        """
+        txn = self.pool.connection()
+        for beta, gamma in [
+            (123, u"one"),
+            (234, u"two"),
+            (345, u"three"),
+            (356, u"three"),
+            (456, u"four"),
+        ]:
+            yield txn.execSQL(
+                "insert into ALPHA values (:1, :2)", [beta, gamma]
+            )
+
+        rec = yield TestSerializeRecord.load(txn, 234)
+        result = rec.serialize()
+        self.assertEqual(result, {"beta": 234, "gamma": u"two"})
+
+
+    @inlineCallbacks
+    def test_deserialize(self):
+        """
+        A L{SerializableRecord} may be deserialized.
+        """
+        txn = self.pool.connection()
+
+        rec = yield TestSerializeRecord.deserialize({"beta": 234, "gamma": u"two"})
+        yield rec.insert(txn)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.query(txn, TestSerializeRecord.beta == 234)
+        self.assertEqual(len(rec), 1)
+        self.assertEqual(rec[0].gamma, u"two")
+        yield txn.commit()
+
+        # Check that attributes can be changed prior to insert, and not after
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.deserialize({"beta": 456, "gamma": u"one"})
+        rec.gamma = u"four"
+        yield rec.insert(txn)
+        def _raise():
+            rec.gamma = u"five"
+        self.assertRaises(ReadOnly, _raise)
+        yield txn.commit()
+
+        txn = self.pool.connection()
+        rec = yield TestSerializeRecord.query(txn, TestSerializeRecord.beta == 456)
+        self.assertEqual(len(rec), 1)
+        self.assertEqual(rec[0].gamma, u"four")
+        yield txn.commit()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150216/546ee6c5/attachment-0001.html>


More information about the calendarserver-changes mailing list