<!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>[12663] CalendarServer/trunk</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/12663">12663</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-02-11 16:30:58 -0800 (Tue, 11 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>New group cacher based on twext.who; delegates in the store</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertapcaldavpy">CalendarServer/trunk/calendarserver/tap/caldav.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsprincipalspy">CalendarServer/trunk/calendarserver/tools/principals.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolsutilpy">CalendarServer/trunk/calendarserver/tools/util.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavdirectorydirectorypy">CalendarServer/trunk/twistedcaldav/directory/directory.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavdirectorytesttest_directorypy">CalendarServer/trunk/twistedcaldav/directory/test/test_directory.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavstdconfigpy">CalendarServer/trunk/twistedcaldav/stdconfig.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresqlpy">CalendarServer/trunk/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemacurrentoracledialectsql">CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemacurrentsql">CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaoldoracledialectv34sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/old/oracle-dialect/v34.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaoldpostgresdialectv34sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/old/postgres-dialect/v34.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaupgradesoracledialectupgrade_from_34_to_35sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaupgradespostgresdialectupgrade_from_34_to_35sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql</a></li>
<li><a href="#CalendarServertrunktxdavwhodelegatespy">CalendarServer/trunk/txdav/who/delegates.py</a></li>
<li><a href="#CalendarServertrunktxdavwhogroupspy">CalendarServer/trunk/txdav/who/groups.py</a></li>
<li>CalendarServer/trunk/txdav/who/test/accounts/</li>
<li><a href="#CalendarServertrunktxdavwhotestaccountsaccountsxml">CalendarServer/trunk/txdav/who/test/accounts/accounts.xml</a></li>
<li><a href="#CalendarServertrunktxdavwhotestaccountsresourcesxml">CalendarServer/trunk/txdav/who/test/accounts/resources.xml</a></li>
<li><a href="#CalendarServertrunktxdavwhotesttest_delegatespy">CalendarServer/trunk/txdav/who/test/test_delegates.py</a></li>
<li><a href="#CalendarServertrunktxdavwhotesttest_groupspy">CalendarServer/trunk/txdav/who/test/test_groups.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertapcaldavpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tap/caldav.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tap/caldav.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -78,12 +78,14 @@
</span><span class="cx">     UpgradeAcquireLockStep, UpgradeReleaseLockStep, UpgradeDatabaseNotificationDataStep)
</span><span class="cx"> from txdav.common.datastore.work.revision_cleanup import scheduleFirstFindMinRevision
</span><span class="cx"> from txdav.dps.server import DirectoryProxyServiceMaker
</span><ins>+from txdav.dps.client import DirectoryService as DirectoryProxyClientService
+from txdav.who.groups import GroupCacher as NewGroupCacher
</ins><span class="cx"> 
</span><span class="cx"> from twistedcaldav import memcachepool
</span><span class="cx"> from twistedcaldav.config import config, ConfigurationError
</span><span class="cx"> from twistedcaldav.directory import calendaruserproxy
</span><span class="cx"> from twistedcaldav.directory.directory import GroupMembershipCacheUpdater
</span><del>-from twistedcaldav.directory.directory import scheduleNextGroupCachingUpdate
</del><ins>+from txdav.who.groups import scheduleNextGroupCachingUpdate
</ins><span class="cx"> from twistedcaldav.localization import processLocalizationFiles
</span><span class="cx"> from twistedcaldav.stdconfig import DEFAULT_CONFIG, DEFAULT_CONFIG_FILE
</span><span class="cx"> from twistedcaldav.upgrade import UpgradeFileSystemFormatStep, PostDBImportStep
</span><span class="lines">@@ -1239,9 +1241,11 @@
</span><span class="cx">                     config.GroupCaching.LockSeconds,
</span><span class="cx">                     namespace=config.GroupCaching.MemcachedPool,
</span><span class="cx">                     useExternalProxies=config.GroupCaching.UseExternalProxies
</span><del>-                    )
</del><ins>+                )
+                newGroupCacher = NewGroupCacher(DirectoryProxyClientService(None))
</ins><span class="cx">             else:
</span><span class="cx">                 groupCacher = None
</span><ins>+                newGroupCacher = None
</ins><span class="cx"> 
</span><span class="cx">             # Optionally enable Manhole access
</span><span class="cx">             if config.Manhole.Enabled:
</span><span class="lines">@@ -1275,6 +1279,7 @@
</span><span class="cx">                 txn._rootResource = result.rootResource
</span><span class="cx">                 txn._mailRetriever = mailRetriever
</span><span class="cx">                 txn._groupCacher = groupCacher
</span><ins>+                txn._newGroupCacher = newGroupCacher
</ins><span class="cx"> 
</span><span class="cx">             store.callWithNewTransactions(decorateTransaction)
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsprincipalspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/principals.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/principals.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/calendarserver/tools/principals.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -31,7 +31,7 @@
</span><span class="cx"> 
</span><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from twistedcaldav.directory.directory import UnknownRecordTypeError, DirectoryError
</span><del>-from twistedcaldav.directory.directory import schedulePolledGroupCachingUpdate
</del><ins>+from txdav.who.groups import schedulePolledGroupCachingUpdate
</ins><span class="cx"> 
</span><span class="cx"> from calendarserver.tools.util import (
</span><span class="cx">     booleanArgument, proxySubprincipal, action_addProxyPrincipal,
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolsutilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/util.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/util.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/calendarserver/tools/util.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -50,7 +50,7 @@
</span><span class="cx"> from twistedcaldav.directory import calendaruserproxy
</span><span class="cx"> from twistedcaldav.directory.aggregate import AggregateDirectoryService
</span><span class="cx"> from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord
</span><del>-from twistedcaldav.directory.directory import schedulePolledGroupCachingUpdate
</del><ins>+from txdav.who.groups import schedulePolledGroupCachingUpdate
</ins><span class="cx"> from calendarserver.push.notifier import NotifierFactory
</span><span class="cx"> 
</span><span class="cx"> from txdav.common.datastore.file import CommonDataStore
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavdirectorydirectorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/directory/directory.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/directory/directory.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/twistedcaldav/directory/directory.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -31,9 +31,6 @@
</span><span class="cx"> 
</span><span class="cx"> from plistlib import readPlistFromString
</span><span class="cx"> 
</span><del>-from twext.enterprise.dal.record import fromTable
-from twext.enterprise.dal.syntax import Delete
-from twext.enterprise.queue import WorkItem, PeerConnectionPool
</del><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> from txweb2.dav.auth import IPrincipalCredentials
</span><span class="cx"> from txweb2.dav.util import joinURL
</span><span class="lines">@@ -52,7 +49,6 @@
</span><span class="cx"> 
</span><span class="cx"> from txdav.caldav.icalendardirectoryservice import ICalendarStoreDirectoryService, \
</span><span class="cx">     ICalendarStoreDirectoryRecord
</span><del>-from txdav.common.datastore.sql_tables import schema
</del><span class="cx"> 
</span><span class="cx"> from xml.parsers.expat import ExpatError
</span><span class="cx"> 
</span><span class="lines">@@ -1041,61 +1037,8 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class GroupCacherPollingWork(WorkItem, fromTable(schema.GROUP_CACHER_POLLING_WORK)):
</del><span class="cx"> 
</span><del>-    group = &quot;group_cacher_polling&quot;
</del><span class="cx"> 
</span><del>-    @inlineCallbacks
-    def doWork(self):
-
-        # Delete all other work items
-        yield Delete(From=self.table, Where=None).on(self.transaction)
-
-        groupCacher = getattr(self.transaction, &quot;_groupCacher&quot;, None)
-        if groupCacher is not None:
-
-            # Schedule next update
-            notBefore = (datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=groupCacher.updateSeconds))
-            log.debug(&quot;Scheduling next group cacher update: %s&quot; % (notBefore,))
-            yield self.transaction.enqueue(GroupCacherPollingWork,
-                notBefore=notBefore)
-
-            try:
-                groupCacher.updateCache()
-            except Exception, e:
-                log.error(&quot;Failed to update group membership cache (%s)&quot; % (e,))
-
-        else:
-            notBefore = (datetime.datetime.utcnow() +
-                datetime.timedelta(seconds=10))
-            log.debug(&quot;Rescheduling group cacher update: %s&quot; % (notBefore,))
-            yield self.transaction.enqueue(GroupCacherPollingWork,
-                notBefore=notBefore)
-
-
-
-@inlineCallbacks
-def scheduleNextGroupCachingUpdate(store, seconds):
-    txn = store.newTransaction()
-    notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
-    log.debug(&quot;Scheduling next group cacher update: %s&quot; % (notBefore,))
-    wp = (yield txn.enqueue(GroupCacherPollingWork, notBefore=notBefore))
-    yield txn.commit()
-    returnValue(wp)
-
-
-
-def schedulePolledGroupCachingUpdate(store):
-    &quot;&quot;&quot;
-    Schedules a group caching update work item in &quot;the past&quot; so PeerConnectionPool's
-    overdue-item logic picks it up quickly.
-    &quot;&quot;&quot;
-    seconds = -PeerConnectionPool.queueProcessTimeout
-    return scheduleNextGroupCachingUpdate(store, seconds)
-
-
-
</del><span class="cx"> def diffAssignments(old, new):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Compare two proxy assignment lists and return their differences in the form of
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavdirectorytesttest_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/directory/test/test_directory.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/directory/test/test_directory.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/twistedcaldav/directory/test/test_directory.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -20,13 +20,12 @@
</span><span class="cx"> from twistedcaldav.test.util import TestCase
</span><span class="cx"> from twistedcaldav.test.util import xmlFile, augmentsFile, proxiesFile, dirTest
</span><span class="cx"> from twistedcaldav.config import config
</span><del>-from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord, GroupMembershipCache, GroupMembershipCacheUpdater, diffAssignments, schedulePolledGroupCachingUpdate
</del><ins>+from twistedcaldav.directory.directory import DirectoryService, DirectoryRecord, GroupMembershipCache, GroupMembershipCacheUpdater, diffAssignments
</ins><span class="cx"> from twistedcaldav.directory.xmlfile import XMLDirectoryService
</span><span class="cx"> from twistedcaldav.directory.calendaruserproxyloader import XMLCalendarUserProxyLoader
</span><span class="cx"> from twistedcaldav.directory import augment, calendaruserproxy
</span><span class="cx"> from twistedcaldav.directory.util import normalizeUUID
</span><span class="cx"> from twistedcaldav.directory.principal import DirectoryPrincipalProvisioningResource
</span><del>-from txdav.common.datastore.test.util import buildStore
</del><span class="cx"> 
</span><span class="cx"> import cPickle as pickle
</span><span class="cx"> import uuid
</span><span class="lines">@@ -926,25 +925,25 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    @inlineCallbacks
-    def testScheduling(self):
-        &quot;&quot;&quot;
-        Exercise schedulePolledGroupCachingUpdate
-        &quot;&quot;&quot;
</del><ins>+    # @inlineCallbacks
+    # def testScheduling(self):
+    #     &quot;&quot;&quot;
+    #     Exercise schedulePolledGroupCachingUpdate
+    #     &quot;&quot;&quot;
</ins><span class="cx"> 
</span><del>-        groupCacher = StubGroupCacher()
</del><ins>+    #     groupCacher = StubGroupCacher()
</ins><span class="cx"> 
</span><span class="cx"> 
</span><del>-        def decorateTransaction(txn):
-            txn._groupCacher = groupCacher
</del><ins>+    #     def decorateTransaction(txn):
+    #         txn._groupCacher = groupCacher
</ins><span class="cx"> 
</span><del>-        store = yield buildStore(self, None)
-        store.callWithNewTransactions(decorateTransaction)
-        wp = (yield schedulePolledGroupCachingUpdate(store))
-        yield wp.whenExecuted()
-        self.assertTrue(groupCacher.called)
</del><ins>+    #     store = yield buildStore(self, None)
+    #     store.callWithNewTransactions(decorateTransaction)
+    #     wp = (yield schedulePolledGroupCachingUpdate(store))
+    #     yield wp.whenExecuted()
+    #     self.assertTrue(groupCacher.called)
</ins><span class="cx"> 
</span><del>-    testScheduling.skip = &quot;Fix WorkProposal to track delayed calls and cancel them&quot;
</del><ins>+    # testScheduling.skip = &quot;Fix WorkProposal to track delayed calls and cancel them&quot;
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavstdconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/stdconfig.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -735,6 +735,7 @@
</span><span class="cx">             &quot;AllowResourceAsOrganizer&quot;            : False, # Allow resources to be Organizers
</span><span class="cx">             &quot;AllowLocationWithoutOrganizer&quot;       : True, # Allow locations to have events without an Organizer
</span><span class="cx">             &quot;AllowResourceWithoutOrganizer&quot;       : True, # Allow resources to have events without an Organizer
</span><ins>+            &quot;AllowGroupAsAttendee&quot;                : False, # Allow groups to be Attendees
</ins><span class="cx">             &quot;TrackUnscheduledLocationData&quot;        : True, # Track who the last modifier of an unscheduled location event is
</span><span class="cx">             &quot;TrackUnscheduledResourceData&quot;        : True, # Track who the last modifier of an unscheduled resource event is
</span><span class="cx">             &quot;LimitFreeBusyAttendees&quot;              : 30, # Maximum number of attendees to request freebusy for
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql.py (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql.py        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -68,7 +68,7 @@
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema, splitSQLString
</span><span class="cx"> from txdav.common.icommondatastore import ConcurrentModification, \
</span><span class="cx">     RecordNotAllowedError, ExternalShareFailed, ShareNotAllowed, \
</span><del>-    IndexedSearchException
</del><ins>+    IndexedSearchException, NotFoundError
</ins><span class="cx"> from txdav.common.icommondatastore import HomeChildNameNotAllowedError, \
</span><span class="cx">     HomeChildNameAlreadyExistsError, NoSuchHomeChildError, \
</span><span class="cx">     ObjectResourceNameNotAllowedError, ObjectResourceNameAlreadyExistsError, \
</span><span class="lines">@@ -87,6 +87,7 @@
</span><span class="cx"> from zope.interface import implements, directlyProvides
</span><span class="cx"> 
</span><span class="cx"> from collections import namedtuple
</span><ins>+import datetime
</ins><span class="cx"> import itertools
</span><span class="cx"> import json
</span><span class="cx"> import sys
</span><span class="lines">@@ -107,6 +108,7 @@
</span><span class="cx">     EADDRESSBOOKTYPE: &quot;CardDAV&quot;,
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class CommonDataStore(Service, object):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Shared logic for SQL-based data stores, between calendar and addressbook
</span><span class="lines">@@ -692,8 +694,8 @@
</span><span class="cx">                        apn.RESOURCE_KEY: Parameter(&quot;resourceKey&quot;),
</span><span class="cx">                        apn.MODIFIED: Parameter(&quot;modified&quot;),
</span><span class="cx">                        apn.SUBSCRIBER_GUID: Parameter(&quot;subscriber&quot;),
</span><del>-                       apn.USER_AGENT : Parameter(&quot;userAgent&quot;),
-                       apn.IP_ADDR : Parameter(&quot;ipAddr&quot;)})
</del><ins>+                       apn.USER_AGENT: Parameter(&quot;userAgent&quot;),
+                       apn.IP_ADDR: Parameter(&quot;ipAddr&quot;)})
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classproperty
</span><span class="lines">@@ -702,7 +704,7 @@
</span><span class="cx">         return Update({apn.MODIFIED: Parameter(&quot;modified&quot;),
</span><span class="cx">                        apn.SUBSCRIBER_GUID: Parameter(&quot;subscriber&quot;),
</span><span class="cx">                        apn.USER_AGENT: Parameter(&quot;userAgent&quot;),
</span><del>-                       apn.IP_ADDR : Parameter(&quot;ipAddr&quot;)},
</del><ins>+                       apn.IP_ADDR: Parameter(&quot;ipAddr&quot;)},
</ins><span class="cx">                       Where=(apn.TOKEN == Parameter(&quot;token&quot;)).And(
</span><span class="cx">                              apn.RESOURCE_KEY == Parameter(&quot;resourceKey&quot;)))
</span><span class="cx"> 
</span><span class="lines">@@ -910,12 +912,683 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         @type olderThan: datetime
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        return self._purgeOldIMIPTokensQuery.on(self,
-            olderThan=olderThan)
</del><ins>+        return self._purgeOldIMIPTokensQuery.on(self, olderThan=olderThan)
</ins><span class="cx"> 
</span><span class="cx">     # End of IMIP
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    # Groups
+
+    @classproperty
+    def _addGroupQuery(cls):
+        gr = schema.GROUPS
+        return Insert({gr.NAME: Parameter(&quot;name&quot;),
+                       gr.GROUP_GUID: Parameter(&quot;groupGUID&quot;),
+                       gr.MEMBERSHIP_HASH: Parameter(&quot;membershipHash&quot;)},
+                       Return=gr.GROUP_ID)
+
+
+    @classproperty
+    def _updateGroupQuery(cls):
+        gr = schema.GROUPS
+        return Update({gr.MEMBERSHIP_HASH: Parameter(&quot;membershipHash&quot;),
+            gr.NAME: Parameter(&quot;name&quot;), gr.MODIFIED: Parameter(&quot;timestamp&quot;)},
+            Where=(gr.GROUP_GUID == Parameter(&quot;groupGUID&quot;)))
+
+
+    @classproperty
+    def _groupByGUID(cls):
+        gr = schema.GROUPS
+        return Select([gr.GROUP_ID, gr.NAME, gr.MEMBERSHIP_HASH], From=gr,
+                Where=(
+                    gr.GROUP_GUID == Parameter(&quot;groupGUID&quot;)
+                )
+            )
+
+
+    @classproperty
+    def _groupByID(cls):
+        gr = schema.GROUPS
+        return Select([gr.GROUP_GUID, gr.NAME, gr.MEMBERSHIP_HASH], From=gr,
+                Where=(
+                    gr.GROUP_ID == Parameter(&quot;groupID&quot;)
+                )
+            )
+
+
+    @classproperty
+    def _deleteGroup(cls):
+        gr = schema.GROUPS
+        return Delete(From=gr,
+              Where=(gr.GROUP_ID == Parameter(&quot;groupID&quot;)))
+
+
+    def addGroup(self, groupGUID, name, membershipHash):
+        &quot;&quot;&quot;
+        @type groupGUID: C{UUID}
+        &quot;&quot;&quot;
+        return self._addGroupQuery.on(self, name=name,
+            groupGUID=str(groupGUID), membershipHash=membershipHash)
+
+
+    def updateGroup(self, groupGUID, name, membershipHash):
+        &quot;&quot;&quot;
+        @type groupGUID: C{UUID}
+        &quot;&quot;&quot;
+        timestamp = datetime.datetime.utcnow()
+        return self._updateGroupQuery.on(self, name=name,
+            groupGUID=str(groupGUID), timestamp=timestamp,
+            membershipHash=membershipHash)
+
+
+    @inlineCallbacks
+    def groupByGUID(self, groupGUID):
+        &quot;&quot;&quot;
+        @type groupGUID: C{UUID}
+        &quot;&quot;&quot;
+        results = (yield self._groupByGUID.on(self, groupGUID=str(groupGUID)))
+        if results:
+            returnValue(results[0])
+        else:
+            savepoint = SavepointAction(&quot;groupByGUID&quot;)
+            yield savepoint.acquire(self)
+            try:
+                yield self.addGroup(groupGUID, &quot;&quot;, &quot;&quot;)
+            except Exception:
+                yield savepoint.rollback(self)
+                results = (yield self._groupByGUID.on(self,
+                    groupGUID=str(groupGUID)))
+                if results:
+                    returnValue(results[0])
+                else:
+                    raise
+            else:
+                yield savepoint.release(self)
+                results = (yield self._groupByGUID.on(self,
+                    groupGUID=str(groupGUID)))
+                if results:
+                    returnValue(results[0])
+                else:
+                    raise
+
+
+    @inlineCallbacks
+    def groupByID(self, groupID):
+        try:
+            results = (yield self._groupByID.on(self, groupID=groupID))[0]
+            if results:
+                results = [UUID(&quot;urn:uuid:&quot; + results[0])] + results[1:]
+            returnValue(results)
+        except IndexError:
+            raise NotFoundError
+
+
+    def deleteGroup(self, groupID):
+        return self._deleteGroup.on(self, groupID=groupID)
+
+    # End of Groups
+
+
+    # Group Members
+
+    @classproperty
+    def _addMemberToGroupQuery(cls):
+        gm = schema.GROUP_MEMBERSHIP
+        return Insert(
+            {
+                gm.GROUP_ID: Parameter(&quot;groupID&quot;),
+                gm.MEMBER_GUID: Parameter(&quot;memberGUID&quot;)
+            }
+        )
+
+
+    @classproperty
+    def _removeMemberFromGroupQuery(cls):
+        gm = schema.GROUP_MEMBERSHIP
+        return Delete(
+            From=gm,
+            Where=(
+                gm.GROUP_ID == Parameter(&quot;groupID&quot;)
+            ).And(
+                gm.MEMBER_GUID == Parameter(&quot;memberGUID&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectGroupMembersQuery(cls):
+        gm = schema.GROUP_MEMBERSHIP
+        return Select(
+            [gm.MEMBER_GUID],
+            From=gm,
+            Where=(
+                gm.GROUP_ID == Parameter(&quot;groupID&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectGroupsForQuery(cls):
+        gm = schema.GROUP_MEMBERSHIP
+        return Select(
+            [gm.GROUP_ID],
+            From=gm,
+            Where=(
+                gm.MEMBER_GUID == Parameter(&quot;guid&quot;)
+            )
+        )
+
+
+    def addMemberToGroup(self, memberGUID, groupID):
+        return self._addMemberToGroupQuery.on(
+            self, groupID=groupID, memberGUID=str(memberGUID)
+        )
+
+
+    def removeMemberFromGroup(self, memberGUID, groupID):
+        return self._removeMemberFromGroupQuery.on(
+            self, groupID=groupID, memberGUID=str(memberGUID)
+        )
+
+
+    @inlineCallbacks
+    def membersOfGroup(self, groupID):
+        &quot;&quot;&quot;
+        Returns the cached set of GUIDs for members of the given groupID.
+        Sub-groups are not returned in the results but their members are,
+        because the group membership has already been expanded/flattened
+        before storing in the db.
+
+        @param groupID: the group ID
+        @type groupID: C{int}
+        @return: the set of member GUIDs
+        @rtype: a Deferred which fires with a set() of C{str} GUIDs
+        &quot;&quot;&quot;
+        members = set()
+        results = (yield self._selectGroupMembersQuery.on(self, groupID=groupID))
+        for row in results:
+            members.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+        returnValue(members)
+
+
+    @inlineCallbacks
+    def groupsFor(self, guid):
+        &quot;&quot;&quot;
+        Returns the cached set of GUIDs for the groups this given guid is
+        a member of.
+
+        @param guid: the guid
+        @type guid: C{UUID}
+        @return: the set of group IDs
+        @rtype: a Deferred which fires with a set() of C{int} group IDs
+        &quot;&quot;&quot;
+        groups = set()
+        results = (yield self._selectGroupsForQuery.on(self, guid=str(guid)))
+        for row in results:
+            groups.add(row[0])
+        returnValue(groups)
+
+    # End of Group Members
+
+    # Delegates
+
+
+    @classproperty
+    def _addDelegateQuery(cls):
+        de = schema.DELEGATES
+        return Insert({de.DELEGATOR: Parameter(&quot;delegator&quot;),
+                       de.DELEGATE: Parameter(&quot;delegate&quot;),
+                       de.READ_WRITE: Parameter(&quot;readWrite&quot;),
+                       })
+
+
+    @classproperty
+    def _addDelegateGroupQuery(cls):
+        ds = schema.DELEGATE_GROUPS
+        return Insert({ds.DELEGATOR: Parameter(&quot;delegator&quot;),
+                       ds.GROUP_ID: Parameter(&quot;groupID&quot;),
+                       ds.READ_WRITE: Parameter(&quot;readWrite&quot;),
+                       ds.IS_EXTERNAL: Parameter(&quot;isExternal&quot;),
+                       })
+
+
+    @classproperty
+    def _removeDelegateQuery(cls):
+        de = schema.DELEGATES
+        return Delete(
+            From=de,
+            Where=(
+                de.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                de.DELEGATE == Parameter(&quot;delegate&quot;)
+            ).And(
+                de.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _removeDelegateGroupQuery(cls):
+        ds = schema.DELEGATE_GROUPS
+        return Delete(
+            From=ds,
+            Where=(
+                ds.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                ds.GROUP_ID == Parameter(&quot;groupID&quot;)
+            ).And(
+                ds.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectDelegatesQuery(cls):
+        de = schema.DELEGATES
+        return Select(
+            [de.DELEGATE],
+            From=de,
+            Where=(
+                de.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                de.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectDelegateGroupsQuery(cls):
+        ds = schema.DELEGATE_GROUPS
+        return Select(
+            [ds.GROUP_ID],
+            From=ds,
+            Where=(
+                ds.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                ds.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectDirectDelegatorsQuery(cls):
+        de = schema.DELEGATES
+        return Select(
+            [de.DELEGATOR],
+            From=de,
+            Where=(
+                de.DELEGATE == Parameter(&quot;delegate&quot;)
+            ).And(
+                de.READ_WRITE == Parameter(&quot;readWrite&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _selectIndirectDelegatorsQuery(cls):
+        dg = schema.DELEGATE_GROUPS
+        gm = schema.GROUP_MEMBERSHIP
+
+        return Select(
+            [dg.DELEGATOR],
+            From=dg,
+            Where=(
+                dg.GROUP_ID.In(
+                    Select(
+                        [gm.GROUP_ID],
+                        From=gm,
+                        Where=(gm.MEMBER_GUID == Parameter(&quot;delegate&quot;))
+                    )
+                ).And(
+                    dg.READ_WRITE == Parameter(&quot;readWrite&quot;)
+                )
+            )
+        )
+
+
+    @classproperty
+    def _selectIndirectDelegatesQuery(cls):
+        dg = schema.DELEGATE_GROUPS
+        gm = schema.GROUP_MEMBERSHIP
+
+        return Select(
+            [gm.MEMBER_GUID],
+            From=gm,
+            Where=(
+                gm.GROUP_ID.In(
+                    Select(
+                        [dg.GROUP_ID],
+                        From=dg,
+                        Where=(dg.DELEGATOR == Parameter(&quot;delegator&quot;)).And(
+                            dg.READ_WRITE == Parameter(&quot;readWrite&quot;))
+                    )
+                )
+            )
+        )
+
+
+    @classproperty
+    def _selectExternalDelegateGroupsQuery(cls):
+        edg = schema.EXTERNAL_DELEGATE_GROUPS
+        return Select(
+            [edg.DELEGATOR, edg.GROUP_GUID_READ, edg.GROUP_GUID_WRITE],
+            From=edg
+        )
+
+
+    @classproperty
+    def _removeExternalDelegateGroupsPairQuery(cls):
+        edg = schema.EXTERNAL_DELEGATE_GROUPS
+        return Delete(
+            From=edg,
+            Where=(
+                edg.DELEGATOR == Parameter(&quot;delegator&quot;)
+            )
+        )
+
+
+    @classproperty
+    def _storeExternalDelegateGroupsPairQuery(cls):
+        edg = schema.EXTERNAL_DELEGATE_GROUPS
+        return Insert(
+            {
+                edg.DELEGATOR: Parameter(&quot;delegator&quot;),
+                edg.GROUP_GUID_READ: Parameter(&quot;readDelegate&quot;),
+                edg.GROUP_GUID_WRITE: Parameter(&quot;writeDelegate&quot;),
+            }
+        )
+
+
+    @classproperty
+    def _removeExternalDelegateGroupsQuery(cls):
+        ds = schema.DELEGATE_GROUPS
+        return Delete(
+            From=ds,
+            Where=(
+                ds.DELEGATOR == Parameter(&quot;delegator&quot;)
+            ).And(
+                ds.IS_EXTERNAL == 1
+            )
+        )
+
+
+    def addDelegate(self, delegator, delegate, readWrite):
+        &quot;&quot;&quot;
+        Adds a row to the DELEGATES table.  The delegate should not be a
+        group.  To delegate to a group, call addDelegateGroup() instead.
+
+        @param delegator: the GUID of the delegator
+        @type delegator: C{UUID}
+        @param delegate: the GUID of the delegate
+        @type delegate: C{UUID}
+        @param readWrite: grant read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._addDelegateQuery.on(
+            self,
+            delegator=str(delegator),
+            delegate=str(delegate),
+            readWrite=1 if readWrite else 0
+        )
+
+
+    def addDelegateGroup(self, delegator, delegateGroupID, readWrite,
+                         isExternal=False):
+        &quot;&quot;&quot;
+        Adds a row to the DELEGATE_GROUPS table.  The delegate should be a
+        group.  To delegate to a person, call addDelegate() instead.
+
+        @param delegator: the GUID of the delegator
+        @type delegator: C{UUID}
+        @param delegateGroupID: the GROUP_ID of the delegate group
+        @type delegateGroupID: C{int}
+        @param readWrite: grant read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._addDelegateGroupQuery.on(
+            self,
+            delegator=str(delegator),
+            groupID=delegateGroupID,
+            readWrite=1 if readWrite else 0,
+            isExternal=1 if isExternal else 0
+        )
+
+
+    def removeDelegate(self, delegator, delegate, readWrite):
+        &quot;&quot;&quot;
+        Removes a row from the DELEGATES table.  The delegate should not be a
+        group.  To remove a delegate group, call removeDelegateGroup() instead.
+
+        @param delegator: the GUID of the delegator
+        @type delegator: C{UUID}
+        @param delegate: the GUID of the delegate
+        @type delegate: C{UUID}
+        @param readWrite: remove read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._removeDelegateQuery.on(
+            self,
+            delegator=str(delegator),
+            delegate=str(delegate),
+            readWrite=1 if readWrite else 0
+        )
+
+
+    def removeDelegateGroup(self, delegator, delegateGroupID, readWrite):
+        &quot;&quot;&quot;
+        Removes a row from the DELEGATE_GROUPS table.  The delegate should be a
+        group.  To remove a delegate person, call removeDelegate() instead.
+
+        @param delegator: the GUID of the delegator
+        @type delegator: C{UUID}
+        @param delegateGroupID: the GROUP_ID of the delegate group
+        @type delegateGroupID: C{int}
+        @param readWrite: remove read and write access if True, otherwise
+            read-only access
+        @type readWrite: C{boolean}
+        &quot;&quot;&quot;
+        return self._removeDelegateGroupQuery.on(
+            self,
+            delegator=str(delegator),
+            groupID=delegateGroupID,
+            readWrite=1 if readWrite else 0
+        )
+
+
+    @inlineCallbacks
+    def delegates(self, delegator, readWrite):
+        &quot;&quot;&quot;
+        Returns the GUIDs of all delegates for the given delegator.  If
+        delegate access was granted to any groups, those groups' members
+        (flattened) will be included. No GUIDs of the groups themselves
+        will be returned.
+
+        @param delegator: the GUID of the delegator
+        @type delegator: C{UUID}
+        @param readWrite: the access-type to check for; read and write
+            access if True, otherwise read-only access
+        @type readWrite: C{boolean}
+        @returns: the GUIDs of the delegates (for the specified access
+            type)
+        @rtype: a Deferred resulting in a set
+        &quot;&quot;&quot;
+        delegates = set()
+
+
+        # First get the direct delegates
+        results = (
+            yield self._selectDelegatesQuery.on(
+                self,
+                delegator=str(delegator),
+                readWrite=1 if readWrite else 0
+            )
+        )
+        for row in results:
+            delegates.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+
+        # Finally get those who are in groups which have been delegated to
+        results = (
+            yield self._selectIndirectDelegatesQuery.on(
+                self,
+                delegator=str(delegator),
+                readWrite=1 if readWrite else 0
+            )
+        )
+        for row in results:
+            delegates.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+
+        returnValue(delegates)
+
+
+    @inlineCallbacks
+    def delegators(self, delegate, readWrite):
+        &quot;&quot;&quot;
+        Returns the GUIDs of all delegators which have granted access to
+        the given delegate, either directly or indirectly via groups.
+
+        @param delegate: the GUID of the delegate
+        @type delegate: C{UUID}
+        @param readWrite: the access-type to check for; read and write
+            access if True, otherwise read-only access
+        @type readWrite: C{boolean}
+        @returns: the GUIDs of the delegators (for the specified access
+            type)
+        @rtype: a Deferred resulting in a set
+        &quot;&quot;&quot;
+        delegators = set()
+
+        # First get the direct delegators
+        results = (
+            yield self._selectDirectDelegatorsQuery.on(
+                self,
+                delegate=str(delegate),
+                readWrite=1 if readWrite else 0
+            )
+        )
+        for row in results:
+            delegators.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+
+        # Finally get those who have delegated to groups the delegate
+        # is a member of
+        results = (
+            yield self._selectIndirectDelegatorsQuery.on(
+                self,
+                delegate=str(delegate),
+                readWrite=1 if readWrite else 0
+            )
+        )
+        for row in results:
+            delegators.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+
+        returnValue(delegators)
+
+
+    @inlineCallbacks
+    def allGroupDelegates(self):
+        &quot;&quot;&quot;
+        Return the GUIDs of all groups which have been delegated to.  Useful
+        for obtaining the set of groups which need to be synchronized from
+        the directory.
+
+        @returns: the GUIDs of all delegated-to groups
+        @rtype: a Deferred resulting in a set
+        &quot;&quot;&quot;
+        gr = schema.GROUPS
+        dg = schema.DELEGATE_GROUPS
+
+        results = (yield Select(
+            [gr.GROUP_GUID],
+            From=gr,
+            Where=(gr.GROUP_ID.In(Select([dg.GROUP_ID], From=dg, Where=None)))
+        ).on(self))
+        delegates = set()
+        for row in results:
+            delegates.add(UUID(&quot;urn:uuid:&quot; + row[0]))
+
+        returnValue(delegates)
+
+
+    @inlineCallbacks
+    def externalDelegates(self):
+        &quot;&quot;&quot;
+        Returns a dictionary mapping delegate GUIDs to (read-group, write-group)
+        tuples, including only those assignments that originated from the
+        directory.
+
+        @returns: dictionary mapping delegator guid to (readDelegateGUID,
+            writeDelegateGUID) tuples
+        @rtype: a Deferred resulting in a dictionary
+        &quot;&quot;&quot;
+        delegates = {}
+
+        # Get the externally managed delegates (which are all groups)
+        results = (yield self._selectExternalDelegateGroupsQuery.on(self))
+        for delegator, readDelegateGUID, writeDelegateGUID in results:
+            delegates[UUID(delegator)] = (
+                UUID(readDelegateGUID) if readDelegateGUID else None,
+                UUID(writeDelegateGUID) if writeDelegateGUID else None
+            )
+
+        returnValue(delegates)
+
+
+    @inlineCallbacks
+    def assignExternalDelegates(
+        self, delegator, readDelegateGroupID, writeDelegateGroupID,
+        readDelegateGUID, writeDelegateGUID
+    ):
+        &quot;&quot;&quot;
+        Update the external delegate group table so we can quickly identify
+        diffs next time, and update the delegate group table itself
+
+        @param delegator
+        @type delegator: C{UUID}
+        &quot;&quot;&quot;
+
+        # Delete existing external assignments for the delegator
+        yield self._removeExternalDelegateGroupsQuery.on(
+            self,
+            delegator=str(delegator)
+        )
+
+        # Remove from the external comparison table
+        yield self._removeExternalDelegateGroupsPairQuery.on(
+            self,
+            delegator=str(delegator)
+        )
+
+        # Store new assignments in the external comparison table
+        if readDelegateGUID or writeDelegateGUID:
+            readDelegateForDB = (
+                str(readDelegateGUID) if readDelegateGUID else &quot;&quot;
+            )
+            writeDelegateForDB = (
+                str(writeDelegateGUID) if writeDelegateGUID else &quot;&quot;
+            )
+            yield self._storeExternalDelegateGroupsPairQuery.on(
+                self,
+                delegator=str(delegator),
+                readDelegate=readDelegateForDB,
+                writeDelegate=writeDelegateForDB
+            )
+
+        # Apply new assignments
+        if readDelegateGroupID is not None:
+            yield self.addDelegateGroup(
+                delegator, readDelegateGroupID, False, isExternal=True
+            )
+        if writeDelegateGroupID is not None:
+            yield self.addDelegateGroup(
+                delegator, writeDelegateGroupID, True, isExternal=True
+            )
+
+
+    # End of Delegates
+
+
</ins><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="CalendarServertrunktxdavcommondatastoresql_schemacurrentoracledialectsql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -379,6 +379,59 @@
</span><span class="cx">     &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create table GROUP_REFRESH_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;GROUP_GUID&quot; nvarchar2(255)
+);
+
+create table GROUP_ATTENDEE_RECONCILIATION_ (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;RESOURCE_ID&quot; integer,
+    &quot;GROUP_ID&quot; integer
+);
+
+create table GROUPS (
+    &quot;GROUP_ID&quot; integer primary key,
+    &quot;NAME&quot; nvarchar2(255),
+    &quot;GROUP_GUID&quot; nvarchar2(255),
+    &quot;MEMBERSHIP_HASH&quot; nvarchar2(255),
+    &quot;EXTANT&quot; integer default 1,
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table GROUP_MEMBERSHIP (
+    &quot;GROUP_ID&quot; integer,
+    &quot;MEMBER_GUID&quot; nvarchar2(255)
+);
+
+create table GROUP_ATTENDEE (
+    &quot;GROUP_ID&quot; integer,
+    &quot;RESOURCE_ID&quot; integer,
+    &quot;MEMBERSHIP_HASH&quot; nvarchar2(255)
+);
+
+create table DELEGATES (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;DELEGATE&quot; nvarchar2(255),
+    &quot;READ_WRITE&quot; integer not null
+);
+
+create table DELEGATE_GROUPS (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;GROUP_ID&quot; integer not null,
+    &quot;READ_WRITE&quot; integer not null,
+    &quot;IS_EXTERNAL&quot; integer not null
+);
+
+create table EXTERNAL_DELEGATE_GROUPS (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;GROUP_GUID_READ&quot; nvarchar2(255),
+    &quot;GROUP_GUID_WRITE&quot; nvarchar2(255)
+);
+
</ins><span class="cx"> create table CALENDAR_OBJECT_SPLITTER_WORK (
</span><span class="cx">     &quot;WORK_ID&quot; integer primary key not null,
</span><span class="cx">     &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</span><span class="lines">@@ -462,7 +515,7 @@
</span><span class="cx">     &quot;VALUE&quot; nvarchar2(255)
</span><span class="cx"> );
</span><span class="cx"> 
</span><del>-insert into CALENDARSERVER (NAME, VALUE) values ('VERSION', '34');
</del><ins>+insert into CALENDARSERVER (NAME, VALUE) values ('VERSION', '35');
</ins><span class="cx"> insert into CALENDARSERVER (NAME, VALUE) values ('CALENDAR-DATAVERSION', '5');
</span><span class="cx"> insert into CALENDARSERVER (NAME, VALUE) values ('ADDRESSBOOK-DATAVERSION', '2');
</span><span class="cx"> insert into CALENDARSERVER (NAME, VALUE) values ('NOTIFICATION-DATAVERSION', '1');
</span><span class="lines">@@ -596,6 +649,18 @@
</span><span class="cx">     TOKEN
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create index GROUPS_GROUP_GUID_ebf7a1d4 on GROUPS (
+    GROUP_GUID
+);
+
+create index GROUP_MEMBERSHIP_GROU_9560a5e6 on GROUP_MEMBERSHIP (
+    GROUP_ID
+);
+
+create index GROUP_MEMBERSHIP_MEMB_0ca508e8 on GROUP_MEMBERSHIP (
+    MEMBER_GUID
+);
+
</ins><span class="cx"> create index CALENDAR_OBJECT_SPLIT_af71dcda on CALENDAR_OBJECT_SPLITTER_WORK (
</span><span class="cx">     RESOURCE_ID
</span><span class="cx"> );
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemacurrentsql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql (12662 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2014-02-12 00:26:02 UTC (rev 12662)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -522,7 +522,7 @@
</span><span class="cx"> 
</span><span class="cx"> -- Joins ADDRESSBOOK_HOME and ADDRESSBOOK_OBJECT (kind == group)
</span><span class="cx"> 
</span><del>-create table SHARED_GROUP_BIND (        
</del><ins>+create table SHARED_GROUP_BIND (
</ins><span class="cx">   ADDRESSBOOK_HOME_RESOURCE_ID                 integer      not null references ADDRESSBOOK_HOME,
</span><span class="cx">   GROUP_RESOURCE_ID                              integer      not null references ADDRESSBOOK_OBJECT on delete cascade,
</span><span class="cx">   EXTERNAL_ID                                    integer      default null,
</span><span class="lines">@@ -632,7 +632,7 @@
</span><span class="cx"> create index APN_SUBSCRIPTIONS_RESOURCE_KEY
</span><span class="cx">    on APN_SUBSCRIPTIONS(RESOURCE_KEY);
</span><span class="cx"> 
</span><del>-   
</del><ins>+
</ins><span class="cx"> -----------------
</span><span class="cx"> -- IMIP Tokens --
</span><span class="cx"> -----------------
</span><span class="lines">@@ -650,7 +650,7 @@
</span><span class="cx"> create index IMIP_TOKENS_TOKEN
</span><span class="cx">    on IMIP_TOKENS(TOKEN);
</span><span class="cx"> 
</span><del>-   
</del><ins>+
</ins><span class="cx"> ----------------
</span><span class="cx"> -- Work Items --
</span><span class="cx"> ----------------
</span><span class="lines">@@ -714,7 +714,67 @@
</span><span class="cx">   NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create table GROUP_REFRESH_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  GROUP_GUID                    varchar(255) not null
+);
</ins><span class="cx"> 
</span><ins>+create table GROUP_ATTENDEE_RECONCILIATION_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  RESOURCE_ID                   integer,
+  GROUP_ID                      integer
+);
+
+
+create table GROUPS (
+  GROUP_ID                      integer      primary key default nextval('RESOURCE_ID_SEQ'),    -- implicit index
+  NAME                          varchar(255) not null,
+  GROUP_GUID                    varchar(255) not null,
+  MEMBERSHIP_HASH               varchar(255) not null,
+  EXTANT                        integer default 1,
+  CREATED                       timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                      timestamp default timezone('UTC', CURRENT_TIMESTAMP)
+);
+create index GROUPS_GROUP_GUID on GROUPS(GROUP_GUID);
+
+create table GROUP_MEMBERSHIP (
+  GROUP_ID                      integer,
+  MEMBER_GUID                   varchar(255) not null
+);
+create index GROUP_MEMBERSHIP_GROUP on GROUP_MEMBERSHIP(GROUP_ID);
+create index GROUP_MEMBERSHIP_MEMBER on GROUP_MEMBERSHIP(MEMBER_GUID);
+
+create table GROUP_ATTENDEE (
+  GROUP_ID                      integer,
+  RESOURCE_ID                   integer,
+  MEMBERSHIP_HASH               varchar(255) not null
+);
+
+---------------
+-- Delegates --
+---------------
+
+create table DELEGATES (
+  DELEGATOR                     varchar(255) not null,
+  DELEGATE                      varchar(255) not null,
+  READ_WRITE                    integer      not null -- 1 = ReadWrite, 0 = ReadOnly
+);
+
+create table DELEGATE_GROUPS (
+  DELEGATOR                     varchar(255) not null,
+  GROUP_ID                      integer      not null,
+  READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+  IS_EXTERNAL                   integer      not null -- 1 = ReadWrite, 0 = ReadOnly
+);
+
+create table EXTERNAL_DELEGATE_GROUPS (
+  DELEGATOR                     varchar(255) not null,
+  GROUP_GUID_READ               varchar(255),
+  GROUP_GUID_WRITE              varchar(255)
+);
+
</ins><span class="cx"> --------------------------
</span><span class="cx"> -- Object Splitter Work --
</span><span class="cx"> --------------------------
</span><span class="lines">@@ -862,7 +922,7 @@
</span><span class="cx">   VALUE                         varchar(255)
</span><span class="cx"> );
</span><span class="cx"> 
</span><del>-insert into CALENDARSERVER values ('VERSION', '34');
</del><ins>+insert into CALENDARSERVER values ('VERSION', '35');
</ins><span class="cx"> insert into CALENDARSERVER values ('CALENDAR-DATAVERSION', '5');
</span><span class="cx"> insert into CALENDARSERVER values ('ADDRESSBOOK-DATAVERSION', '2');
</span><span class="cx"> insert into CALENDARSERVER values ('NOTIFICATION-DATAVERSION', '1');
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaoldoracledialectv34sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/old/oracle-dialect/v34.sql (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/old/oracle-dialect/v34.sql                                (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/old/oracle-dialect/v34.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,643 @@
</span><ins>+create sequence RESOURCE_ID_SEQ;
+create sequence INSTANCE_ID_SEQ;
+create sequence ATTACHMENT_ID_SEQ;
+create sequence REVISION_SEQ;
+create sequence WORKITEM_SEQ;
+create table NODE_INFO (
+    &quot;HOSTNAME&quot; nvarchar2(255),
+    &quot;PID&quot; integer not null,
+    &quot;PORT&quot; integer not null,
+    &quot;TIME&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC' not null, 
+    primary key(&quot;HOSTNAME&quot;, &quot;PORT&quot;)
+);
+
+create table NAMED_LOCK (
+    &quot;LOCK_NAME&quot; nvarchar2(255) primary key
+);
+
+create table CALENDAR_HOME (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;OWNER_UID&quot; nvarchar2(255) unique,
+    &quot;STATUS&quot; integer default 0 not null,
+    &quot;DATAVERSION&quot; integer default 0 not null
+);
+
+create table HOME_STATUS (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into HOME_STATUS (DESCRIPTION, ID) values ('normal', 0);
+insert into HOME_STATUS (DESCRIPTION, ID) values ('external', 1);
+create table CALENDAR (
+    &quot;RESOURCE_ID&quot; integer primary key
+);
+
+create table CALENDAR_HOME_METADATA (
+    &quot;RESOURCE_ID&quot; integer primary key references CALENDAR_HOME on delete cascade,
+    &quot;QUOTA_USED_BYTES&quot; integer default 0 not null,
+    &quot;DEFAULT_EVENTS&quot; integer default null references CALENDAR on delete set null,
+    &quot;DEFAULT_TASKS&quot; integer default null references CALENDAR on delete set null,
+    &quot;DEFAULT_POLLS&quot; integer default null references CALENDAR on delete set null,
+    &quot;ALARM_VEVENT_TIMED&quot; nclob default null,
+    &quot;ALARM_VEVENT_ALLDAY&quot; nclob default null,
+    &quot;ALARM_VTODO_TIMED&quot; nclob default null,
+    &quot;ALARM_VTODO_ALLDAY&quot; nclob default null,
+    &quot;AVAILABILITY&quot; nclob default null,
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table CALENDAR_METADATA (
+    &quot;RESOURCE_ID&quot; integer primary key references CALENDAR on delete cascade,
+    &quot;SUPPORTED_COMPONENTS&quot; nvarchar2(255) default null,
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table NOTIFICATION_HOME (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;OWNER_UID&quot; nvarchar2(255) unique,
+    &quot;STATUS&quot; integer default 0 not null,
+    &quot;DATAVERSION&quot; integer default 0 not null
+);
+
+create table NOTIFICATION (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;NOTIFICATION_HOME_RESOURCE_ID&quot; integer not null references NOTIFICATION_HOME,
+    &quot;NOTIFICATION_UID&quot; nvarchar2(255),
+    &quot;NOTIFICATION_TYPE&quot; nvarchar2(255),
+    &quot;NOTIFICATION_DATA&quot; nclob,
+    &quot;MD5&quot; nchar(32),
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    unique(&quot;NOTIFICATION_UID&quot;, &quot;NOTIFICATION_HOME_RESOURCE_ID&quot;)
+);
+
+create table CALENDAR_BIND (
+    &quot;CALENDAR_HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME,
+    &quot;CALENDAR_RESOURCE_ID&quot; integer not null references CALENDAR on delete cascade,
+    &quot;EXTERNAL_ID&quot; integer default null,
+    &quot;CALENDAR_RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;BIND_MODE&quot; integer not null,
+    &quot;BIND_STATUS&quot; integer not null,
+    &quot;BIND_REVISION&quot; integer default 0 not null,
+    &quot;MESSAGE&quot; nclob,
+    &quot;TRANSP&quot; integer default 0 not null,
+    &quot;ALARM_VEVENT_TIMED&quot; nclob default null,
+    &quot;ALARM_VEVENT_ALLDAY&quot; nclob default null,
+    &quot;ALARM_VTODO_TIMED&quot; nclob default null,
+    &quot;ALARM_VTODO_ALLDAY&quot; nclob default null,
+    &quot;TIMEZONE&quot; nclob default null, 
+    primary key(&quot;CALENDAR_HOME_RESOURCE_ID&quot;, &quot;CALENDAR_RESOURCE_ID&quot;), 
+    unique(&quot;CALENDAR_HOME_RESOURCE_ID&quot;, &quot;CALENDAR_RESOURCE_NAME&quot;)
+);
+
+create table CALENDAR_BIND_MODE (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into CALENDAR_BIND_MODE (DESCRIPTION, ID) values ('own', 0);
+insert into CALENDAR_BIND_MODE (DESCRIPTION, ID) values ('read', 1);
+insert into CALENDAR_BIND_MODE (DESCRIPTION, ID) values ('write', 2);
+insert into CALENDAR_BIND_MODE (DESCRIPTION, ID) values ('direct', 3);
+insert into CALENDAR_BIND_MODE (DESCRIPTION, ID) values ('indirect', 4);
+create table CALENDAR_BIND_STATUS (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into CALENDAR_BIND_STATUS (DESCRIPTION, ID) values ('invited', 0);
+insert into CALENDAR_BIND_STATUS (DESCRIPTION, ID) values ('accepted', 1);
+insert into CALENDAR_BIND_STATUS (DESCRIPTION, ID) values ('declined', 2);
+insert into CALENDAR_BIND_STATUS (DESCRIPTION, ID) values ('invalid', 3);
+insert into CALENDAR_BIND_STATUS (DESCRIPTION, ID) values ('deleted', 4);
+create table CALENDAR_TRANSP (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into CALENDAR_TRANSP (DESCRIPTION, ID) values ('opaque', 0);
+insert into CALENDAR_TRANSP (DESCRIPTION, ID) values ('transparent', 1);
+create table CALENDAR_OBJECT (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;CALENDAR_RESOURCE_ID&quot; integer not null references CALENDAR on delete cascade,
+    &quot;RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;ICALENDAR_TEXT&quot; nclob,
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;ICALENDAR_TYPE&quot; nvarchar2(255),
+    &quot;ATTACHMENTS_MODE&quot; integer default 0 not null,
+    &quot;DROPBOX_ID&quot; nvarchar2(255),
+    &quot;ORGANIZER&quot; nvarchar2(255),
+    &quot;RECURRANCE_MIN&quot; date,
+    &quot;RECURRANCE_MAX&quot; date,
+    &quot;ACCESS&quot; integer default 0 not null,
+    &quot;SCHEDULE_OBJECT&quot; integer default 0,
+    &quot;SCHEDULE_TAG&quot; nvarchar2(36) default null,
+    &quot;SCHEDULE_ETAGS&quot; nclob default null,
+    &quot;PRIVATE_COMMENTS&quot; integer default 0 not null,
+    &quot;MD5&quot; nchar(32),
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    unique(&quot;CALENDAR_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;)
+);
+
+create table CALENDAR_OBJECT_ATTACHMENTS_MO (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into CALENDAR_OBJECT_ATTACHMENTS_MO (DESCRIPTION, ID) values ('none', 0);
+insert into CALENDAR_OBJECT_ATTACHMENTS_MO (DESCRIPTION, ID) values ('read', 1);
+insert into CALENDAR_OBJECT_ATTACHMENTS_MO (DESCRIPTION, ID) values ('write', 2);
+create table CALENDAR_ACCESS_TYPE (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(32) unique
+);
+
+insert into CALENDAR_ACCESS_TYPE (DESCRIPTION, ID) values ('', 0);
+insert into CALENDAR_ACCESS_TYPE (DESCRIPTION, ID) values ('public', 1);
+insert into CALENDAR_ACCESS_TYPE (DESCRIPTION, ID) values ('private', 2);
+insert into CALENDAR_ACCESS_TYPE (DESCRIPTION, ID) values ('confidential', 3);
+insert into CALENDAR_ACCESS_TYPE (DESCRIPTION, ID) values ('restricted', 4);
+create table TIME_RANGE (
+    &quot;INSTANCE_ID&quot; integer primary key,
+    &quot;CALENDAR_RESOURCE_ID&quot; integer not null references CALENDAR on delete cascade,
+    &quot;CALENDAR_OBJECT_RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade,
+    &quot;FLOATING&quot; integer not null,
+    &quot;START_DATE&quot; timestamp not null,
+    &quot;END_DATE&quot; timestamp not null,
+    &quot;FBTYPE&quot; integer not null,
+    &quot;TRANSPARENT&quot; integer not null
+);
+
+create table FREE_BUSY_TYPE (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into FREE_BUSY_TYPE (DESCRIPTION, ID) values ('unknown', 0);
+insert into FREE_BUSY_TYPE (DESCRIPTION, ID) values ('free', 1);
+insert into FREE_BUSY_TYPE (DESCRIPTION, ID) values ('busy', 2);
+insert into FREE_BUSY_TYPE (DESCRIPTION, ID) values ('busy-unavailable', 3);
+insert into FREE_BUSY_TYPE (DESCRIPTION, ID) values ('busy-tentative', 4);
+create table TRANSPARENCY (
+    &quot;TIME_RANGE_INSTANCE_ID&quot; integer not null references TIME_RANGE on delete cascade,
+    &quot;USER_ID&quot; nvarchar2(255),
+    &quot;TRANSPARENT&quot; integer not null
+);
+
+create table ATTACHMENT (
+    &quot;ATTACHMENT_ID&quot; integer primary key,
+    &quot;CALENDAR_HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME,
+    &quot;DROPBOX_ID&quot; nvarchar2(255),
+    &quot;CONTENT_TYPE&quot; nvarchar2(255),
+    &quot;SIZE&quot; integer not null,
+    &quot;MD5&quot; nchar(32),
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;PATH&quot; nvarchar2(1024)
+);
+
+create table ATTACHMENT_CALENDAR_OBJECT (
+    &quot;ATTACHMENT_ID&quot; integer not null references ATTACHMENT on delete cascade,
+    &quot;MANAGED_ID&quot; nvarchar2(255),
+    &quot;CALENDAR_OBJECT_RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade, 
+    primary key(&quot;ATTACHMENT_ID&quot;, &quot;CALENDAR_OBJECT_RESOURCE_ID&quot;), 
+    unique(&quot;MANAGED_ID&quot;, &quot;CALENDAR_OBJECT_RESOURCE_ID&quot;)
+);
+
+create table RESOURCE_PROPERTY (
+    &quot;RESOURCE_ID&quot; integer not null,
+    &quot;NAME&quot; nvarchar2(255),
+    &quot;VALUE&quot; nclob,
+    &quot;VIEWER_UID&quot; nvarchar2(255), 
+    primary key(&quot;RESOURCE_ID&quot;, &quot;NAME&quot;, &quot;VIEWER_UID&quot;)
+);
+
+create table ADDRESSBOOK_HOME (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;ADDRESSBOOK_PROPERTY_STORE_ID&quot; integer not null,
+    &quot;OWNER_UID&quot; nvarchar2(255) unique,
+    &quot;STATUS&quot; integer default 0 not null,
+    &quot;DATAVERSION&quot; integer default 0 not null
+);
+
+create table ADDRESSBOOK_HOME_METADATA (
+    &quot;RESOURCE_ID&quot; integer primary key references ADDRESSBOOK_HOME on delete cascade,
+    &quot;QUOTA_USED_BYTES&quot; integer default 0 not null,
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table SHARED_ADDRESSBOOK_BIND (
+    &quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_HOME,
+    &quot;OWNER_HOME_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_HOME on delete cascade,
+    &quot;EXTERNAL_ID&quot; integer default null,
+    &quot;ADDRESSBOOK_RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;BIND_MODE&quot; integer not null,
+    &quot;BIND_STATUS&quot; integer not null,
+    &quot;BIND_REVISION&quot; integer default 0 not null,
+    &quot;MESSAGE&quot; nclob, 
+    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;OWNER_HOME_RESOURCE_ID&quot;), 
+    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;ADDRESSBOOK_RESOURCE_NAME&quot;)
+);
+
+create table ADDRESSBOOK_OBJECT (
+    &quot;RESOURCE_ID&quot; integer primary key,
+    &quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_HOME on delete cascade,
+    &quot;RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;VCARD_TEXT&quot; nclob,
+    &quot;VCARD_UID&quot; nvarchar2(255),
+    &quot;KIND&quot; integer not null,
+    &quot;MD5&quot; nchar(32),
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;), 
+    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;VCARD_UID&quot;)
+);
+
+create table ADDRESSBOOK_OBJECT_KIND (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into ADDRESSBOOK_OBJECT_KIND (DESCRIPTION, ID) values ('person', 0);
+insert into ADDRESSBOOK_OBJECT_KIND (DESCRIPTION, ID) values ('group', 1);
+insert into ADDRESSBOOK_OBJECT_KIND (DESCRIPTION, ID) values ('resource', 2);
+insert into ADDRESSBOOK_OBJECT_KIND (DESCRIPTION, ID) values ('location', 3);
+create table ABO_MEMBERS (
+    &quot;GROUP_ID&quot; integer not null,
+    &quot;ADDRESSBOOK_ID&quot; integer not null references ADDRESSBOOK_HOME on delete cascade,
+    &quot;MEMBER_ID&quot; integer not null,
+    &quot;REVISION&quot; integer not null,
+    &quot;REMOVED&quot; integer default 0 not null,
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    primary key(&quot;GROUP_ID&quot;, &quot;MEMBER_ID&quot;, &quot;REVISION&quot;)
+);
+
+create table ABO_FOREIGN_MEMBERS (
+    &quot;GROUP_ID&quot; integer not null references ADDRESSBOOK_OBJECT on delete cascade,
+    &quot;ADDRESSBOOK_ID&quot; integer not null references ADDRESSBOOK_HOME on delete cascade,
+    &quot;MEMBER_ADDRESS&quot; nvarchar2(255), 
+    primary key(&quot;GROUP_ID&quot;, &quot;MEMBER_ADDRESS&quot;)
+);
+
+create table SHARED_GROUP_BIND (
+    &quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_HOME,
+    &quot;GROUP_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_OBJECT on delete cascade,
+    &quot;EXTERNAL_ID&quot; integer default null,
+    &quot;GROUP_ADDRESSBOOK_NAME&quot; nvarchar2(255),
+    &quot;BIND_MODE&quot; integer not null,
+    &quot;BIND_STATUS&quot; integer not null,
+    &quot;BIND_REVISION&quot; integer default 0 not null,
+    &quot;MESSAGE&quot; nclob, 
+    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;GROUP_RESOURCE_ID&quot;), 
+    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;GROUP_ADDRESSBOOK_NAME&quot;)
+);
+
+create table CALENDAR_OBJECT_REVISIONS (
+    &quot;CALENDAR_HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME,
+    &quot;CALENDAR_RESOURCE_ID&quot; integer references CALENDAR,
+    &quot;CALENDAR_NAME&quot; nvarchar2(255) default null,
+    &quot;RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;REVISION&quot; integer not null,
+    &quot;DELETED&quot; integer not null,
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table ADDRESSBOOK_OBJECT_REVISIONS (
+    &quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot; integer not null references ADDRESSBOOK_HOME,
+    &quot;OWNER_HOME_RESOURCE_ID&quot; integer references ADDRESSBOOK_HOME,
+    &quot;ADDRESSBOOK_NAME&quot; nvarchar2(255) default null,
+    &quot;OBJECT_RESOURCE_ID&quot; integer default 0,
+    &quot;RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;REVISION&quot; integer not null,
+    &quot;DELETED&quot; integer not null,
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table NOTIFICATION_OBJECT_REVISIONS (
+    &quot;NOTIFICATION_HOME_RESOURCE_ID&quot; integer not null references NOTIFICATION_HOME on delete cascade,
+    &quot;RESOURCE_NAME&quot; nvarchar2(255),
+    &quot;REVISION&quot; integer not null,
+    &quot;DELETED&quot; integer not null,
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    unique(&quot;NOTIFICATION_HOME_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;)
+);
+
+create table APN_SUBSCRIPTIONS (
+    &quot;TOKEN&quot; nvarchar2(255),
+    &quot;RESOURCE_KEY&quot; nvarchar2(255),
+    &quot;MODIFIED&quot; integer not null,
+    &quot;SUBSCRIBER_GUID&quot; nvarchar2(255),
+    &quot;USER_AGENT&quot; nvarchar2(255) default null,
+    &quot;IP_ADDR&quot; nvarchar2(255) default null, 
+    primary key(&quot;TOKEN&quot;, &quot;RESOURCE_KEY&quot;)
+);
+
+create table IMIP_TOKENS (
+    &quot;TOKEN&quot; nvarchar2(255),
+    &quot;ORGANIZER&quot; nvarchar2(255),
+    &quot;ATTENDEE&quot; nvarchar2(255),
+    &quot;ICALUID&quot; nvarchar2(255),
+    &quot;ACCESSED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    primary key(&quot;ORGANIZER&quot;, &quot;ATTENDEE&quot;, &quot;ICALUID&quot;)
+);
+
+create table IMIP_INVITATION_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;FROM_ADDR&quot; nvarchar2(255),
+    &quot;TO_ADDR&quot; nvarchar2(255),
+    &quot;ICALENDAR_TEXT&quot; nclob
+);
+
+create table IMIP_POLLING_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table IMIP_REPLY_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ORGANIZER&quot; nvarchar2(255),
+    &quot;ATTENDEE&quot; nvarchar2(255),
+    &quot;ICALENDAR_TEXT&quot; nclob
+);
+
+create table PUSH_NOTIFICATION_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;PUSH_ID&quot; nvarchar2(255),
+    &quot;PRIORITY&quot; integer not null
+);
+
+create table GROUP_CACHER_POLLING_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table CALENDAR_OBJECT_SPLITTER_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade
+);
+
+create table FIND_MIN_VALID_REVISION_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table REVISION_CLEANUP_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table SCHEDULE_REFRESH_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade,
+    &quot;RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade,
+    &quot;ATTENDEE_COUNT&quot; integer
+);
+
+create table SCHEDULE_REFRESH_ATTENDEES (
+    &quot;RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade,
+    &quot;ATTENDEE&quot; nvarchar2(255)
+);
+
+create table SCHEDULE_AUTO_REPLY_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade,
+    &quot;RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade,
+    &quot;PARTSTAT&quot; nvarchar2(255)
+);
+
+create table SCHEDULE_ORGANIZER_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;SCHEDULE_ACTION&quot; integer not null,
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade,
+    &quot;RESOURCE_ID&quot; integer,
+    &quot;ICALENDAR_TEXT_OLD&quot; nclob,
+    &quot;ICALENDAR_TEXT_NEW&quot; nclob,
+    &quot;ATTENDEE_COUNT&quot; integer,
+    &quot;SMART_MERGE&quot; integer
+);
+
+create table SCHEDULE_ACTION (
+    &quot;ID&quot; integer primary key,
+    &quot;DESCRIPTION&quot; nvarchar2(16) unique
+);
+
+insert into SCHEDULE_ACTION (DESCRIPTION, ID) values ('create', 0);
+insert into SCHEDULE_ACTION (DESCRIPTION, ID) values ('modify', 1);
+insert into SCHEDULE_ACTION (DESCRIPTION, ID) values ('modify-cancelled', 2);
+insert into SCHEDULE_ACTION (DESCRIPTION, ID) values ('remove', 3);
+create table SCHEDULE_REPLY_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade,
+    &quot;RESOURCE_ID&quot; integer not null references CALENDAR_OBJECT on delete cascade,
+    &quot;CHANGED_RIDS&quot; nclob
+);
+
+create table SCHEDULE_REPLY_CANCEL_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;ICALENDAR_UID&quot; nvarchar2(255),
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade,
+    &quot;ICALENDAR_TEXT&quot; nclob
+);
+
+create table CALENDARSERVER (
+    &quot;NAME&quot; nvarchar2(255) primary key,
+    &quot;VALUE&quot; nvarchar2(255)
+);
+
+insert into CALENDARSERVER (NAME, VALUE) values ('VERSION', '34');
+insert into CALENDARSERVER (NAME, VALUE) values ('CALENDAR-DATAVERSION', '5');
+insert into CALENDARSERVER (NAME, VALUE) values ('ADDRESSBOOK-DATAVERSION', '2');
+insert into CALENDARSERVER (NAME, VALUE) values ('NOTIFICATION-DATAVERSION', '1');
+insert into CALENDARSERVER (NAME, VALUE) values ('MIN-VALID-REVISION', '1');
+create index CALENDAR_HOME_METADAT_3cb9049e on CALENDAR_HOME_METADATA (
+    DEFAULT_EVENTS
+);
+
+create index CALENDAR_HOME_METADAT_d55e5548 on CALENDAR_HOME_METADATA (
+    DEFAULT_TASKS
+);
+
+create index CALENDAR_HOME_METADAT_910264ce on CALENDAR_HOME_METADATA (
+    DEFAULT_POLLS
+);
+
+create index NOTIFICATION_NOTIFICA_f891f5f9 on NOTIFICATION (
+    NOTIFICATION_HOME_RESOURCE_ID
+);
+
+create index CALENDAR_BIND_RESOURC_e57964d4 on CALENDAR_BIND (
+    CALENDAR_RESOURCE_ID
+);
+
+create index CALENDAR_OBJECT_CALEN_a9a453a9 on CALENDAR_OBJECT (
+    CALENDAR_RESOURCE_ID,
+    ICALENDAR_UID
+);
+
+create index CALENDAR_OBJECT_CALEN_96e83b73 on CALENDAR_OBJECT (
+    CALENDAR_RESOURCE_ID,
+    RECURRANCE_MAX
+);
+
+create index CALENDAR_OBJECT_ICALE_82e731d5 on CALENDAR_OBJECT (
+    ICALENDAR_UID
+);
+
+create index CALENDAR_OBJECT_DROPB_de041d80 on CALENDAR_OBJECT (
+    DROPBOX_ID
+);
+
+create index TIME_RANGE_CALENDAR_R_beb6e7eb on TIME_RANGE (
+    CALENDAR_RESOURCE_ID
+);
+
+create index TIME_RANGE_CALENDAR_O_acf37bd1 on TIME_RANGE (
+    CALENDAR_OBJECT_RESOURCE_ID
+);
+
+create index TRANSPARENCY_TIME_RAN_5f34467f on TRANSPARENCY (
+    TIME_RANGE_INSTANCE_ID
+);
+
+create index ATTACHMENT_CALENDAR_H_0078845c on ATTACHMENT (
+    CALENDAR_HOME_RESOURCE_ID
+);
+
+create index ATTACHMENT_DROPBOX_ID_5073cf23 on ATTACHMENT (
+    DROPBOX_ID
+);
+
+create index ATTACHMENT_CALENDAR_O_81508484 on ATTACHMENT_CALENDAR_OBJECT (
+    CALENDAR_OBJECT_RESOURCE_ID
+);
+
+create index SHARED_ADDRESSBOOK_BI_e9a2e6d4 on SHARED_ADDRESSBOOK_BIND (
+    OWNER_HOME_RESOURCE_ID
+);
+
+create index ABO_MEMBERS_ADDRESSBO_4effa879 on ABO_MEMBERS (
+    ADDRESSBOOK_ID
+);
+
+create index ABO_MEMBERS_MEMBER_ID_8d66adcf on ABO_MEMBERS (
+    MEMBER_ID
+);
+
+create index ABO_FOREIGN_MEMBERS_A_1fd2c5e9 on ABO_FOREIGN_MEMBERS (
+    ADDRESSBOOK_ID
+);
+
+create index SHARED_GROUP_BIND_RES_cf52f95d on SHARED_GROUP_BIND (
+    GROUP_RESOURCE_ID
+);
+
+create index CALENDAR_OBJECT_REVIS_3a3956c4 on CALENDAR_OBJECT_REVISIONS (
+    CALENDAR_HOME_RESOURCE_ID,
+    CALENDAR_RESOURCE_ID
+);
+
+create index CALENDAR_OBJECT_REVIS_6d9d929c on CALENDAR_OBJECT_REVISIONS (
+    CALENDAR_RESOURCE_ID,
+    RESOURCE_NAME,
+    DELETED,
+    REVISION
+);
+
+create index CALENDAR_OBJECT_REVIS_265c8acf on CALENDAR_OBJECT_REVISIONS (
+    CALENDAR_RESOURCE_ID,
+    REVISION
+);
+
+create index ADDRESSBOOK_OBJECT_RE_2bfcf757 on ADDRESSBOOK_OBJECT_REVISIONS (
+    ADDRESSBOOK_HOME_RESOURCE_ID,
+    OWNER_HOME_RESOURCE_ID
+);
+
+create index ADDRESSBOOK_OBJECT_RE_00fe8288 on ADDRESSBOOK_OBJECT_REVISIONS (
+    OWNER_HOME_RESOURCE_ID,
+    RESOURCE_NAME,
+    DELETED,
+    REVISION
+);
+
+create index ADDRESSBOOK_OBJECT_RE_45004780 on ADDRESSBOOK_OBJECT_REVISIONS (
+    OWNER_HOME_RESOURCE_ID,
+    REVISION
+);
+
+create index NOTIFICATION_OBJECT_R_036a9cee on NOTIFICATION_OBJECT_REVISIONS (
+    NOTIFICATION_HOME_RESOURCE_ID,
+    REVISION
+);
+
+create index APN_SUBSCRIPTIONS_RES_9610d78e on APN_SUBSCRIPTIONS (
+    RESOURCE_KEY
+);
+
+create index IMIP_TOKENS_TOKEN_e94b918f on IMIP_TOKENS (
+    TOKEN
+);
+
+create index CALENDAR_OBJECT_SPLIT_af71dcda on CALENDAR_OBJECT_SPLITTER_WORK (
+    RESOURCE_ID
+);
+
+create index SCHEDULE_REFRESH_WORK_26084c7b on SCHEDULE_REFRESH_WORK (
+    HOME_RESOURCE_ID
+);
+
+create index SCHEDULE_REFRESH_WORK_989efe54 on SCHEDULE_REFRESH_WORK (
+    RESOURCE_ID
+);
+
+create index SCHEDULE_REFRESH_ATTE_83053b91 on SCHEDULE_REFRESH_ATTENDEES (
+    RESOURCE_ID,
+    ATTENDEE
+);
+
+create index SCHEDULE_AUTO_REPLY_W_0256478d on SCHEDULE_AUTO_REPLY_WORK (
+    HOME_RESOURCE_ID
+);
+
+create index SCHEDULE_AUTO_REPLY_W_0755e754 on SCHEDULE_AUTO_REPLY_WORK (
+    RESOURCE_ID
+);
+
+create index SCHEDULE_ORGANIZER_WO_18ce4edd on SCHEDULE_ORGANIZER_WORK (
+    HOME_RESOURCE_ID
+);
+
+create index SCHEDULE_ORGANIZER_WO_14702035 on SCHEDULE_ORGANIZER_WORK (
+    RESOURCE_ID
+);
+
+create index SCHEDULE_REPLY_WORK_H_745af8cf on SCHEDULE_REPLY_WORK (
+    HOME_RESOURCE_ID
+);
+
+create index SCHEDULE_REPLY_WORK_R_11bd3fbb on SCHEDULE_REPLY_WORK (
+    RESOURCE_ID
+);
+
+create index SCHEDULE_REPLY_CANCEL_dab513ef on SCHEDULE_REPLY_CANCEL_WORK (
+    HOME_RESOURCE_ID
+);
+
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaoldpostgresdialectv34sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/old/postgres-dialect/v34.sql (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/old/postgres-dialect/v34.sql                                (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/old/postgres-dialect/v34.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,869 @@
</span><ins>+-- -*- test-case-name: txdav.caldav.datastore.test.test_sql,txdav.carddav.datastore.test.test_sql -*-
+
+----
+-- Copyright (c) 2010-2014 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.
+----
+
+
+-----------------
+-- Resource ID --
+-----------------
+
+create sequence RESOURCE_ID_SEQ;
+
+
+-------------------------
+-- Cluster Bookkeeping --
+-------------------------
+
+-- Information about a process connected to this database.
+
+-- Note that this must match the node info schema in twext.enterprise.queue.
+create table NODE_INFO (
+  HOSTNAME  varchar(255) not null,
+  PID       integer      not null,
+  PORT      integer      not null,
+  TIME      timestamp    not null default timezone('UTC', CURRENT_TIMESTAMP),
+
+  primary key (HOSTNAME, PORT)
+);
+
+-- Unique named locks.  This table should always be empty, but rows are
+-- temporarily created in order to prevent undesirable concurrency.
+create table NAMED_LOCK (
+    LOCK_NAME varchar(255) primary key
+);
+
+
+-------------------
+-- Calendar Home --
+-------------------
+
+create table CALENDAR_HOME (
+  RESOURCE_ID      integer      primary key default nextval('RESOURCE_ID_SEQ'), -- implicit index
+  OWNER_UID        varchar(255) not null unique,                                -- implicit index
+  STATUS           integer      default 0 not null,                             -- enum HOME_STATUS
+  DATAVERSION      integer      default 0 not null
+);
+
+-- Enumeration of statuses
+
+create table HOME_STATUS (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into HOME_STATUS values (0, 'normal' );
+insert into HOME_STATUS values (1, 'external');
+
+
+--------------
+-- Calendar --
+--------------
+
+create table CALENDAR (
+  RESOURCE_ID integer   primary key default nextval('RESOURCE_ID_SEQ') -- implicit index
+);
+
+
+----------------------------
+-- Calendar Home Metadata --
+----------------------------
+
+create table CALENDAR_HOME_METADATA (
+  RESOURCE_ID              integer     primary key references CALENDAR_HOME on delete cascade, -- implicit index
+  QUOTA_USED_BYTES         integer     default 0 not null,
+  DEFAULT_EVENTS           integer     default null references CALENDAR on delete set null,
+  DEFAULT_TASKS            integer     default null references CALENDAR on delete set null,
+  DEFAULT_POLLS            integer     default null references CALENDAR on delete set null,
+  ALARM_VEVENT_TIMED       text        default null,
+  ALARM_VEVENT_ALLDAY      text        default null,
+  ALARM_VTODO_TIMED        text        default null,
+  ALARM_VTODO_ALLDAY       text        default null,
+  AVAILABILITY             text        default null,
+  CREATED                  timestamp   default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                 timestamp   default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+create index CALENDAR_HOME_METADATA_DEFAULT_EVENTS on
+        CALENDAR_HOME_METADATA(DEFAULT_EVENTS);
+create index CALENDAR_HOME_METADATA_DEFAULT_TASKS on
+        CALENDAR_HOME_METADATA(DEFAULT_TASKS);
+create index CALENDAR_HOME_METADATA_DEFAULT_POLLS on
+        CALENDAR_HOME_METADATA(DEFAULT_POLLS);
+
+
+-----------------------
+-- Calendar Metadata --
+-----------------------
+
+create table CALENDAR_METADATA (
+  RESOURCE_ID           integer      primary key references CALENDAR on delete cascade, -- implicit index
+  SUPPORTED_COMPONENTS  varchar(255) default null,
+  CREATED               timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+---------------------------
+-- Sharing Notifications --
+---------------------------
+
+create table NOTIFICATION_HOME (
+  RESOURCE_ID integer      primary key default nextval('RESOURCE_ID_SEQ'), -- implicit index
+  OWNER_UID   varchar(255) not null unique,                                -- implicit index
+  STATUS      integer      default 0 not null,                             -- enum HOME_STATUS
+  DATAVERSION integer      default 0 not null
+);
+
+create table NOTIFICATION (
+  RESOURCE_ID                   integer      primary key default nextval('RESOURCE_ID_SEQ'), -- implicit index
+  NOTIFICATION_HOME_RESOURCE_ID integer      not null references NOTIFICATION_HOME,
+  NOTIFICATION_UID              varchar(255) not null,
+  NOTIFICATION_TYPE             varchar(255) not null,
+  NOTIFICATION_DATA             text         not null,
+  MD5                           char(32)     not null,
+  CREATED                       timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                      timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique(NOTIFICATION_UID, NOTIFICATION_HOME_RESOURCE_ID) -- implicit index
+);
+
+create index NOTIFICATION_NOTIFICATION_HOME_RESOURCE_ID on
+        NOTIFICATION(NOTIFICATION_HOME_RESOURCE_ID);
+
+
+-------------------
+-- Calendar Bind --
+-------------------
+
+-- Joins CALENDAR_HOME and CALENDAR
+
+create table CALENDAR_BIND (
+  CALENDAR_HOME_RESOURCE_ID integer      not null references CALENDAR_HOME,
+  CALENDAR_RESOURCE_ID      integer      not null references CALENDAR on delete cascade,
+  EXTERNAL_ID                            integer      default null,
+  CALENDAR_RESOURCE_NAME    varchar(255) not null,
+  BIND_MODE                 integer      not null, -- enum CALENDAR_BIND_MODE
+  BIND_STATUS               integer      not null, -- enum CALENDAR_BIND_STATUS
+  BIND_REVISION                                integer      default 0 not null,
+  MESSAGE                   text,
+  TRANSP                    integer      default 0 not null, -- enum CALENDAR_TRANSP
+  ALARM_VEVENT_TIMED        text         default null,
+  ALARM_VEVENT_ALLDAY       text         default null,
+  ALARM_VTODO_TIMED         text         default null,
+  ALARM_VTODO_ALLDAY        text         default null,
+  TIMEZONE                  text         default null,
+
+  primary key(CALENDAR_HOME_RESOURCE_ID, CALENDAR_RESOURCE_ID), -- implicit index
+  unique(CALENDAR_HOME_RESOURCE_ID, CALENDAR_RESOURCE_NAME)     -- implicit index
+);
+
+create index CALENDAR_BIND_RESOURCE_ID on
+        CALENDAR_BIND(CALENDAR_RESOURCE_ID);
+
+-- Enumeration of calendar bind modes
+
+create table CALENDAR_BIND_MODE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_BIND_MODE values (0, 'own'  );
+insert into CALENDAR_BIND_MODE values (1, 'read' );
+insert into CALENDAR_BIND_MODE values (2, 'write');
+insert into CALENDAR_BIND_MODE values (3, 'direct');
+insert into CALENDAR_BIND_MODE values (4, 'indirect');
+
+-- Enumeration of statuses
+
+create table CALENDAR_BIND_STATUS (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_BIND_STATUS values (0, 'invited' );
+insert into CALENDAR_BIND_STATUS values (1, 'accepted');
+insert into CALENDAR_BIND_STATUS values (2, 'declined');
+insert into CALENDAR_BIND_STATUS values (3, 'invalid');
+insert into CALENDAR_BIND_STATUS values (4, 'deleted');
+
+
+-- Enumeration of transparency
+
+create table CALENDAR_TRANSP (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_TRANSP values (0, 'opaque' );
+insert into CALENDAR_TRANSP values (1, 'transparent');
+
+
+---------------------
+-- Calendar Object --
+---------------------
+
+create table CALENDAR_OBJECT (
+  RESOURCE_ID          integer      primary key default nextval('RESOURCE_ID_SEQ'), -- implicit index
+  CALENDAR_RESOURCE_ID integer      not null references CALENDAR on delete cascade,
+  RESOURCE_NAME        varchar(255) not null,
+  ICALENDAR_TEXT       text         not null,
+  ICALENDAR_UID        varchar(255) not null,
+  ICALENDAR_TYPE       varchar(255) not null,
+  ATTACHMENTS_MODE     integer      default 0 not null, -- enum CALENDAR_OBJECT_ATTACHMENTS_MODE
+  DROPBOX_ID           varchar(255),
+  ORGANIZER            varchar(255),
+  RECURRANCE_MIN       date,        -- minimum date that recurrences have been expanded to.
+  RECURRANCE_MAX       date,        -- maximum date that recurrences have been expanded to.
+  ACCESS               integer      default 0 not null,
+  SCHEDULE_OBJECT      boolean      default false,
+  SCHEDULE_TAG         varchar(36)  default null,
+  SCHEDULE_ETAGS       text         default null,
+  PRIVATE_COMMENTS     boolean      default false not null,
+  MD5                  char(32)     not null,
+  CREATED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED             timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique (CALENDAR_RESOURCE_ID, RESOURCE_NAME) -- implicit index
+
+  -- since the 'inbox' is a 'calendar resource' for the purpose of storing
+  -- calendar objects, this constraint has to be selectively enforced by the
+  -- application layer.
+
+  -- unique(CALENDAR_RESOURCE_ID, ICALENDAR_UID)
+);
+
+create index CALENDAR_OBJECT_CALENDAR_RESOURCE_ID_AND_ICALENDAR_UID on
+  CALENDAR_OBJECT(CALENDAR_RESOURCE_ID, ICALENDAR_UID);
+
+create index CALENDAR_OBJECT_CALENDAR_RESOURCE_ID_RECURRANCE_MAX on
+  CALENDAR_OBJECT(CALENDAR_RESOURCE_ID, RECURRANCE_MAX);
+
+create index CALENDAR_OBJECT_ICALENDAR_UID on
+  CALENDAR_OBJECT(ICALENDAR_UID);
+
+create index CALENDAR_OBJECT_DROPBOX_ID on
+  CALENDAR_OBJECT(DROPBOX_ID);
+
+-- Enumeration of attachment modes
+
+create table CALENDAR_OBJECT_ATTACHMENTS_MODE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (0, 'none' );
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (1, 'read' );
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (2, 'write');
+
+
+-- Enumeration of calendar access types
+
+create table CALENDAR_ACCESS_TYPE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(32) not null unique
+);
+
+insert into CALENDAR_ACCESS_TYPE values (0, ''             );
+insert into CALENDAR_ACCESS_TYPE values (1, 'public'       );
+insert into CALENDAR_ACCESS_TYPE values (2, 'private'      );
+insert into CALENDAR_ACCESS_TYPE values (3, 'confidential' );
+insert into CALENDAR_ACCESS_TYPE values (4, 'restricted'   );
+
+
+-----------------
+-- Instance ID --
+-----------------
+
+create sequence INSTANCE_ID_SEQ;
+
+
+----------------
+-- Time Range --
+----------------
+
+create table TIME_RANGE (
+  INSTANCE_ID                 integer        primary key default nextval('INSTANCE_ID_SEQ'), -- implicit index
+  CALENDAR_RESOURCE_ID        integer        not null references CALENDAR on delete cascade,
+  CALENDAR_OBJECT_RESOURCE_ID integer        not null references CALENDAR_OBJECT on delete cascade,
+  FLOATING                    boolean        not null,
+  START_DATE                  timestamp      not null,
+  END_DATE                    timestamp      not null,
+  FBTYPE                      integer        not null,
+  TRANSPARENT                 boolean        not null
+);
+
+create index TIME_RANGE_CALENDAR_RESOURCE_ID on
+  TIME_RANGE(CALENDAR_RESOURCE_ID);
+create index TIME_RANGE_CALENDAR_OBJECT_RESOURCE_ID on
+  TIME_RANGE(CALENDAR_OBJECT_RESOURCE_ID);
+
+
+-- Enumeration of free/busy types
+
+create table FREE_BUSY_TYPE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into FREE_BUSY_TYPE values (0, 'unknown'         );
+insert into FREE_BUSY_TYPE values (1, 'free'            );
+insert into FREE_BUSY_TYPE values (2, 'busy'            );
+insert into FREE_BUSY_TYPE values (3, 'busy-unavailable');
+insert into FREE_BUSY_TYPE values (4, 'busy-tentative'  );
+
+
+------------------
+-- Transparency --
+------------------
+
+create table TRANSPARENCY (
+  TIME_RANGE_INSTANCE_ID      integer      not null references TIME_RANGE on delete cascade,
+  USER_ID                     varchar(255) not null,
+  TRANSPARENT                 boolean      not null
+);
+
+create index TRANSPARENCY_TIME_RANGE_INSTANCE_ID on
+  TRANSPARENCY(TIME_RANGE_INSTANCE_ID);
+
+
+----------------
+-- Attachment --
+----------------
+
+create sequence ATTACHMENT_ID_SEQ;
+
+create table ATTACHMENT (
+  ATTACHMENT_ID               integer           primary key default nextval('ATTACHMENT_ID_SEQ'), -- implicit index
+  CALENDAR_HOME_RESOURCE_ID   integer           not null references CALENDAR_HOME,
+  DROPBOX_ID                  varchar(255),
+  CONTENT_TYPE                varchar(255)      not null,
+  SIZE                        integer           not null,
+  MD5                         char(32)          not null,
+  CREATED                     timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                    timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  PATH                        varchar(1024)     not null
+);
+
+create index ATTACHMENT_CALENDAR_HOME_RESOURCE_ID on
+  ATTACHMENT(CALENDAR_HOME_RESOURCE_ID);
+
+create index ATTACHMENT_DROPBOX_ID on
+  ATTACHMENT(DROPBOX_ID);
+
+-- Many-to-many relationship between attachments and calendar objects
+create table ATTACHMENT_CALENDAR_OBJECT (
+  ATTACHMENT_ID                  integer      not null references ATTACHMENT on delete cascade,
+  MANAGED_ID                     varchar(255) not null,
+  CALENDAR_OBJECT_RESOURCE_ID    integer      not null references CALENDAR_OBJECT on delete cascade,
+
+  primary key (ATTACHMENT_ID, CALENDAR_OBJECT_RESOURCE_ID), -- implicit index
+  unique (MANAGED_ID, CALENDAR_OBJECT_RESOURCE_ID) --implicit index
+);
+
+create index ATTACHMENT_CALENDAR_OBJECT_CALENDAR_OBJECT_RESOURCE_ID on
+        ATTACHMENT_CALENDAR_OBJECT(CALENDAR_OBJECT_RESOURCE_ID);
+
+-----------------------
+-- Resource Property --
+-----------------------
+
+create table RESOURCE_PROPERTY (
+  RESOURCE_ID integer      not null, -- foreign key: *.RESOURCE_ID
+  NAME        varchar(255) not null,
+  VALUE       text         not null, -- FIXME: xml?
+  VIEWER_UID  varchar(255),
+
+  primary key (RESOURCE_ID, NAME, VIEWER_UID) -- implicit index
+);
+
+
+----------------------
+-- AddressBook Home --
+----------------------
+
+create table ADDRESSBOOK_HOME (
+  RESOURCE_ID                                      integer                        primary key default nextval('RESOURCE_ID_SEQ'), -- implicit index
+  ADDRESSBOOK_PROPERTY_STORE_ID        integer              default nextval('RESOURCE_ID_SEQ') not null,         -- implicit index
+  OWNER_UID                                        varchar(255)         not null unique,                                -- implicit index
+  STATUS                                           integer              default 0 not null,                             -- enum HOME_STATUS
+  DATAVERSION                                      integer              default 0 not null
+);
+
+
+-------------------------------
+-- AddressBook Home Metadata --
+-------------------------------
+
+create table ADDRESSBOOK_HOME_METADATA (
+  RESOURCE_ID      integer      primary key references ADDRESSBOOK_HOME on delete cascade, -- implicit index
+  QUOTA_USED_BYTES integer      default 0 not null,
+  CREATED          timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED         timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+-----------------------------
+-- Shared AddressBook Bind --
+-----------------------------
+
+-- Joins sharee ADDRESSBOOK_HOME and owner ADDRESSBOOK_HOME
+
+create table SHARED_ADDRESSBOOK_BIND (
+  ADDRESSBOOK_HOME_RESOURCE_ID                        integer                        not null references ADDRESSBOOK_HOME,
+  OWNER_HOME_RESOURCE_ID                            integer              not null references ADDRESSBOOK_HOME on delete cascade,
+  EXTERNAL_ID                                        integer         default null,
+  ADDRESSBOOK_RESOURCE_NAME                            varchar(255)         not null,
+  BIND_MODE                                            integer              not null,        -- enum CALENDAR_BIND_MODE
+  BIND_STATUS                                          integer              not null,        -- enum CALENDAR_BIND_STATUS
+  BIND_REVISION                                                           integer              default 0 not null,
+  MESSAGE                                              text,                                  -- FIXME: xml?
+
+  primary key (ADDRESSBOOK_HOME_RESOURCE_ID, OWNER_HOME_RESOURCE_ID), -- implicit index
+  unique (ADDRESSBOOK_HOME_RESOURCE_ID, ADDRESSBOOK_RESOURCE_NAME)     -- implicit index
+);
+
+create index SHARED_ADDRESSBOOK_BIND_RESOURCE_ID on
+  SHARED_ADDRESSBOOK_BIND(OWNER_HOME_RESOURCE_ID);
+
+
+------------------------
+-- AddressBook Object --
+------------------------
+
+create table ADDRESSBOOK_OBJECT (
+  RESOURCE_ID                             integer                   primary key default nextval('RESOURCE_ID_SEQ'),    -- implicit index
+  ADDRESSBOOK_HOME_RESOURCE_ID         integer              not null references ADDRESSBOOK_HOME on delete cascade,
+  RESOURCE_NAME                           varchar(255)         not null,
+  VCARD_TEXT                              text                 not null,
+  VCARD_UID                               varchar(255)         not null,
+  KIND                                                             integer              not null,  -- enum ADDRESSBOOK_OBJECT_KIND
+  MD5                                     char(32)             not null,
+  CREATED                                 timestamp            default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                                timestamp            default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique (ADDRESSBOOK_HOME_RESOURCE_ID, RESOURCE_NAME), -- implicit index
+  unique (ADDRESSBOOK_HOME_RESOURCE_ID, VCARD_UID)      -- implicit index
+);
+
+
+-----------------------------
+-- AddressBook Object kind --
+-----------------------------
+
+create table ADDRESSBOOK_OBJECT_KIND (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into ADDRESSBOOK_OBJECT_KIND values (0, 'person');
+insert into ADDRESSBOOK_OBJECT_KIND values (1, 'group' );
+insert into ADDRESSBOOK_OBJECT_KIND values (2, 'resource');
+insert into ADDRESSBOOK_OBJECT_KIND values (3, 'location');
+
+
+----------------------------------
+-- Revisions, forward reference --
+----------------------------------
+
+create sequence REVISION_SEQ;
+
+---------------------------------
+-- Address Book Object Members --
+---------------------------------
+
+create table ABO_MEMBERS (
+    GROUP_ID                   integer                not null, -- references ADDRESSBOOK_OBJECT on delete cascade,        -- AddressBook Object's (kind=='group') RESOURCE_ID
+         ADDRESSBOOK_ID        integer                not null references ADDRESSBOOK_HOME on delete cascade,
+    MEMBER_ID              integer                not null, -- references ADDRESSBOOK_OBJECT,                                                -- member AddressBook Object's RESOURCE_ID
+          REVISION        integer           default nextval('REVISION_SEQ') not null,
+          REMOVED                boolean                default false not null,
+        MODIFIED        timestamp        default timezone('UTC', CURRENT_TIMESTAMP),
+
+    primary key (GROUP_ID, MEMBER_ID, REVISION) -- implicit index
+);
+
+create index ABO_MEMBERS_ADDRESSBOOK_ID on
+        ABO_MEMBERS(ADDRESSBOOK_ID);
+create index ABO_MEMBERS_MEMBER_ID on
+        ABO_MEMBERS(MEMBER_ID);
+
+------------------------------------------
+-- Address Book Object Foreign Members  --
+------------------------------------------
+
+create table ABO_FOREIGN_MEMBERS (
+    GROUP_ID              integer      not null references ADDRESSBOOK_OBJECT on delete cascade,        -- AddressBook Object's (kind=='group') RESOURCE_ID
+         ADDRESSBOOK_ID                  integer      not null references ADDRESSBOOK_HOME on delete cascade,
+    MEMBER_ADDRESS            varchar(255) not null,                                                                                                         -- member AddressBook Object's 'calendar' address
+
+    primary key (GROUP_ID, MEMBER_ADDRESS) -- implicit index
+);
+
+create index ABO_FOREIGN_MEMBERS_ADDRESSBOOK_ID on
+        ABO_FOREIGN_MEMBERS(ADDRESSBOOK_ID);
+
+-----------------------
+-- Shared Group Bind --
+-----------------------
+
+-- Joins ADDRESSBOOK_HOME and ADDRESSBOOK_OBJECT (kind == group)
+
+create table SHARED_GROUP_BIND (        
+  ADDRESSBOOK_HOME_RESOURCE_ID                 integer      not null references ADDRESSBOOK_HOME,
+  GROUP_RESOURCE_ID                              integer      not null references ADDRESSBOOK_OBJECT on delete cascade,
+  EXTERNAL_ID                                    integer      default null,
+  GROUP_ADDRESSBOOK_NAME                        varchar(255) not null,
+  BIND_MODE                                    integer      not null, -- enum CALENDAR_BIND_MODE
+  BIND_STATUS                                  integer      not null, -- enum CALENDAR_BIND_STATUS
+  BIND_REVISION                                                   integer      default 0 not null,
+  MESSAGE                                      text,                  -- FIXME: xml?
+
+  primary key (ADDRESSBOOK_HOME_RESOURCE_ID, GROUP_RESOURCE_ID), -- implicit index
+  unique (ADDRESSBOOK_HOME_RESOURCE_ID, GROUP_ADDRESSBOOK_NAME)  -- implicit index
+);
+
+create index SHARED_GROUP_BIND_RESOURCE_ID on
+  SHARED_GROUP_BIND(GROUP_RESOURCE_ID);
+
+
+---------------
+-- Revisions --
+---------------
+
+-- create sequence REVISION_SEQ;
+
+
+-------------------------------
+-- Calendar Object Revisions --
+-------------------------------
+
+create table CALENDAR_OBJECT_REVISIONS (
+  CALENDAR_HOME_RESOURCE_ID integer      not null references CALENDAR_HOME,
+  CALENDAR_RESOURCE_ID      integer      references CALENDAR,
+  CALENDAR_NAME             varchar(255) default null,
+  RESOURCE_NAME             varchar(255),
+  REVISION                  integer      default nextval('REVISION_SEQ') not null,
+  DELETED                   boolean      not null,
+  MODIFIED                  timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+create index CALENDAR_OBJECT_REVISIONS_HOME_RESOURCE_ID_CALENDAR_RESOURCE_ID
+  on CALENDAR_OBJECT_REVISIONS(CALENDAR_HOME_RESOURCE_ID, CALENDAR_RESOURCE_ID);
+
+create index CALENDAR_OBJECT_REVISIONS_RESOURCE_ID_RESOURCE_NAME_DELETED_REVISION
+  on CALENDAR_OBJECT_REVISIONS(CALENDAR_RESOURCE_ID, RESOURCE_NAME, DELETED, REVISION);
+
+create index CALENDAR_OBJECT_REVISIONS_RESOURCE_ID_REVISION
+  on CALENDAR_OBJECT_REVISIONS(CALENDAR_RESOURCE_ID, REVISION);
+
+
+----------------------------------
+-- AddressBook Object Revisions --
+----------------------------------
+
+create table ADDRESSBOOK_OBJECT_REVISIONS (
+  ADDRESSBOOK_HOME_RESOURCE_ID         integer                 not null references ADDRESSBOOK_HOME,
+  OWNER_HOME_RESOURCE_ID            integer      references ADDRESSBOOK_HOME,
+  ADDRESSBOOK_NAME                     varchar(255) default null,
+  OBJECT_RESOURCE_ID                        integer                 default 0,
+  RESOURCE_NAME                        varchar(255),
+  REVISION                             integer      default nextval('REVISION_SEQ') not null,
+  DELETED                              boolean            not null,
+  MODIFIED                          timestamp          default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+create index ADDRESSBOOK_OBJECT_REVISIONS_HOME_RESOURCE_ID_OWNER_HOME_RESOURCE_ID
+  on ADDRESSBOOK_OBJECT_REVISIONS(ADDRESSBOOK_HOME_RESOURCE_ID, OWNER_HOME_RESOURCE_ID);
+
+create index ADDRESSBOOK_OBJECT_REVISIONS_OWNER_HOME_RESOURCE_ID_RESOURCE_NAME_DELETED_REVISION
+  on ADDRESSBOOK_OBJECT_REVISIONS(OWNER_HOME_RESOURCE_ID, RESOURCE_NAME, DELETED, REVISION);
+
+create index ADDRESSBOOK_OBJECT_REVISIONS_OWNER_HOME_RESOURCE_ID_REVISION
+  on ADDRESSBOOK_OBJECT_REVISIONS(OWNER_HOME_RESOURCE_ID, REVISION);
+
+
+-----------------------------------
+-- Notification Object Revisions --
+-----------------------------------
+
+create table NOTIFICATION_OBJECT_REVISIONS (
+  NOTIFICATION_HOME_RESOURCE_ID integer      not null references NOTIFICATION_HOME on delete cascade,
+  RESOURCE_NAME                 varchar(255),
+  REVISION                      integer      default nextval('REVISION_SEQ') not null,
+  DELETED                       boolean      not null,
+  MODIFIED                          timestamp         default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique(NOTIFICATION_HOME_RESOURCE_ID, RESOURCE_NAME) -- implicit index
+);
+
+create index NOTIFICATION_OBJECT_REVISIONS_RESOURCE_ID_REVISION
+  on NOTIFICATION_OBJECT_REVISIONS(NOTIFICATION_HOME_RESOURCE_ID, REVISION);
+
+
+-------------------------------------------
+-- Apple Push Notification Subscriptions --
+-------------------------------------------
+
+create table APN_SUBSCRIPTIONS (
+  TOKEN                         varchar(255) not null,
+  RESOURCE_KEY                  varchar(255) not null,
+  MODIFIED                      integer      not null,
+  SUBSCRIBER_GUID               varchar(255) not null,
+  USER_AGENT                    varchar(255) default null,
+  IP_ADDR                       varchar(255) default null,
+
+  primary key (TOKEN, RESOURCE_KEY) -- implicit index
+);
+
+create index APN_SUBSCRIPTIONS_RESOURCE_KEY
+   on APN_SUBSCRIPTIONS(RESOURCE_KEY);
+
+   
+-----------------
+-- IMIP Tokens --
+-----------------
+
+create table IMIP_TOKENS (
+  TOKEN                         varchar(255) not null,
+  ORGANIZER                     varchar(255) not null,
+  ATTENDEE                      varchar(255) not null,
+  ICALUID                       varchar(255) not null,
+  ACCESSED                      timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+
+  primary key (ORGANIZER, ATTENDEE, ICALUID) -- implicit index
+);
+
+create index IMIP_TOKENS_TOKEN
+   on IMIP_TOKENS(TOKEN);
+
+   
+----------------
+-- Work Items --
+----------------
+
+create sequence WORKITEM_SEQ;
+
+
+---------------------------
+-- IMIP Inivitation Work --
+---------------------------
+
+create table IMIP_INVITATION_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  FROM_ADDR                     varchar(255) not null,
+  TO_ADDR                       varchar(255) not null,
+  ICALENDAR_TEXT                text         not null
+);
+
+
+-----------------------
+-- IMIP Polling Work --
+-----------------------
+
+create table IMIP_POLLING_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+---------------------
+-- IMIP Reply Work --
+---------------------
+
+create table IMIP_REPLY_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ORGANIZER                     varchar(255) not null,
+  ATTENDEE                      varchar(255) not null,
+  ICALENDAR_TEXT                text         not null
+);
+
+
+------------------------
+-- Push Notifications --
+------------------------
+
+create table PUSH_NOTIFICATION_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  PUSH_ID                       varchar(255) not null,
+  PRIORITY                      integer      not null -- 1:low 5:medium 10:high
+);
+
+-----------------
+-- GroupCacher --
+-----------------
+
+create table GROUP_CACHER_POLLING_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+--------------------------
+-- Object Splitter Work --
+--------------------------
+
+create table CALENDAR_OBJECT_SPLITTER_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  RESOURCE_ID                   integer      not null references CALENDAR_OBJECT on delete cascade
+);
+
+create index CALENDAR_OBJECT_SPLITTER_WORK_RESOURCE_ID on
+        CALENDAR_OBJECT_SPLITTER_WORK(RESOURCE_ID);
+
+---------------------------
+-- Revision Cleaner Work --
+---------------------------
+
+create table FIND_MIN_VALID_REVISION_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+create table REVISION_CLEANUP_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+---------------------------
+-- Schedule Refresh Work --
+---------------------------
+
+create table SCHEDULE_REFRESH_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ICALENDAR_UID                                varchar(255) not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade,
+  RESOURCE_ID                   integer      not null references CALENDAR_OBJECT on delete cascade,
+  ATTENDEE_COUNT                                integer
+);
+
+create index SCHEDULE_REFRESH_WORK_HOME_RESOURCE_ID on
+        SCHEDULE_REFRESH_WORK(HOME_RESOURCE_ID);
+create index SCHEDULE_REFRESH_WORK_RESOURCE_ID on
+        SCHEDULE_REFRESH_WORK(RESOURCE_ID);
+
+create table SCHEDULE_REFRESH_ATTENDEES (
+  RESOURCE_ID                   integer      not null references CALENDAR_OBJECT on delete cascade,
+  ATTENDEE                                    varchar(255) not null
+);
+
+create index SCHEDULE_REFRESH_ATTENDEES_RESOURCE_ID_ATTENDEE on
+        SCHEDULE_REFRESH_ATTENDEES(RESOURCE_ID, ATTENDEE);
+
+------------------------------
+-- Schedule Auto Reply Work --
+------------------------------
+
+create table SCHEDULE_AUTO_REPLY_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ICALENDAR_UID                                varchar(255) not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade,
+  RESOURCE_ID                   integer      not null references CALENDAR_OBJECT on delete cascade,
+  PARTSTAT                                                varchar(255) not null
+);
+
+create index SCHEDULE_AUTO_REPLY_WORK_HOME_RESOURCE_ID on
+        SCHEDULE_AUTO_REPLY_WORK(HOME_RESOURCE_ID);
+create index SCHEDULE_AUTO_REPLY_WORK_RESOURCE_ID on
+        SCHEDULE_AUTO_REPLY_WORK(RESOURCE_ID);
+
+-----------------------------
+-- Schedule Organizer Work --
+-----------------------------
+
+create table SCHEDULE_ORGANIZER_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ICALENDAR_UID                                varchar(255) not null,
+  SCHEDULE_ACTION                                integer                 not null, -- Enum SCHEDULE_ACTION
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade,
+  RESOURCE_ID                   integer,         -- this references a possibly non-existent CALENDR_OBJECT
+  ICALENDAR_TEXT_OLD                        text,
+  ICALENDAR_TEXT_NEW                        text,
+  ATTENDEE_COUNT                                integer,
+  SMART_MERGE                                        boolean
+);
+
+create index SCHEDULE_ORGANIZER_WORK_HOME_RESOURCE_ID on
+        SCHEDULE_ORGANIZER_WORK(HOME_RESOURCE_ID);
+create index SCHEDULE_ORGANIZER_WORK_RESOURCE_ID on
+        SCHEDULE_ORGANIZER_WORK(RESOURCE_ID);
+
+-- Enumeration of schedule actions
+
+create table SCHEDULE_ACTION (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into SCHEDULE_ACTION values (0, 'create');
+insert into SCHEDULE_ACTION values (1, 'modify');
+insert into SCHEDULE_ACTION values (2, 'modify-cancelled');
+insert into SCHEDULE_ACTION values (3, 'remove');
+
+-------------------------
+-- Schedule Reply Work --
+-------------------------
+
+create table SCHEDULE_REPLY_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ICALENDAR_UID                                varchar(255) not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade,
+  RESOURCE_ID                   integer      not null references CALENDAR_OBJECT on delete cascade,
+  CHANGED_RIDS                               text
+);
+
+create index SCHEDULE_REPLY_WORK_HOME_RESOURCE_ID on
+        SCHEDULE_REPLY_WORK(HOME_RESOURCE_ID);
+create index SCHEDULE_REPLY_WORK_RESOURCE_ID on
+        SCHEDULE_REPLY_WORK(RESOURCE_ID);
+
+--------------------------------
+-- Schedule Reply Cancel Work --
+--------------------------------
+
+create table SCHEDULE_REPLY_CANCEL_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  ICALENDAR_UID                                varchar(255) not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade,
+  ICALENDAR_TEXT                               text         not null
+);
+
+create index SCHEDULE_REPLY_CANCEL_WORK_HOME_RESOURCE_ID on
+        SCHEDULE_REPLY_CANCEL_WORK(HOME_RESOURCE_ID);
+
+--------------------
+-- Schema Version --
+--------------------
+
+create table CALENDARSERVER (
+  NAME                          varchar(255) primary key, -- implicit index
+  VALUE                         varchar(255)
+);
+
+insert into CALENDARSERVER values ('VERSION', '34');
+insert into CALENDARSERVER values ('CALENDAR-DATAVERSION', '5');
+insert into CALENDARSERVER values ('ADDRESSBOOK-DATAVERSION', '2');
+insert into CALENDARSERVER values ('NOTIFICATION-DATAVERSION', '1');
+insert into CALENDARSERVER values ('MIN-VALID-REVISION', '1');
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradesoracledialectupgrade_from_34_to_35sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql                                (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,92 @@
</span><ins>+----
+-- Copyright (c) 2012-2013 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.
+----
+
+---------------------------------------------------
+-- Upgrade database schema from VERSION 34 to 35 --
+---------------------------------------------------
+
+create table GROUP_REFRESH_WORK (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;GROUP_GUID&quot; nvarchar2(255)
+);
+
+create table GROUP_ATTENDEE_RECONCILIATION_ (
+    &quot;WORK_ID&quot; integer primary key not null,
+    &quot;NOT_BEFORE&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;RESOURCE_ID&quot; integer,
+    &quot;GROUP_ID&quot; integer
+);
+
+create table GROUPS (
+    &quot;GROUP_ID&quot; integer primary key,
+    &quot;NAME&quot; nvarchar2(255),
+    &quot;GROUP_GUID&quot; nvarchar2(255),
+    &quot;MEMBERSHIP_HASH&quot; nvarchar2(255),
+    &quot;EXTANT&quot; integer default 1,
+    &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC'
+);
+
+create table GROUP_MEMBERSHIP (
+    &quot;GROUP_ID&quot; integer,
+    &quot;MEMBER_GUID&quot; nvarchar2(255)
+);
+
+create table GROUP_ATTENDEE (
+    &quot;GROUP_ID&quot; integer,
+    &quot;RESOURCE_ID&quot; integer,
+    &quot;MEMBERSHIP_HASH&quot; nvarchar2(255)
+);
+
+create table DELEGATES (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;DELEGATE&quot; nvarchar2(255),
+    &quot;READ_WRITE&quot; integer not null
+);
+
+create table DELEGATE_GROUPS (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;GROUP_ID&quot; integer not null,
+    &quot;READ_WRITE&quot; integer not null,
+    &quot;IS_EXTERNAL&quot; integer not null
+);
+
+create table EXTERNAL_DELEGATE_GROUPS (
+    &quot;DELEGATOR&quot; nvarchar2(255),
+    &quot;GROUP_GUID_READ&quot; nvarchar2(255),
+    &quot;GROUP_GUID_WRITE&quot; nvarchar2(255)
+);
+
+
+create index GROUPS_GROUP_GUID_ebf7a1d4 on GROUPS (
+    GROUP_GUID
+);
+
+create index GROUP_MEMBERSHIP_GROU_9560a5e6 on GROUP_MEMBERSHIP (
+    GROUP_ID
+);
+
+create index GROUP_MEMBERSHIP_MEMB_0ca508e8 on GROUP_MEMBERSHIP (
+    MEMBER_GUID
+);
+
+
+-- Now update the version
+-- No data upgrades
+update CALENDARSERVER set VALUE = '35' where NAME = 'VERSION';
+
+
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradespostgresdialectupgrade_from_34_to_35sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql                                (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,87 @@
</span><ins>+----
+-- Copyright (c) 2012-2013 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.
+----
+
+---------------------------------------------------
+-- Upgrade database schema from VERSION 34 to 35 --
+---------------------------------------------------
+
+----------------------
+-- Group membership --
+----------------------
+
+create table GROUP_REFRESH_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  GROUP_GUID                    varchar(255) not null
+);
+
+create table GROUP_ATTENDEE_RECONCILIATION_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ') not null, -- implicit index
+  NOT_BEFORE                    timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  RESOURCE_ID                   integer,
+  GROUP_ID                      integer
+);
+
+create table GROUPS (
+  GROUP_ID                      integer      primary key default nextval('RESOURCE_ID_SEQ'),    -- implicit index
+  NAME                          varchar(255) not null,
+  GROUP_GUID                    varchar(255) not null,
+  MEMBERSHIP_HASH               varchar(255) not null,
+  EXTANT                        integer default 1,
+  CREATED                       timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                      timestamp default timezone('UTC', CURRENT_TIMESTAMP)
+);
+create index GROUPS_GROUP_GUID on GROUPS(GROUP_GUID);
+
+create table GROUP_MEMBERSHIP (
+  GROUP_ID                      integer,
+  MEMBER_GUID                   varchar(255) not null
+);
+create index GROUP_MEMBERSHIP_GROUP on GROUP_MEMBERSHIP(GROUP_ID);
+create index GROUP_MEMBERSHIP_MEMBER on GROUP_MEMBERSHIP(MEMBER_GUID);
+
+create table GROUP_ATTENDEE (
+  GROUP_ID                      integer,
+  RESOURCE_ID                   integer,
+  MEMBERSHIP_HASH               varchar(255) not null
+);
+
+---------------
+-- Delegates --
+---------------
+
+create table DELEGATES (
+  DELEGATOR                     varchar(255) not null,
+  DELEGATE                      varchar(255) not null,
+  READ_WRITE                    integer      not null -- 1 = ReadWrite, 0 = ReadOnly
+);
+
+create table DELEGATE_GROUPS (
+  DELEGATOR                     varchar(255) not null,
+  GROUP_ID                      integer      not null,
+  READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+  IS_EXTERNAL                   integer      not null -- 1 = ReadWrite, 0 = ReadOnly
+);
+
+create table EXTERNAL_DELEGATE_GROUPS (
+  DELEGATOR                     varchar(255) not null,
+  GROUP_GUID_READ               varchar(255),
+  GROUP_GUID_WRITE              varchar(255)
+);
+
+-- Now update the version
+-- No data upgrades
+update CALENDARSERVER set VALUE = '35' where NAME = 'VERSION';
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhodelegatespy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/delegates.py (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/delegates.py                                (rev 0)
+++ CalendarServer/trunk/txdav/who/delegates.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,124 @@
</span><ins>+# -*- test-case-name: twext.who.test.test_delegates -*-
+##
+# Copyright (c) 2013 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.
+##
+
+&quot;&quot;&quot;
+Delegate assignments
+&quot;&quot;&quot;
+
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twext.who.idirectory import RecordType
+
+from twext.python.log import Logger
+log = Logger()
+
+
+@inlineCallbacks
+def addDelegate(txn, delegator, delegate, readWrite):
+    &quot;&quot;&quot;
+    Adds &quot;delegate&quot; as a delegate of &quot;delegator&quot;.  The type of access is
+    specified by the &quot;readWrite&quot; parameter.
+
+    @param delegator: the delegator's directory record
+    @type delegator: L{IDirectoryRecord}
+    @param delegate: the delegate's directory record
+    @type delegate: L{IDirectoryRecord}
+    @param readWrite: if True, read and write access is granted; read-only
+        access otherwise
+    &quot;&quot;&quot;
+    if delegate.recordType == RecordType.group:
+        # find the groupID
+        groupID, name, membershipHash = (yield txn.groupByGUID(delegate.guid))
+        yield txn.addDelegateGroup(delegator.guid, groupID, readWrite)
+    else:
+        yield txn.addDelegate(delegator.guid, delegate.guid, readWrite)
+
+
+@inlineCallbacks
+def removeDelegate(txn, delegator, delegate, readWrite):
+    &quot;&quot;&quot;
+    Removes &quot;delegate&quot; as a delegate of &quot;delegator&quot;.  The type of access is
+    specified by the &quot;readWrite&quot; parameter.
+
+    @param delegator: the delegator's directory record
+    @type delegator: L{IDirectoryRecord}
+    @param delegate: the delegate's directory record
+    @type delegate: L{IDirectoryRecord}
+    @param readWrite: if True, read and write access is revoked; read-only
+        access otherwise
+    &quot;&quot;&quot;
+    if delegate.recordType == RecordType.group:
+        # find the groupID
+        groupID, name, membershipHash = (yield txn.groupByGUID(delegate.guid))
+        yield txn.removeDelegateGroup(delegator.guid, groupID, readWrite)
+    else:
+        yield txn.removeDelegate(delegator.guid, delegate.guid, readWrite)
+
+
+@inlineCallbacks
+def delegatesOf(txn, delegator, readWrite):
+    &quot;&quot;&quot;
+    Return the records of the delegates of &quot;delegator&quot;.  The type of access
+    is specified by the &quot;readWrite&quot; parameter.
+
+    @param delegator: the delegator's directory record
+    @type delegator: L{IDirectoryRecord}
+    @param readWrite: if True, read and write access delegates are returned;
+        read-only access otherwise
+    @return: the set of directory records
+    @rtype: a Deferred which fires a set of L{IDirectoryRecord}
+    &quot;&quot;&quot;
+    records = []
+    directory = delegator.service
+    delegateGUIDs = (yield txn.delegates(delegator.guid, readWrite))
+    for guid in delegateGUIDs:
+        if guid != delegator.guid:
+            record = (yield directory.recordWithGUID(guid))
+            if record is not None:
+                records.append(record)
+    returnValue(records)
+
+
+@inlineCallbacks
+def delegatedTo(txn, delegate, readWrite):
+    &quot;&quot;&quot;
+    Return the records of those who have delegated to &quot;delegate&quot;.  The type of
+    access is specified by the &quot;readWrite&quot; parameter.
+
+    @param delegate: the delegate's directory record
+    @type delegate: L{IDirectoryRecord}
+    @param readWrite: if True, read and write access delegators are returned;
+        read-only access otherwise
+    @return: the set of directory records
+    @rtype: a Deferred which fires a set of L{IDirectoryRecord}
+    &quot;&quot;&quot;
+    records = []
+    directory = delegate.service
+    delegatorGUIDs = (yield txn.delegators(delegate.guid, readWrite))
+    for guid in delegatorGUIDs:
+        if guid != delegate.guid:
+            record = (yield directory.recordWithGUID(guid))
+            if record is not None:
+                records.append(record)
+    returnValue(records)
+
+
+def allGroupDelegates(txn):
+    &quot;&quot;&quot;
+    @return: the GUIDs of all groups which are currently delegated to
+    @rtype: a Deferred which fires with a set() of GUID strings
+    &quot;&quot;&quot;
+    return txn.allGroupDelegates()
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhogroupspy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/groups.py (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/groups.py                                (rev 0)
+++ CalendarServer/trunk/txdav/who/groups.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,427 @@
</span><ins>+# -*- test-case-name: twext.who.test.test_groups -*-
+##
+# Copyright (c) 2013 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.
+##
+
+&quot;&quot;&quot;
+Group membership caching
+&quot;&quot;&quot;
+
+from twext.enterprise.dal.record import fromTable
+from twext.enterprise.dal.syntax import Delete, Select
+from twext.enterprise.queue import WorkItem, PeerConnectionPool
+from txdav.who.delegates import allGroupDelegates
+from twext.who.idirectory import RecordType
+from twisted.internet.defer import inlineCallbacks, returnValue
+from txdav.common.datastore.sql_tables import schema
+import datetime
+import hashlib
+
+from twext.python.log import Logger
+log = Logger()
+
+
+class GroupCacherPollingWork(
+    WorkItem,
+    fromTable(schema.GROUP_CACHER_POLLING_WORK)
+):
+
+    group = &quot;group_cacher_polling&quot;
+
+    @inlineCallbacks
+    def doWork(self):
+
+        # Delete all other work items
+        yield Delete(From=self.table, Where=None).on(self.transaction)
+
+        oldGroupCacher = getattr(self.transaction, &quot;_groupCacher&quot;, None)
+        newGroupCacher = getattr(self.transaction, &quot;_newGroupCacher&quot;, None)
+        if oldGroupCacher is not None or newGroupCacher is not None:
+
+            # Schedule next update
+
+            # TODO: Be sure to move updateSeconds to the new cacher
+            # implementation
+            notBefore = (
+                datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=oldGroupCacher.updateSeconds)
+            )
+            log.debug(&quot;Scheduling next group cacher update: %s&quot; % (notBefore,))
+            yield self.transaction.enqueue(
+                GroupCacherPollingWork,
+                notBefore=notBefore
+            )
+
+            # New implmementation
+            try:
+                newGroupCacher.update(self.transaction)
+            except Exception, e:
+                log.error(&quot;Failed to update new group membership cache (%s)&quot; % (e,))
+
+            # Old implmementation
+            try:
+                oldGroupCacher.updateCache()
+            except Exception, e:
+                log.error(&quot;Failed to update old group membership cache (%s)&quot; % (e,))
+
+        else:
+            notBefore = (
+                datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=10)
+            )
+            log.debug(&quot;Rescheduling group cacher update: %s&quot; % (notBefore,))
+            yield self.transaction.enqueue(
+                GroupCacherPollingWork,
+                notBefore=notBefore
+            )
+
+
+
+@inlineCallbacks
+def scheduleNextGroupCachingUpdate(store, seconds):
+    txn = store.newTransaction()
+    notBefore = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds)
+    log.debug(&quot;Scheduling next group cacher update: %s&quot; % (notBefore,))
+    wp = (yield txn.enqueue(GroupCacherPollingWork, notBefore=notBefore))
+    yield txn.commit()
+    returnValue(wp)
+
+
+
+def schedulePolledGroupCachingUpdate(store):
+    &quot;&quot;&quot;
+    Schedules a group caching update work item in &quot;the past&quot; so PeerConnectionPool's
+    overdue-item logic picks it up quickly.
+    &quot;&quot;&quot;
+    seconds = -PeerConnectionPool.queueProcessTimeout
+    return scheduleNextGroupCachingUpdate(store, seconds)
+
+
+
+class GroupRefreshWork(WorkItem, fromTable(schema.GROUP_REFRESH_WORK)):
+
+    group = property(lambda self: self.groupGUID)
+
+    @inlineCallbacks
+    def doWork(self):
+
+        # Delete all other work items for this group
+        yield Delete(From=self.table, Where=(self.table.GROUP_GUID == self.groupGUID)).on(self.transaction)
+
+        groupCacher = getattr(self.transaction, &quot;_groupCacher&quot;, None)
+        if groupCacher is not None:
+
+            try:
+                groupCacher.refreshGroup(self.transaction, self.groupGUID)
+            except Exception, e:
+                log.error(
+                    &quot;Failed to refresh group {group} {err}&quot;,
+                    group=self.groupGUID, err=e
+                )
+
+        else:
+            notBefore = (
+                datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=10)
+            )
+            log.debug(
+                &quot;Rescheduling group refresh for {group}: {when}&quot;,
+                group=self.groupGUID, when=notBefore
+            )
+            yield self.transaction.enqueue(
+                GroupRefreshWork,
+                groupGUID=self.groupGUID, notBefore=notBefore
+            )
+
+
+
+class GroupAttendeeReconciliationWork(WorkItem, fromTable(schema.GROUP_ATTENDEE_RECONCILIATION_WORK)):
+
+    group = property(lambda self: &quot;%s, %s&quot; % (self.groupID, self.eventID))
+
+    @inlineCallbacks
+    def doWork(self):
+
+        # Delete all other work items for this group
+        yield Delete(
+            From=self.table,
+            Where=((self.table.GROUP_ID == self.self.groupID).And(
+                self.table.RESOURCE_ID == self.self.eventID)
+            )
+        ).on(self.transaction)
+
+    # TODO: Pull this over from groupcacher branch
+
+
+@inlineCallbacks
+def expandedMembers(record, members=None, records=None):
+    &quot;&quot;&quot;
+    Return the expanded set of member records.  Intermediate groups are not returned
+    in the results, but their members are.
+    &quot;&quot;&quot;
+    if members is None:
+        members = set()
+    if records is None:
+        records = set()
+
+    if record not in records:
+        records.add(record)
+        for member in (yield record.members()):
+            if member not in records:
+                #TODO:  HACK for old-style XML. FIX
+                if member.recordType != RecordType.group and str(member.recordType) != &quot;groups&quot;:
+                    members.add(member)
+                yield expandedMembers(member, members, records)
+
+    returnValue(members)
+
+
+def diffAssignments(old, new):
+    &quot;&quot;&quot;
+    Compare two proxy assignment lists and return their differences in the form of
+    two lists -- one for added/updated assignments, and one for removed assignments.
+    @param old: dictionary of delegator: (readGroupGUID, writeGroupGUID)
+    @type old: C{dict}
+    @param new: dictionary of delegator: (readGroupGUID, writeGroupGUID)
+    @type new: C{dict}
+    @return: Tuple of two lists; the first list contains tuples of (delegator,
+        (readGroupGUID, writeGroupGUID)), and represents all the new or updated
+        assignments.  The second list contains all the delegators which used to
+        have a delegate but don't anymore.
+    &quot;&quot;&quot;
+    changed = []
+    removed = []
+    for key in old.iterkeys():
+        if key not in new:
+            removed.append(key)
+        else:
+            if old[key] != new[key]:
+                changed.append((key, new[key]))
+    for key in new.iterkeys():
+        if key not in old:
+            changed.append((key, new[key]))
+    return changed, removed
+
+
+
+class GroupCacher(object):
+    log = Logger()
+
+    def __init__(
+        self, directory,
+        useExternalProxies=False, externalProxiesSource=None
+    ):
+        self.directory = directory
+        self.useExternalProxies = useExternalProxies
+        if useExternalProxies and externalProxiesSource is None:
+            externalProxiesSource = self.directory.getExternalProxyAssignments
+        self.externalProxiesSource = externalProxiesSource
+
+
+    @inlineCallbacks
+    def update(self, txn):
+        # TODO
+        # Pull in external delegate assignments and stick in delegate db
+        # if self.useExternalProxies:
+        #     externalAssignments = (yield self.externalProxiesSource())
+        # yield self.applyExternalAssignments(txn, externalAssignments)
+
+
+        # Figure out which groups matter
+        groupGUIDs = yield self.groupsToRefresh(txn)
+        self.log.debug(
+            &quot;Number of groups to refresh: {num}&quot;.format(num=len(groupGUIDs))
+        )
+        # For each of those groups, create a per-group refresh work item
+        for groupGUID in groupGUIDs:
+            notBefore = (
+                datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=1)
+            )
+            yield txn.enqueue(
+                GroupRefreshWork, groupGUID=groupGUID, notBefore=notBefore
+            )
+
+
+    @inlineCallbacks
+    def applyExternalAssignments(self, txn, newAssignments):
+
+        oldAssignments = (yield txn.externalDelegates())
+
+        &quot;&quot;&quot;
+        external assignments is of the form:
+        { delegatorGUID: (readDelegateGroupGUID, writeDelegateGroupGUID),
+        }
+        &quot;&quot;&quot;
+
+        changed, removed = diffAssignments(oldAssignments, newAssignments)
+        if changed:
+            for delegatorGUID, (readDelegateGUID, writeDelegateGUID) in changed:
+                readDelegateGroupID = writeDelegateGroupID  = None
+                if readDelegateGUID:
+                    readDelegateGroupID, name, hash = (
+                        yield txn.groupByGUID(readDelegateGUID)
+                    )
+                if writeDelegateGUID:
+                    writeDelegateGroupID, name, hash = (
+                        yield txn.groupByGUID(writeDelegateGUID)
+                    )
+                yield txn.assignExternalDelegates(
+                    delegatorGUID, readDelegateGroupID, writeDelegateGroupID,
+                    readDelegateGUID, writeDelegateGUID
+                )
+        if removed:
+            for delegatorGUID in removed:
+                yield txn.assignExternalDelegates(
+                    delegatorGUID, None, None, None, None
+                )
+
+
+
+    @inlineCallbacks
+    def refreshGroup(self, txn, groupGUID):
+        # Does the work of a per-group refresh work item
+        # Faults in the flattened membership of a group, as GUIDs
+        # and updates the GROUP_MEMBERSHIP table
+        record = (yield self.directory.recordWithGUID(groupGUID))
+        membershipHashContent = hashlib.md5()
+        members = (yield expandedMembers(record))
+        members = list(members)
+        members.sort(cmp=lambda x, y: cmp(x.guid, y.guid))
+        for member in members:
+            membershipHashContent.update(str(member.guid))
+        membershipHash = membershipHashContent.hexdigest()
+        groupID, cachedName, cachedMembershipHash = (
+            yield txn.groupByGUID(groupGUID)
+        )
+
+        if cachedMembershipHash != membershipHash:
+            membershipChanged = True
+            self.log.debug(&quot;Group '{group}' changed&quot;, group=record.fullNames[0])
+        else:
+            membershipChanged = False
+
+        yield txn.updateGroup(groupGUID, record.fullNames[0], membershipHash)
+
+        if membershipChanged:
+            newMemberGUIDs = set()
+            for member in members:
+                newMemberGUIDs.add(member.guid)
+            yield self.synchronizeMembers(txn, groupID, newMemberGUIDs)
+
+        yield self.scheduleEventReconciliations(txn, groupID, groupGUID)
+
+
+    @inlineCallbacks
+    def synchronizeMembers(self, txn, groupID, newMemberGUIDs):
+        numRemoved = numAdded = 0
+        cachedMemberGUIDs = (yield txn.membersOfGroup(groupID))
+
+        for memberGUID in cachedMemberGUIDs:
+            if memberGUID not in newMemberGUIDs:
+                numRemoved += 1
+                yield txn.removeMemberFromGroup(memberGUID, groupID)
+
+        for memberGUID in newMemberGUIDs:
+            if memberGUID not in cachedMemberGUIDs:
+                numAdded += 1
+                yield txn.addMemberToGroup(memberGUID, groupID)
+
+        returnValue((numAdded, numRemoved))
+
+
+    @inlineCallbacks
+    def cachedMembers(self, txn, groupID):
+        &quot;&quot;&quot;
+        The members of the given group as recorded in the db
+        &quot;&quot;&quot;
+        members = set()
+        memberGUIDs = (yield txn.membersOfGroup(groupID))
+        for guid in memberGUIDs:
+            record = (yield self.directory.recordWithGUID(guid))
+            if record is not None:
+                members.add(record)
+        returnValue(members)
+
+
+    def cachedGroupsFor(self, txn, guid):
+        &quot;&quot;&quot;
+        The IDs of the groups the guid is a member of
+        &quot;&quot;&quot;
+        return txn.groupsFor(guid)
+
+
+    @inlineCallbacks
+    def scheduleEventReconciliations(self, txn, groupID, groupGUID):
+        &quot;&quot;&quot;
+        Find all events who have this groupID as an attendee and create
+        work items for them.
+        &quot;&quot;&quot;
+        groupAttendee = schema.GROUP_ATTENDEE
+        rows = yield Select(
+            [groupAttendee.RESOURCE_ID, ],
+            From=groupAttendee,
+            Where=groupAttendee.GROUP_ID == groupID,
+        ).on(txn)
+        eventIDs = [row[0] for row in rows]
+
+        for eventID in eventIDs:
+
+            notBefore = (
+                datetime.datetime.utcnow() +
+                datetime.timedelta(seconds=10)
+            )
+            log.debug(
+                &quot;scheduling group reconciliation for ({eventID}, {groupID}, {groupGUID}): {when}&quot;,
+                eventID=eventID,
+                groupID=groupID,
+                groupGUID=groupGUID,
+                when=notBefore)
+
+            yield txn.enqueue(
+                GroupAttendeeReconciliationWork,
+                eventID=eventID,
+                groupID=groupID,
+                groupGUID=groupGUID,
+                notBefore=notBefore
+            )
+
+
+    @inlineCallbacks
+    def groupsToRefresh(self, txn):
+        delegatedGUIDs = set((yield allGroupDelegates(txn)))
+        self.log.info(&quot;There are %d group delegates&quot; % (len(delegatedGUIDs),))
+
+        attendeeGroupGUIDs = set()
+
+        # get all groups from events
+        groupAttendee = schema.GROUP_ATTENDEE
+        rows = yield Select(
+            [groupAttendee.GROUP_ID, ],
+            From=groupAttendee,
+        ).on(txn)
+        groupIDs = set([row[0] for row in rows])
+
+        # get groupGUIDs
+        if groupIDs:
+            gr = schema.GROUPS
+            rows = yield Select(
+                [gr.GROUP_GUID, ],
+                From=gr,
+                Where=gr.GROUP_ID.In(groupIDs)
+            ).on(txn)
+            attendeeGroupGUIDs = set([row[0] for row in rows])
+
+        returnValue(delegatedGUIDs.union(attendeeGroupGUIDs))
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhotestaccountsaccountsxml"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/test/accounts/accounts.xml (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/accounts/accounts.xml                                (rev 0)
+++ CalendarServer/trunk/txdav/who/test/accounts/accounts.xml        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,191 @@
</span><ins>+&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
+
+&lt;!--
+Copyright (c) 2006-2013 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.
+ --&gt;
+
+&lt;!DOCTYPE accounts SYSTEM &quot;accounts.dtd&quot;&gt;
+
+&lt;accounts realm=&quot;Test Realm&quot;&gt;
+  &lt;user&gt;
+    &lt;uid&gt;admin&lt;/uid&gt;
+    &lt;guid&gt;admin&lt;/guid&gt;
+    &lt;password&gt;admin&lt;/password&gt;
+    &lt;name&gt;Super User&lt;/name&gt;
+    &lt;first-name&gt;Super&lt;/first-name&gt;
+    &lt;last-name&gt;User&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;apprentice&lt;/uid&gt;
+    &lt;guid&gt;apprentice&lt;/guid&gt;
+    &lt;password&gt;apprentice&lt;/password&gt;
+    &lt;name&gt;Apprentice Super User&lt;/name&gt;
+    &lt;first-name&gt;Apprentice&lt;/first-name&gt;
+    &lt;last-name&gt;Super User&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;wsanchez&lt;/uid&gt;
+    &lt;guid&gt;wsanchez&lt;/guid&gt;
+    &lt;email-address&gt;wsanchez@example.com&lt;/email-address&gt;
+    &lt;password&gt;test&lt;/password&gt;
+    &lt;name&gt;Wilfredo Sanchez Vega&lt;/name&gt;
+    &lt;first-name&gt;Wilfredo&lt;/first-name&gt;
+    &lt;last-name&gt;Sanchez Vega&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;cdaboo&lt;/uid&gt;
+    &lt;guid&gt;cdaboo&lt;/guid&gt;
+    &lt;email-address&gt;cdaboo@example.com&lt;/email-address&gt;
+    &lt;password&gt;test&lt;/password&gt;
+    &lt;name&gt;Cyrus Daboo&lt;/name&gt;
+    &lt;first-name&gt;Cyrus&lt;/first-name&gt;
+    &lt;last-name&gt;Daboo&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;sagen&lt;/uid&gt;
+    &lt;guid&gt;sagen&lt;/guid&gt;
+    &lt;email-address&gt;sagen@example.com&lt;/email-address&gt;
+    &lt;password&gt;test&lt;/password&gt;
+    &lt;name&gt;Morgen Sagen&lt;/name&gt;
+    &lt;first-name&gt;Morgen&lt;/first-name&gt;
+    &lt;last-name&gt;Sagen&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;dre&lt;/uid&gt;
+    &lt;guid&gt;andre&lt;/guid&gt;
+    &lt;email-address&gt;dre@example.com&lt;/email-address&gt;
+    &lt;password&gt;test&lt;/password&gt;
+    &lt;name&gt;Andre LaBranche&lt;/name&gt;
+    &lt;first-name&gt;Andre&lt;/first-name&gt;
+    &lt;last-name&gt;LaBranche&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;glyph&lt;/uid&gt;
+    &lt;guid&gt;glyph&lt;/guid&gt;
+    &lt;email-address&gt;glyph@example.com&lt;/email-address&gt;
+    &lt;password&gt;test&lt;/password&gt;
+    &lt;name&gt;Glyph Lefkowitz&lt;/name&gt;
+    &lt;first-name&gt;Glyph&lt;/first-name&gt;
+    &lt;last-name&gt;Lefkowitz&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user&gt;
+    &lt;uid&gt;i18nuser&lt;/uid&gt;
+    &lt;guid&gt;i18nuser&lt;/guid&gt;
+    &lt;email-address&gt;i18nuser@example.com&lt;/email-address&gt;
+    &lt;password&gt;i18nuser&lt;/password&gt;
+    &lt;name&gt;まだ&lt;/name&gt;
+    &lt;first-name&gt;ま&lt;/first-name&gt;
+    &lt;last-name&gt;だ&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;user repeat=&quot;101&quot;&gt;
+    &lt;uid&gt;user%02d&lt;/uid&gt;
+    &lt;uid&gt;User %02d&lt;/uid&gt;
+    &lt;guid&gt;user%02d&lt;/guid&gt;
+    &lt;password&gt;user%02d&lt;/password&gt;
+    &lt;name&gt;User %02d&lt;/name&gt;
+    &lt;first-name&gt;User&lt;/first-name&gt;
+    &lt;last-name&gt;%02d&lt;/last-name&gt;
+    &lt;email-address&gt;user%02d@example.com&lt;/email-address&gt;
+  &lt;/user&gt;
+  &lt;user repeat=&quot;10&quot;&gt;
+    &lt;uid&gt;public%02d&lt;/uid&gt;
+    &lt;guid&gt;public%02d&lt;/guid&gt;
+    &lt;password&gt;public%02d&lt;/password&gt;
+    &lt;name&gt;Public %02d&lt;/name&gt;
+    &lt;first-name&gt;Public&lt;/first-name&gt;
+    &lt;last-name&gt;%02d&lt;/last-name&gt;
+  &lt;/user&gt;
+  &lt;group&gt;
+    &lt;uid&gt;group01&lt;/uid&gt;
+    &lt;guid&gt;group01&lt;/guid&gt;
+    &lt;password&gt;group01&lt;/password&gt;
+    &lt;name&gt;Group 01&lt;/name&gt;
+    &lt;email-address&gt;group01@example.com&lt;/email-address&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user01&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt;
+    &lt;uid&gt;group02&lt;/uid&gt;
+    &lt;guid&gt;group02&lt;/guid&gt;
+    &lt;password&gt;group02&lt;/password&gt;
+    &lt;name&gt;Group 02&lt;/name&gt;
+    &lt;email-address&gt;group02@example.com&lt;/email-address&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user06&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user07&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt;
+    &lt;uid&gt;group03&lt;/uid&gt;
+    &lt;guid&gt;group03&lt;/guid&gt;
+    &lt;password&gt;group03&lt;/password&gt;
+    &lt;name&gt;Group 03&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user08&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user09&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt;
+    &lt;uid&gt;group04&lt;/uid&gt;
+    &lt;guid&gt;group04&lt;/guid&gt;
+    &lt;password&gt;group04&lt;/password&gt;
+    &lt;name&gt;Group 04&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;groups&quot;&gt;group02&lt;/member&gt;
+      &lt;member type=&quot;groups&quot;&gt;group03&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user10&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt; &lt;!-- delegategroup --&gt;
+    &lt;uid&gt;group05&lt;/uid&gt;
+    &lt;guid&gt;group05&lt;/guid&gt;
+    &lt;password&gt;group05&lt;/password&gt;
+    &lt;name&gt;Group 05&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;groups&quot;&gt;group06&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user20&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt; &lt;!-- delegatesubgroup --&gt;
+    &lt;uid&gt;group06&lt;/uid&gt;
+    &lt;guid&gt;group06&lt;/guid&gt;
+    &lt;password&gt;group06&lt;/password&gt;
+    &lt;name&gt;Group 06&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user21&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt; &lt;!-- readonlydelegategroup --&gt;
+    &lt;uid&gt;group07&lt;/uid&gt;
+    &lt;guid&gt;group07&lt;/guid&gt;
+    &lt;password&gt;group07&lt;/password&gt;
+    &lt;name&gt;Group 07&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user22&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user23&lt;/member&gt;
+      &lt;member type=&quot;users&quot;&gt;user24&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+  &lt;group&gt;
+    &lt;uid&gt;disabledgroup&lt;/uid&gt;
+    &lt;guid&gt;disabledgroup&lt;/guid&gt;
+    &lt;password&gt;disabledgroup&lt;/password&gt;
+    &lt;name&gt;Disabled Group&lt;/name&gt;
+    &lt;members&gt;
+      &lt;member type=&quot;users&quot;&gt;user01&lt;/member&gt;
+    &lt;/members&gt;
+  &lt;/group&gt;
+&lt;/accounts&gt;
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhotestaccountsresourcesxml"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/test/accounts/resources.xml (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/accounts/resources.xml                                (rev 0)
+++ CalendarServer/trunk/txdav/who/test/accounts/resources.xml        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,34 @@
</span><ins>+&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
+
+&lt;!--
+Copyright (c) 2006-2013 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.
+ --&gt;
+
+&lt;!DOCTYPE accounts SYSTEM &quot;accounts.dtd&quot;&gt;
+
+&lt;accounts realm=&quot;Test Realm&quot;&gt;
+  &lt;location repeat=&quot;10&quot;&gt;
+    &lt;uid&gt;location%02d&lt;/uid&gt;
+    &lt;guid&gt;location%02d&lt;/guid&gt;
+    &lt;password&gt;location%02d&lt;/password&gt;
+    &lt;name&gt;Room %02d&lt;/name&gt;
+  &lt;/location&gt;
+  &lt;resource repeat=&quot;10&quot;&gt;
+    &lt;uid&gt;resource%02d&lt;/uid&gt;
+    &lt;guid&gt;resource%02d&lt;/guid&gt;
+    &lt;password&gt;resource%02d&lt;/password&gt;
+    &lt;name&gt;Resource %02d&lt;/name&gt;
+  &lt;/resource&gt;
+&lt;/accounts&gt;
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_delegatespy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/test/test_delegates.py (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_delegates.py                                (rev 0)
+++ CalendarServer/trunk/txdav/who/test/test_delegates.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,239 @@
</span><ins>+##
+# Copyright (c) 2013 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.
+##
+
+&quot;&quot;&quot;
+Delegates implementation tests
+&quot;&quot;&quot;
+
+from txdav.who.delegates import (
+    addDelegate, removeDelegate, delegatesOf, delegatedTo, allGroupDelegates
+)
+from txdav.who.groups import GroupCacher
+from twext.who.idirectory import RecordType
+from twext.who.test.test_xml import xmlService
+from twisted.internet.defer import inlineCallbacks
+from twistedcaldav.test.util import StoreTestCase
+from uuid import UUID
+
+
+class DelegationTest(StoreTestCase):
+
+    @inlineCallbacks
+    def setUp(self):
+        yield super(DelegationTest, self).setUp()
+        self.xmlService = xmlService(self.mktemp(), xmlData=testXMLConfig)
+        self.groupCacher = GroupCacher(self.xmlService)
+
+
+    @inlineCallbacks
+    def test_directDelegation(self):
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        delegator = yield self.xmlService.recordWithUID(u&quot;__wsanchez__&quot;)
+        delegate1 = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
+        delegate2 = yield self.xmlService.recordWithUID(u&quot;__cdaboo__&quot;)
+
+        # Add 1 delegate
+        yield addDelegate(txn, delegator, delegate1, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals([&quot;sagen&quot;], [d.shortNames[0] for d in delegates])
+        delegators = (yield delegatedTo(txn, delegate1, True))
+        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
+
+        # Add another delegate
+        yield addDelegate(txn, delegator, delegate2, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;, &quot;cdaboo&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+        delegators = (yield delegatedTo(txn, delegate2, True))
+        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
+
+        # Remove 1 delegate
+        yield removeDelegate(txn, delegator, delegate1, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals([&quot;cdaboo&quot;], [d.shortNames[0] for d in delegates])
+        delegators = (yield delegatedTo(txn, delegate1, True))
+        self.assertEquals(0, len(delegators))
+
+        # Remove the other delegate
+        yield removeDelegate(txn, delegator, delegate2, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(0, len(delegates))
+        delegators = (yield delegatedTo(txn, delegate2, True))
+        self.assertEquals(0, len(delegators))
+
+
+    @inlineCallbacks
+    def test_indirectDelegation(self):
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        delegator = yield self.xmlService.recordWithUID(u&quot;__wsanchez__&quot;)
+        delegate1 = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
+        group1 = yield self.xmlService.recordWithUID(u&quot;__top_group_1__&quot;)
+        group2 = yield self.xmlService.recordWithUID(u&quot;__sub_group_1__&quot;)
+
+        # Add group delegate, but before the group membership has been
+        # pulled in
+        yield addDelegate(txn, delegator, group1, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(0, len(delegates))
+
+        # Now refresh the group and there will be 3 delegates (contained
+        # within 2 nested groups)
+        # guid = &quot;49b350c69611477b94d95516b13856ab&quot;
+        yield self.groupCacher.refreshGroup(txn, group1.guid)
+        yield self.groupCacher.refreshGroup(txn, group2.guid)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+        delegators = (yield delegatedTo(txn, delegate1, True))
+        self.assertEquals([&quot;wsanchez&quot;], [d.shortNames[0] for d in delegators])
+
+        # Verify we can ask for all delegated-to groups
+        yield addDelegate(txn, delegator, group2, True)
+        groups = (yield allGroupDelegates(txn))
+        self.assertEquals(
+            set([
+                UUID(&quot;49b350c69611477b94d95516b13856ab&quot;),
+                UUID(&quot;86144f73345a409782f1b782672087c7&quot;)
+                ]), set(groups))
+
+        # Delegate to a user who is already indirectly delegated-to
+        yield addDelegate(txn, delegator, delegate1, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+
+        # Add a member to the group; they become a delegate
+        newSet = set()
+        for name in (u&quot;wsanchez&quot;, u&quot;cdaboo&quot;, u&quot;sagen&quot;, u&quot;glyph&quot;, u&quot;dre&quot;):
+            record = (
+                yield self.xmlService.recordWithShortName(RecordType.user, name)
+            )
+            newSet.add(record.guid)
+        groupID, name, membershipHash = (yield txn.groupByGUID(group1.guid))
+        numAdded, numRemoved = (
+            yield self.groupCacher.synchronizeMembers(txn, groupID, newSet)
+        )
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;, &quot;dre&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+
+        # Remove delegate access from the top group
+        yield removeDelegate(txn, delegator, group1, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;, &quot;cdaboo&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+
+        # Remove delegate access from the sub group
+        yield removeDelegate(txn, delegator, group2, True)
+        delegates = (yield delegatesOf(txn, delegator, True))
+        self.assertEquals(
+            set([&quot;sagen&quot;]),
+            set([d.shortNames[0] for d in delegates])
+        )
+
+
+
+testXMLConfig = &quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
+
+&lt;directory realm=&quot;xyzzy&quot;&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__wsanchez__&lt;/uid&gt;
+    &lt;guid&gt;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&lt;/guid&gt;
+    &lt;short-name&gt;wsanchez&lt;/short-name&gt;
+    &lt;short-name&gt;wilfredo_sanchez&lt;/short-name&gt;
+    &lt;full-name&gt;Wilfredo Sanchez&lt;/full-name&gt;
+    &lt;password&gt;zehcnasw&lt;/password&gt;
+    &lt;email&gt;wsanchez@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;wsanchez@devnull.twistedmatrix.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__glyph__&lt;/uid&gt;
+    &lt;guid&gt;9064DF91-1DBC-4E07-9C2B-6839B0953876&lt;/guid&gt;
+    &lt;short-name&gt;glyph&lt;/short-name&gt;
+    &lt;full-name&gt;Glyph Lefkowitz&lt;/full-name&gt;
+    &lt;password&gt;hpylg&lt;/password&gt;
+    &lt;email&gt;glyph@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;glyph@devnull.twistedmatrix.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__sagen__&lt;/uid&gt;
+    &lt;guid&gt;4AD155CB-AE9B-475F-986C-E08A7537893E&lt;/guid&gt;
+    &lt;short-name&gt;sagen&lt;/short-name&gt;
+    &lt;full-name&gt;Morgen Sagen&lt;/full-name&gt;
+    &lt;password&gt;negas&lt;/password&gt;
+    &lt;email&gt;sagen@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;shared@example.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__cdaboo__&lt;/uid&gt;
+    &lt;guid&gt;7D45CB10-479E-456B-B54D-528958C5734B&lt;/guid&gt;
+    &lt;short-name&gt;cdaboo&lt;/short-name&gt;
+    &lt;full-name&gt;Cyrus Daboo&lt;/full-name&gt;
+    &lt;password&gt;suryc&lt;/password&gt;
+    &lt;email&gt;cdaboo@bitbucket.calendarserver.org&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__dre__&lt;/uid&gt;
+    &lt;guid&gt;CFC88493-DBFF-42B9-ADC7-9B3DA0B0769B&lt;/guid&gt;
+    &lt;short-name&gt;dre&lt;/short-name&gt;
+    &lt;full-name&gt;Andre LaBranche&lt;/full-name&gt;
+    &lt;password&gt;erd&lt;/password&gt;
+    &lt;email&gt;dre@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;shared@example.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;group&quot;&gt;
+    &lt;uid&gt;__top_group_1__&lt;/uid&gt;
+    &lt;guid&gt;49B350C6-9611-477B-94D9-5516B13856AB&lt;/guid&gt;
+    &lt;short-name&gt;top-group-1&lt;/short-name&gt;
+    &lt;full-name&gt;Top Group 1&lt;/full-name&gt;
+    &lt;email&gt;topgroup1@example.com&lt;/email&gt;
+    &lt;member-uid&gt;__wsanchez__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__glyph__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__sub_group_1__&lt;/member-uid&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;group&quot;&gt;
+    &lt;uid&gt;__sub_group_1__&lt;/uid&gt;
+    &lt;guid&gt;86144F73-345A-4097-82F1-B782672087C7&lt;/guid&gt;
+    &lt;short-name&gt;sub-group-1&lt;/short-name&gt;
+    &lt;full-name&gt;Sub Group 1&lt;/full-name&gt;
+    &lt;email&gt;subgroup1@example.com&lt;/email&gt;
+    &lt;member-uid&gt;__sagen__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__cdaboo__&lt;/member-uid&gt;
+  &lt;/record&gt;
+
+&lt;/directory&gt;
+&quot;&quot;&quot;
</ins></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_groupspy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/who/test/test_groups.py (0 => 12663)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_groups.py                                (rev 0)
+++ CalendarServer/trunk/txdav/who/test/test_groups.py        2014-02-12 00:30:58 UTC (rev 12663)
</span><span class="lines">@@ -0,0 +1,497 @@
</span><ins>+##
+# Copyright (c) 2013 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.
+##
+
+&quot;&quot;&quot;
+Group membership caching implementation tests
+&quot;&quot;&quot;
+
+from txdav.who.groups import GroupCacher, expandedMembers, diffAssignments
+from twext.who.idirectory import RecordType
+from twext.who.test.test_xml import xmlService
+from twisted.internet.defer import inlineCallbacks
+from twistedcaldav.test.util import StoreTestCase
+from txdav.common.icommondatastore import NotFoundError
+from uuid import UUID
+
+
+class GroupCacherTest(StoreTestCase):
+
+    @inlineCallbacks
+    def setUp(self):
+        yield super(GroupCacherTest, self).setUp()
+        self.xmlService = xmlService(self.mktemp(), xmlData=testXMLConfig)
+        self.groupCacher = GroupCacher(self.xmlService)
+
+
+    @inlineCallbacks
+    def test_expandedMembers(self):
+        &quot;&quot;&quot;
+        Verify expandedMembers() returns a &quot;flattened&quot; set of records
+        belonging to a group (and does not return sub-groups themselves,
+        only their members)
+        &quot;&quot;&quot;
+        record = yield self.xmlService.recordWithUID(u&quot;__top_group_1__&quot;)
+        memberUIDs = set()
+        for member in (yield expandedMembers(record)):
+            memberUIDs.add(member.uid)
+        self.assertEquals(
+            memberUIDs,
+            set([&quot;__cdaboo__&quot;, &quot;__glyph__&quot;, &quot;__sagen__&quot;, &quot;__wsanchez__&quot;])
+        )
+
+        # Non group records return an empty set() of members
+        record = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
+        members = yield expandedMembers(record)
+        self.assertEquals(0, len(list(members)))
+
+
+    @inlineCallbacks
+    def test_refreshGroup(self):
+        &quot;&quot;&quot;
+        Verify refreshGroup() adds a group to the Groups table with the
+        expected membership hash value and members
+        &quot;&quot;&quot;
+
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        record = yield self.xmlService.recordWithUID(u&quot;__top_group_1__&quot;)
+        yield self.groupCacher.refreshGroup(txn, record.guid)
+
+        groupID, name, membershipHash = (yield txn.groupByGUID(record.guid))
+        self.assertEquals(membershipHash, &quot;4b0e162f2937f0f3daa6d10e5a6a6c33&quot;)
+
+        groupGUID, name, membershipHash = (yield txn.groupByID(groupID))
+        self.assertEquals(groupGUID, record.guid)
+        self.assertEquals(name, &quot;Top Group 1&quot;)
+        self.assertEquals(membershipHash, &quot;4b0e162f2937f0f3daa6d10e5a6a6c33&quot;)
+
+        members = (yield txn.membersOfGroup(groupID))
+        self.assertEquals(
+            set([UUID(&quot;9064df911dbc4e079c2b6839b0953876&quot;),
+                 UUID(&quot;4ad155cbae9b475f986ce08a7537893e&quot;),
+                 UUID(&quot;3bdcb95484d54f6d8035eac19a6d6e1f&quot;),
+                 UUID(&quot;7d45cb10479e456bb54d528958c5734b&quot;)]),
+            members
+        )
+
+        records = (yield self.groupCacher.cachedMembers(txn, groupID))
+        self.assertEquals(
+            set([r.shortNames[0] for r in records]),
+            set([&quot;wsanchez&quot;, &quot;cdaboo&quot;, &quot;glyph&quot;, &quot;sagen&quot;])
+        )
+
+        # sagen is in the top group, even though it's actually one level
+        # removed
+        record = yield self.xmlService.recordWithUID(u&quot;__sagen__&quot;)
+        groups = (yield self.groupCacher.cachedGroupsFor(txn, record.guid))
+        self.assertEquals(set([groupID]), groups)
+
+
+    @inlineCallbacks
+    def test_synchronizeMembers(self):
+        &quot;&quot;&quot;
+        After loading in a group via refreshGroup(), pass new member sets to
+        synchronizeMembers() and verify members are added and removed as
+        expected
+        &quot;&quot;&quot;
+
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        # Refresh the group so it's assigned a group_id
+        guid = UUID(&quot;49b350c69611477b94d95516b13856ab&quot;)
+        yield self.groupCacher.refreshGroup(txn, guid)
+        groupID, name, membershipHash = (yield txn.groupByGUID(guid))
+
+        # Remove two members, and add one member
+        newSet = set()
+        for name in (u&quot;wsanchez&quot;, u&quot;cdaboo&quot;, u&quot;dre&quot;):
+            record = (
+                yield self.xmlService.recordWithShortName(
+                    RecordType.user,
+                    name
+                )
+            )
+            newSet.add(record.guid)
+        numAdded, numRemoved = (
+            yield self.groupCacher.synchronizeMembers(
+                txn, groupID, newSet
+            )
+        )
+        self.assertEquals(numAdded, 1)
+        self.assertEquals(numRemoved, 2)
+        records = (yield self.groupCacher.cachedMembers(txn, groupID))
+        self.assertEquals(
+            set([r.shortNames[0] for r in records]),
+            set([&quot;wsanchez&quot;, &quot;cdaboo&quot;, &quot;dre&quot;])
+        )
+
+        # Remove all members
+        numAdded, numRemoved = (
+            yield self.groupCacher.synchronizeMembers(txn, groupID, set())
+        )
+        self.assertEquals(numAdded, 0)
+        self.assertEquals(numRemoved, 3)
+        records = (yield self.groupCacher.cachedMembers(txn, groupID))
+        self.assertEquals(len(records), 0)
+
+
+    @inlineCallbacks
+    def test_groupByID(self):
+
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        # Non-existent groupID
+        self.failUnlessFailure(txn.groupByID(42), NotFoundError)
+
+        guid = UUID(&quot;49b350c69611477b94d95516b13856ab&quot;)
+        hash = &quot;4b0e162f2937f0f3daa6d10e5a6a6c33&quot;
+        yield self.groupCacher.refreshGroup(txn, guid)
+        groupID, name, membershipHash = (yield txn.groupByGUID(guid))
+        results = (yield txn.groupByID(groupID))
+        self.assertEquals([guid, &quot;Top Group 1&quot;, hash], results)
+
+
+    @inlineCallbacks
+    def test_externalAssignments(self):
+
+        store = self.storeUnderTest()
+        txn = store.newTransaction()
+
+        oldExternalAssignments = (yield txn.externalDelegates())
+        self.assertEquals(oldExternalAssignments, {})
+
+        newAssignments = {
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;):
+            (None, UUID(&quot;49B350C6-9611-477B-94D9-5516B13856AB&quot;))
+        }
+        yield self.groupCacher.applyExternalAssignments(txn, newAssignments)
+        oldExternalAssignments = (yield txn.externalDelegates())
+        self.assertEquals(
+            oldExternalAssignments,
+            {
+                UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;):
+                (
+                    None,
+                    UUID(&quot;49B350C6-9611-477B-94D9-5516B13856AB&quot;)
+                )
+            }
+        )
+
+        newAssignments = {
+            UUID(&quot;7D45CB10-479E-456B-B54D-528958C5734B&quot;):
+            (
+                UUID(&quot;86144F73-345A-4097-82F1-B782672087C7&quot;),
+                None
+            ),
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;):
+            (
+                UUID(&quot;86144F73-345A-4097-82F1-B782672087C7&quot;),
+                UUID(&quot;49B350C6-9611-477B-94D9-5516B13856AB&quot;)
+            ),
+        }
+        yield self.groupCacher.applyExternalAssignments(txn, newAssignments)
+        oldExternalAssignments = (yield txn.externalDelegates())
+        self.assertEquals(
+            oldExternalAssignments,
+            {
+                UUID('3bdcb954-84d5-4f6d-8035-eac19a6d6e1f'):
+                (
+                    UUID('86144f73-345a-4097-82f1-b782672087c7'),
+                    UUID('49b350c6-9611-477b-94d9-5516b13856ab')
+                ),
+                UUID('7d45cb10-479e-456b-b54d-528958c5734b'):
+                (
+                    UUID('86144f73-345a-4097-82f1-b782672087c7'),
+                    None
+                )
+            }
+        )
+
+        allGroupDelegates = (yield txn.allGroupDelegates())
+        self.assertEquals(
+            allGroupDelegates,
+            set(
+                [
+                    UUID('49b350c6-9611-477b-94d9-5516b13856ab'),
+                    UUID('86144f73-345a-4097-82f1-b782672087c7')
+                ]
+            )
+        )
+
+        # Fault in the read-only group
+        yield self.groupCacher.refreshGroup(txn, UUID('86144f73-345a-4097-82f1-b782672087c7'))
+
+        # Wilfredo should have Sagen and Daboo as read-only delegates
+        delegates = (yield txn.delegates(
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;), False)
+        )
+        self.assertEquals(
+            delegates,
+            set(
+                [
+                    UUID('4ad155cb-ae9b-475f-986c-e08a7537893e'),
+                    UUID('7d45cb10-479e-456b-b54d-528958c5734b')
+                ]
+            )
+        )
+
+        # Fault in the read-write group
+        yield self.groupCacher.refreshGroup(txn, UUID('49b350c6-9611-477b-94d9-5516b13856ab'))
+
+        # Wilfredo should have 4 users as read-write delegates
+        delegates = (yield txn.delegates(
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;), True)
+        )
+        self.assertEquals(
+            delegates,
+            set(
+                [
+                    UUID('3bdcb954-84d5-4f6d-8035-eac19a6d6e1f'),
+                    UUID('4ad155cb-ae9b-475f-986c-e08a7537893e'),
+                    UUID('7d45cb10-479e-456b-b54d-528958c5734b'),
+                    UUID('9064df91-1dbc-4e07-9c2b-6839b0953876')
+                ]
+            )
+        )
+
+
+        #
+        # Now, remove some external assignments
+        #
+        newAssignments = {
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;):
+            (
+                UUID(&quot;86144F73-345A-4097-82F1-B782672087C7&quot;),
+                None
+            ),
+        }
+        yield self.groupCacher.applyExternalAssignments(txn, newAssignments)
+        oldExternalAssignments = (yield txn.externalDelegates())
+        self.assertEquals(
+            oldExternalAssignments,
+            {
+                UUID('3bdcb954-84d5-4f6d-8035-eac19a6d6e1f'):
+                (
+                    UUID('86144f73-345a-4097-82f1-b782672087c7'),
+                    None
+                ),
+            }
+        )
+
+        allGroupDelegates = (yield txn.allGroupDelegates())
+        self.assertEquals(
+            allGroupDelegates,
+            set(
+                [
+                    UUID('86144f73-345a-4097-82f1-b782672087c7')
+                ]
+            )
+        )
+
+        # Wilfredo should have Sagen and Daboo as read-only delegates
+        delegates = (yield txn.delegates(
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;), False)
+        )
+        self.assertEquals(
+            delegates,
+            set(
+                [
+                    UUID('4ad155cb-ae9b-475f-986c-e08a7537893e'),
+                    UUID('7d45cb10-479e-456b-b54d-528958c5734b')
+                ]
+            )
+        )
+
+        # Wilfredo should have no read-write delegates
+        delegates = (yield txn.delegates(
+            UUID(&quot;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&quot;), True)
+        )
+        self.assertEquals(
+            delegates,
+            set([])
+        )
+
+        # Only 1 group as delegate now:
+        allGroupDelegates = (yield txn.allGroupDelegates())
+        self.assertEquals(
+            allGroupDelegates,
+            set(
+                [
+                    UUID('86144f73-345a-4097-82f1-b782672087c7')
+                ]
+            )
+        )
+
+    def test_diffAssignments(self):
+        &quot;&quot;&quot;
+        Ensure external proxy assignment diffing works
+        &quot;&quot;&quot;
+
+        self.assertEquals(
+            (
+                # changed
+                [],
+                # removed
+                [],
+            ),
+            diffAssignments(
+                # old
+                {},
+                # new
+                {}
+            )
+        )
+
+        self.assertEquals(
+            (
+                # changed
+                [],
+                # removed
+                [],
+            ),
+            diffAssignments(
+                # old
+                {&quot;B&quot;: (&quot;1&quot;, &quot;2&quot;)},
+                # new
+                {&quot;B&quot;: (&quot;1&quot;, &quot;2&quot;)},
+            )
+        )
+
+        self.assertEquals(
+            (
+                # changed
+                [(&quot;A&quot;, (&quot;1&quot;, &quot;2&quot;)), (&quot;B&quot;, (&quot;3&quot;, &quot;4&quot;))],
+                # removed
+                [],
+            ),
+            diffAssignments(
+                # old
+                {},
+                # new
+                {&quot;A&quot;: (&quot;1&quot;, &quot;2&quot;), &quot;B&quot;: (&quot;3&quot;, &quot;4&quot;)}
+            )
+        )
+
+        self.assertEquals(
+            (
+                # changed
+                [],
+                # removed
+                [&quot;A&quot;, &quot;B&quot;],
+            ),
+            diffAssignments(
+                # old
+                {&quot;A&quot;: (&quot;1&quot;, &quot;2&quot;), &quot;B&quot;: (&quot;3&quot;, &quot;4&quot;)},
+                # new
+                {},
+            )
+        )
+
+        self.assertEquals(
+            (
+                # changed
+                [('C', ('4', '5')), ('D', ('7', '8'))],
+                # removed
+                [&quot;B&quot;],
+            ),
+            diffAssignments(
+                # old
+                {&quot;A&quot;: (&quot;1&quot;, &quot;2&quot;), &quot;B&quot;: (&quot;3&quot;, &quot;4&quot;), &quot;C&quot;: (&quot;5&quot;, &quot;6&quot;)},
+                # new
+                {&quot;D&quot;: (&quot;7&quot;, &quot;8&quot;), &quot;C&quot;: (&quot;4&quot;, &quot;5&quot;), &quot;A&quot;: (&quot;1&quot;, &quot;2&quot;)},
+            )
+        )
+
+testXMLConfig = &quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
+
+&lt;directory realm=&quot;xyzzy&quot;&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__wsanchez__&lt;/uid&gt;
+    &lt;guid&gt;3BDCB954-84D5-4F6D-8035-EAC19A6D6E1F&lt;/guid&gt;
+    &lt;short-name&gt;wsanchez&lt;/short-name&gt;
+    &lt;short-name&gt;wilfredo_sanchez&lt;/short-name&gt;
+    &lt;full-name&gt;Wilfredo Sanchez&lt;/full-name&gt;
+    &lt;password&gt;zehcnasw&lt;/password&gt;
+    &lt;email&gt;wsanchez@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;wsanchez@devnull.twistedmatrix.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__glyph__&lt;/uid&gt;
+    &lt;guid&gt;9064DF91-1DBC-4E07-9C2B-6839B0953876&lt;/guid&gt;
+    &lt;short-name&gt;glyph&lt;/short-name&gt;
+    &lt;full-name&gt;Glyph Lefkowitz&lt;/full-name&gt;
+    &lt;password&gt;hpylg&lt;/password&gt;
+    &lt;email&gt;glyph@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;glyph@devnull.twistedmatrix.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__sagen__&lt;/uid&gt;
+    &lt;guid&gt;4AD155CB-AE9B-475F-986C-E08A7537893E&lt;/guid&gt;
+    &lt;short-name&gt;sagen&lt;/short-name&gt;
+    &lt;full-name&gt;Morgen Sagen&lt;/full-name&gt;
+    &lt;password&gt;negas&lt;/password&gt;
+    &lt;email&gt;sagen@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;shared@example.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__cdaboo__&lt;/uid&gt;
+    &lt;guid&gt;7D45CB10-479E-456B-B54D-528958C5734B&lt;/guid&gt;
+    &lt;short-name&gt;cdaboo&lt;/short-name&gt;
+    &lt;full-name&gt;Cyrus Daboo&lt;/full-name&gt;
+    &lt;password&gt;suryc&lt;/password&gt;
+    &lt;email&gt;cdaboo@bitbucket.calendarserver.org&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;user&quot;&gt;
+    &lt;uid&gt;__dre__&lt;/uid&gt;
+    &lt;guid&gt;CFC88493-DBFF-42B9-ADC7-9B3DA0B0769B&lt;/guid&gt;
+    &lt;short-name&gt;dre&lt;/short-name&gt;
+    &lt;full-name&gt;Andre LaBranche&lt;/full-name&gt;
+    &lt;password&gt;erd&lt;/password&gt;
+    &lt;email&gt;dre@bitbucket.calendarserver.org&lt;/email&gt;
+    &lt;email&gt;shared@example.com&lt;/email&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;group&quot;&gt;
+    &lt;uid&gt;__top_group_1__&lt;/uid&gt;
+    &lt;guid&gt;49B350C6-9611-477B-94D9-5516B13856AB&lt;/guid&gt;
+    &lt;short-name&gt;top-group-1&lt;/short-name&gt;
+    &lt;full-name&gt;Top Group 1&lt;/full-name&gt;
+    &lt;email&gt;topgroup1@example.com&lt;/email&gt;
+    &lt;member-uid&gt;__wsanchez__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__glyph__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__sub_group_1__&lt;/member-uid&gt;
+  &lt;/record&gt;
+
+  &lt;record type=&quot;group&quot;&gt;
+    &lt;uid&gt;__sub_group_1__&lt;/uid&gt;
+    &lt;guid&gt;86144F73-345A-4097-82F1-B782672087C7&lt;/guid&gt;
+    &lt;short-name&gt;sub-group-1&lt;/short-name&gt;
+    &lt;full-name&gt;Sub Group 1&lt;/full-name&gt;
+    &lt;email&gt;subgroup1@example.com&lt;/email&gt;
+    &lt;member-uid&gt;__sagen__&lt;/member-uid&gt;
+    &lt;member-uid&gt;__cdaboo__&lt;/member-uid&gt;
+  &lt;/record&gt;
+
+&lt;/directory&gt;
+&quot;&quot;&quot;
</ins></span></pre>
</div>
</div>

</body>
</html>