<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14458] CalendarServer/branches/users/cdaboo/pod2pod-migration</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14458">14458</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-02-19 19:36:45 -0800 (Thu, 19 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use DAL Record for APN subscriptions.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationcalendarserverpushapplepushpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationcalendarserverpushtesttest_applepushpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationcalendarservertoolspushpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationrequirementsstabletxt">CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresqlpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommonicommondatastorepy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresql_apnpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationcalendarserverpushapplepushpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/applepush.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -234,10 +234,7 @@
</span><span class="cx">                 self.log.debug(
</span><span class="cx">                     &quot;Sending %d APNS notifications for %s&quot; %
</span><span class="cx">                     (numSubscriptions, pushKey))
</span><del>-                tokens = []
-                for token, uid in subscriptions:
-                    if token and uid:
-                        tokens.append(token)
</del><ins>+                tokens = [record.token for record in subscriptions if record.token and record.subscriberGUID]
</ins><span class="cx">                 if tokens:
</span><span class="cx">                     provider.scheduleNotifications(
</span><span class="cx">                         tokens, pushKey,
</span><span class="lines">@@ -349,11 +346,11 @@
</span><span class="cx">                     (token,))
</span><span class="cx">                 txn = self.factory.store.newTransaction(label=&quot;APNProviderProtocol.processError&quot;)
</span><span class="cx">                 subscriptions = (yield txn.apnSubscriptionsByToken(token))
</span><del>-                for key, _ignore_modified, _ignore_uid in subscriptions:
</del><ins>+                for record in subscriptions:
</ins><span class="cx">                     self.log.debug(
</span><span class="cx">                         &quot;Removing subscription: %s %s&quot; %
</span><del>-                        (token, key))
-                    yield txn.removeAPNSubscription(token, key)
</del><ins>+                        (token, record.resourceKey))
+                    yield txn.removeAPNSubscription(token, record.resourceKey)
</ins><span class="cx">                 yield txn.commit()
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -746,12 +743,12 @@
</span><span class="cx">         txn = self.factory.store.newTransaction(label=&quot;APNFeedbackProtocol.processFeedback&quot;)
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsByToken(token))
</span><span class="cx"> 
</span><del>-        for key, modified, _ignore_uid in subscriptions:
-            if timestamp &gt; modified:
</del><ins>+        for record in subscriptions:
+            if timestamp &gt; record.modified:
</ins><span class="cx">                 self.log.debug(
</span><span class="cx">                     &quot;FeedbackProtocol removing subscription: %s %s&quot; %
</span><del>-                    (token, key))
-                yield txn.removeAPNSubscription(token, key)
</del><ins>+                    (token, record.resourceKey))
+                yield txn.removeAPNSubscription(token, record.resourceKey)
</ins><span class="cx">         yield txn.commit()
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationcalendarserverpushtesttest_applepushpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/push/test/test_applepush.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -88,6 +88,7 @@
</span><span class="cx">         yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)
</span><span class="cx"> 
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
</span><ins>+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
</ins><span class="cx">         self.assertTrue([token, key1, timestamp1, userAgent, ipAddr] in subscriptions)
</span><span class="cx">         self.assertTrue([token, key2, timestamp2, userAgent, ipAddr] in subscriptions)
</span><span class="cx">         self.assertTrue([token2, key1, timestamp1, userAgent, ipAddr] in subscriptions)
</span><span class="lines">@@ -98,9 +99,11 @@
</span><span class="cx">         uid2 = &quot;D8FFB335-9D36-4CE8-A3B9-D1859E38C0DA&quot;
</span><span class="cx">         yield txn.addAPNSubscription(token, key2, timestamp3, uid2, userAgent, ipAddr)
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid))
</span><ins>+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
</ins><span class="cx">         self.assertTrue([token, key1, timestamp1, userAgent, ipAddr] in subscriptions)
</span><span class="cx">         self.assertFalse([token, key2, timestamp3, userAgent, ipAddr] in subscriptions)
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsBySubscriber(uid2))
</span><ins>+        subscriptions = [[record.token, record.resourceKey, record.modified, record.userAgent, record.ipAddr] for record in subscriptions]
</ins><span class="cx">         self.assertTrue([token, key2, timestamp3, userAgent, ipAddr] in subscriptions)
</span><span class="cx">         # Change it back
</span><span class="cx">         yield txn.addAPNSubscription(token, key2, timestamp2, uid, userAgent, ipAddr)
</span><span class="lines">@@ -284,10 +287,10 @@
</span><span class="cx">         txn = self._sqlCalendarStore.newTransaction()
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsByToken(token))
</span><span class="cx">         yield txn.commit()
</span><del>-        self.assertEquals(
-            subscriptions,
-            [[&quot;/CalDAV/calendars.example.com/user02/calendar/&quot;, 3000, &quot;D2256BCC-48E2-42D1-BD89-CBA1E4CCDFFB&quot;]]
-        )
</del><ins>+        self.assertEquals(len(subscriptions), 1)
+        self.assertEqual(subscriptions[0].resourceKey, &quot;/CalDAV/calendars.example.com/user02/calendar/&quot;)
+        self.assertEqual(subscriptions[0].modified, 3000)
+        self.assertEqual(subscriptions[0].subscriberGUID, &quot;D2256BCC-48E2-42D1-BD89-CBA1E4CCDFFB&quot;)
</ins><span class="cx"> 
</span><span class="cx">         # Verify processError removes associated subscriptions and history
</span><span class="cx">         # First find the id corresponding to token2
</span><span class="lines">@@ -326,7 +329,7 @@
</span><span class="cx">         subscriptions = (yield txn.apnSubscriptionsByToken(token2))
</span><span class="cx">         yield txn.commit()
</span><span class="cx">         self.assertEquals(len(subscriptions), 1)
</span><del>-        self.assertEquals(subscriptions[0][0], key2)
</del><ins>+        self.assertEquals(subscriptions[0].resourceKey, key2)
</ins><span class="cx"> 
</span><span class="cx">         service.stopService()
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationcalendarservertoolspushpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/calendarserver/tools/push.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -68,9 +68,9 @@
</span><span class="cx">             (yield txn.commit())
</span><span class="cx">             if subscriptions:
</span><span class="cx">                 byKey = {}
</span><del>-                for token, key, timestamp, userAgent, ipAddr in subscriptions:
-                    byKey.setdefault(key, []).append((token, timestamp, userAgent, ipAddr))
-                for key, tokens in byKey.iteritems():
</del><ins>+                for apnrecord in subscriptions:
+                    byKey.setdefault(apnrecord.resourceKey, []).append(apnrecord)
+                for key, apnsrecords in byKey.iteritems():
</ins><span class="cx">                     print
</span><span class="cx">                     protocol, _ignore_host, path = key.strip(&quot;/&quot;).split(&quot;/&quot;, 2)
</span><span class="cx">                     resource = {
</span><span class="lines">@@ -89,13 +89,13 @@
</span><span class="cx">                     else:
</span><span class="cx">                         print(&quot;...is subscribed to %s's %s home&quot; % (user, resource),)
</span><span class="cx">                         # print(&quot;   (key: %s)\n&quot; % (key,))
</span><del>-                    print(&quot;with %d device(s):&quot; % (len(tokens),))
-                    for token, timestamp, userAgent, ipAddr in tokens:
</del><ins>+                    print(&quot;with %d device(s):&quot; % (len(apnsrecords),))
+                    for apnrecords in apnsrecords:
</ins><span class="cx">                         print(&quot; %s\n   '%s' from %s\n   %s&quot; % (
</span><del>-                            token, userAgent, ipAddr,
</del><ins>+                            apnrecords.token, apnrecords.userAgent, apnrecords.ipAddr,
</ins><span class="cx">                             time.strftime(
</span><span class="cx">                                 &quot;on %a, %d %b %Y at %H:%M:%S %z(%Z)&quot;,
</span><del>-                                time.localtime(timestamp)
</del><ins>+                                time.localtime(apnrecords.modified)
</ins><span class="cx">                             )
</span><span class="cx">                         ))
</span><span class="cx">             else:
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationrequirementsstabletxt"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -36,7 +36,7 @@
</span><span class="cx">             #pyOpenSSL
</span><span class="cx">         pycrypto==2.6.1
</span><span class="cx"> 
</span><del>-    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14448#egg=twextpy
</del><ins>+    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14457#egg=twextpy
</ins><span class="cx">         cffi==0.8.6
</span><span class="cx">             pycparser==2.10
</span><span class="cx">         #twisted
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -14,7 +14,6 @@
</span><span class="cx"> # See the License for the specific language governing permissions and
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><del>-from txdav.common.datastore.sql_imip import imipAPIMixin
</del><span class="cx"> 
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> SQL data store.
</span><span class="lines">@@ -62,8 +61,10 @@
</span><span class="cx"> from txdav.carddav.iaddressbookstore import IAddressBookTransaction
</span><span class="cx"> from txdav.common.datastore.common import HomeChildBase
</span><span class="cx"> from txdav.common.datastore.podding.conduit import PoddingConduit
</span><ins>+from txdav.common.datastore.sql_apn import APNSubscriptionsMixin
</ins><span class="cx"> from txdav.common.datastore.sql_directory import DelegatesAPIMixin, \
</span><span class="cx">     GroupsAPIMixin, GroupCacherAPIMixin
</span><ins>+from txdav.common.datastore.sql_imip import imipAPIMixin
</ins><span class="cx"> from txdav.common.datastore.sql_tables import _BIND_MODE_DIRECT, \
</span><span class="cx">     _BIND_MODE_INDIRECT, _BIND_MODE_OWN, _BIND_STATUS_ACCEPTED, \
</span><span class="cx">     _BIND_STATUS_DECLINED, _BIND_STATUS_DELETED, _BIND_STATUS_INVALID, \
</span><span class="lines">@@ -75,7 +76,7 @@
</span><span class="cx"> from txdav.common.icommondatastore import HomeChildNameNotAllowedError, \
</span><span class="cx">     HomeChildNameAlreadyExistsError, NoSuchHomeChildError, \
</span><span class="cx">     ObjectResourceNameNotAllowedError, ObjectResourceNameAlreadyExistsError, \
</span><del>-    NoSuchObjectResourceError, AllRetriesFailed, InvalidSubscriptionValues, \
</del><ins>+    NoSuchObjectResourceError, AllRetriesFailed, \
</ins><span class="cx">     TooManyObjectResourcesError, SyncTokenValidException
</span><span class="cx"> from txdav.common.idirectoryservice import IStoreDirectoryService, \
</span><span class="cx">     DirectoryRecordNotFoundError
</span><span class="lines">@@ -567,7 +568,7 @@
</span><span class="cx"> 
</span><span class="cx"> class CommonStoreTransaction(
</span><span class="cx">     GroupsAPIMixin, GroupCacherAPIMixin, DelegatesAPIMixin,
</span><del>-    imipAPIMixin,
</del><ins>+    imipAPIMixin, APNSubscriptionsMixin,
</ins><span class="cx"> ):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Transaction implementation for SQL database.
</span><span class="lines">@@ -785,145 +786,6 @@
</span><span class="cx">         return NotificationCollection.notificationsWithResourceID(self, rid)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    @classproperty
-    def _insertAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Insert({
-            apn.TOKEN: Parameter(&quot;token&quot;),
-            apn.RESOURCE_KEY: Parameter(&quot;resourceKey&quot;),
-            apn.MODIFIED: Parameter(&quot;modified&quot;),
-            apn.SUBSCRIBER_GUID: Parameter(&quot;subscriber&quot;),
-            apn.USER_AGENT: Parameter(&quot;userAgent&quot;),
-            apn.IP_ADDR: Parameter(&quot;ipAddr&quot;)
-        })
-
-
-    @classproperty
-    def _updateAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Update(
-            {
-                apn.MODIFIED: Parameter(&quot;modified&quot;),
-                apn.SUBSCRIBER_GUID: Parameter(&quot;subscriber&quot;),
-                apn.USER_AGENT: Parameter(&quot;userAgent&quot;),
-                apn.IP_ADDR: Parameter(&quot;ipAddr&quot;)
-            },
-            Where=(apn.TOKEN == Parameter(&quot;token&quot;)).And(
-                apn.RESOURCE_KEY == Parameter(&quot;resourceKey&quot;))
-        )
-
-
-    @classproperty
-    def _selectAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select(
-            [apn.MODIFIED, apn.SUBSCRIBER_GUID],
-            From=apn,
-            Where=(apn.TOKEN == Parameter(&quot;token&quot;)).And(
-                apn.RESOURCE_KEY == Parameter(&quot;resourceKey&quot;)
-            )
-        )
-
-
-    @inlineCallbacks
-    def addAPNSubscription(
-        self, token, key, timestamp, subscriber,
-        userAgent, ipAddr
-    ):
-        if not (token and key and timestamp and subscriber):
-            raise InvalidSubscriptionValues()
-
-        # Cap these values at 255 characters
-        userAgent = userAgent[:255]
-        ipAddr = ipAddr[:255]
-
-        row = yield self._selectAPNSubscriptionQuery.on(
-            self,
-            token=token, resourceKey=key
-        )
-        if not row:  # Subscription does not yet exist
-            try:
-                yield self._insertAPNSubscriptionQuery.on(
-                    self,
-                    token=token, resourceKey=key, modified=timestamp,
-                    subscriber=subscriber, userAgent=userAgent,
-                    ipAddr=ipAddr)
-            except Exception:
-                # Subscription may have been added by someone else, which is fine
-                pass
-
-        else:  # Subscription exists, so update with new timestamp and subscriber
-            try:
-                yield self._updateAPNSubscriptionQuery.on(
-                    self,
-                    token=token, resourceKey=key, modified=timestamp,
-                    subscriber=subscriber, userAgent=userAgent,
-                    ipAddr=ipAddr)
-            except Exception:
-                # Subscription may have been added by someone else, which is fine
-                pass
-
-
-    @classproperty
-    def _removeAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Delete(From=apn,
-                      Where=(apn.TOKEN == Parameter(&quot;token&quot;)).And(
-                          apn.RESOURCE_KEY == Parameter(&quot;resourceKey&quot;)))
-
-
-    def removeAPNSubscription(self, token, key):
-        return self._removeAPNSubscriptionQuery.on(
-            self,
-            token=token, resourceKey=key)
-
-
-    @classproperty
-    def _purgeOldAPNSubscriptionQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Delete(From=apn,
-                      Where=(apn.MODIFIED &lt; Parameter(&quot;olderThan&quot;)))
-
-
-    def purgeOldAPNSubscriptions(self, olderThan):
-        return self._purgeOldAPNSubscriptionQuery.on(
-            self,
-            olderThan=olderThan)
-
-
-    @classproperty
-    def _apnSubscriptionsByTokenQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.RESOURCE_KEY, apn.MODIFIED, apn.SUBSCRIBER_GUID],
-                      From=apn, Where=apn.TOKEN == Parameter(&quot;token&quot;))
-
-
-    def apnSubscriptionsByToken(self, token):
-        return self._apnSubscriptionsByTokenQuery.on(self, token=token)
-
-
-    @classproperty
-    def _apnSubscriptionsByKeyQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.TOKEN, apn.SUBSCRIBER_GUID],
-                      From=apn, Where=apn.RESOURCE_KEY == Parameter(&quot;resourceKey&quot;))
-
-
-    def apnSubscriptionsByKey(self, key):
-        return self._apnSubscriptionsByKeyQuery.on(self, resourceKey=key)
-
-
-    @classproperty
-    def _apnSubscriptionsBySubscriberQuery(cls):
-        apn = schema.APN_SUBSCRIPTIONS
-        return Select([apn.TOKEN, apn.RESOURCE_KEY, apn.MODIFIED, apn.USER_AGENT, apn.IP_ADDR],
-                      From=apn, Where=apn.SUBSCRIBER_GUID == Parameter(&quot;subscriberGUID&quot;))
-
-
-    def apnSubscriptionsBySubscriber(self, guid):
-        return self._apnSubscriptionsBySubscriberQuery.on(self, subscriberGUID=guid)
-
-
</del><span class="cx">     def preCommit(self, operation):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Run things before C{commit}.  (Note: only provided by SQL
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresql_apnpy"></a>
<div class="addfile"><h4>Added: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py (0 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py                                (rev 0)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_apn.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -0,0 +1,121 @@
</span><ins>+# -*- test-case-name: twext.enterprise.dal.test.test_record -*-
+##
+# Copyright (c) 2015 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from twext.enterprise.dal.record import SerializableRecord, fromTable
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks
+from txdav.common.datastore.sql_tables import schema
+from txdav.common.icommondatastore import InvalidSubscriptionValues
+
+log = Logger()
+
+&quot;&quot;&quot;
+Classes and methods that relate to APN objects in the SQL store.
+&quot;&quot;&quot;
+
+class APNSubscriptionsRecord(SerializableRecord, fromTable(schema.APN_SUBSCRIPTIONS)):
+    &quot;&quot;&quot;
+    @DynamicAttrs
+    L{Record} for L{schema.APN_SUBSCRIPTIONS}.
+    &quot;&quot;&quot;
+    pass
+
+
+
+class APNSubscriptionsMixin(object):
+    &quot;&quot;&quot;
+    A mixin for L{CommonStoreTransaction} that covers the APN API.
+    &quot;&quot;&quot;
+
+    @inlineCallbacks
+    def addAPNSubscription(
+        self, token, key, timestamp, subscriber,
+        userAgent, ipAddr
+    ):
+        if not (token and key and timestamp and subscriber):
+            raise InvalidSubscriptionValues()
+
+        # Cap these values at 255 characters
+        userAgent = userAgent[:255]
+        ipAddr = ipAddr[:255]
+
+        records = yield APNSubscriptionsRecord.querysimple(
+            self,
+            token=token, resourceKey=key
+        )
+        if not records:  # Subscription does not yet exist
+            try:
+                yield APNSubscriptionsRecord.create(
+                    self,
+                    token=token,
+                    resourceKey=key,
+                    modified=timestamp,
+                    subscriberGUID=subscriber,
+                    userAgent=userAgent,
+                    ipAddr=ipAddr
+                )
+            except Exception:
+                # Subscription may have been added by someone else, which is fine
+                pass
+
+        else:  # Subscription exists, so update with new timestamp and subscriber
+            try:
+                yield records[0].update(
+                    modified=timestamp,
+                    subscriberGUID=subscriber,
+                    userAgent=userAgent,
+                    ipAddr=ipAddr,
+                )
+            except Exception:
+                # Subscription may have been added by someone else, which is fine
+                pass
+
+
+    def removeAPNSubscription(self, token, key):
+        return APNSubscriptionsRecord.deletesimple(
+            self,
+            token=token,
+            resourceKey=key
+        )
+
+
+    def purgeOldAPNSubscriptions(self, olderThan):
+        return APNSubscriptionsRecord.deletesome(
+            self,
+            APNSubscriptionsRecord.modified &lt; olderThan,
+        )
+
+
+    def apnSubscriptionsByToken(self, token):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            token=token,
+        )
+
+
+    def apnSubscriptionsByKey(self, key):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            resourceKey=key,
+        )
+
+
+    def apnSubscriptionsBySubscriber(self, guid):
+        return APNSubscriptionsRecord.querysimple(
+            self,
+            subscriberGUID=guid,
+        )
</ins></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommonicommondatastorepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py (14457 => 14458)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py        2015-02-20 03:35:37 UTC (rev 14457)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/icommondatastore.py        2015-02-20 03:36:45 UTC (rev 14458)
</span><span class="lines">@@ -347,7 +347,7 @@
</span><span class="cx">         @param token: The device token of the subscriber
</span><span class="cx">         @type token: C{str}
</span><span class="cx"> 
</span><del>-        @return: tuples of (key, timestamp, guid)
</del><ins>+        @return: list of L{Record}
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     def apnSubscriptionsByKey(key): #@NoSelf
</span><span class="lines">@@ -357,7 +357,7 @@
</span><span class="cx">         @param key: The push key
</span><span class="cx">         @type key: C{str}
</span><span class="cx"> 
</span><del>-        @return: tuples of (token, guid)
</del><ins>+        @return: list of L{Record}
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     def apnSubscriptionsBySubscriber(guid): #@NoSelf
</span><span class="lines">@@ -367,7 +367,7 @@
</span><span class="cx">         @param guid: The GUID of the subscribed principal
</span><span class="cx">         @type guid: C{str}
</span><span class="cx"> 
</span><del>-        @return: tuples of (token, key, timestamp, userAgent, ipAddr)
</del><ins>+        @return: list of L{Record}
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     def imipCreateToken(organizer, attendee, icaluid, token=None): #@NoSelf
</span></span></pre>
</div>
</div>

</body>
</html>