<!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>[13198] CalendarServer/trunk/txdav</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/13198">13198</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-04-08 11:01:59 -0700 (Tue, 08 Apr 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>make addDelegate idempotent; add delegation-related indices to schema</pre>

<h3>Modified Paths</h3>
<ul>
<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>
<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="#CalendarServertrunktxdavwhotesttest_delegatespy">CalendarServer/trunk/txdav/who/test/test_delegates.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktxdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql.py (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql.py        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -1452,6 +1452,7 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
</ins><span class="cx">     def addDelegate(self, delegator, delegate, readWrite):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Adds a row to the DELEGATES table.  The delegate should not be a
</span><span class="lines">@@ -1465,14 +1466,21 @@
</span><span class="cx">             read-only access
</span><span class="cx">         @type readWrite: C{boolean}
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        return self._addDelegateQuery.on(
-            self,
-            delegator=delegator.encode(&quot;utf-8&quot;),
-            delegate=delegate.encode(&quot;utf-8&quot;),
-            readWrite=1 if readWrite else 0
-        )
</del><ins>+        savepoint = SavepointAction(&quot;addDelegate&quot;)
+        yield savepoint.acquire(self)
+        try:
+            yield self._addDelegateQuery.on(
+                self,
+                delegator=delegator.encode(&quot;utf-8&quot;),
+                delegate=delegate.encode(&quot;utf-8&quot;),
+                readWrite=1 if readWrite else 0
+            )
+        except Exception:
+            # Row already exists
+            yield savepoint.rollback(self)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
</ins><span class="cx">     def addDelegateGroup(self, delegator, delegateGroupID, readWrite,
</span><span class="cx">                          isExternal=False):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -1487,13 +1495,19 @@
</span><span class="cx">             read-only access
</span><span class="cx">         @type readWrite: C{boolean}
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        return self._addDelegateGroupQuery.on(
-            self,
-            delegator=delegator.encode(&quot;utf-8&quot;),
-            groupID=delegateGroupID,
-            readWrite=1 if readWrite else 0,
-            isExternal=1 if isExternal else 0
-        )
</del><ins>+        savepoint = SavepointAction(&quot;addDelegateGroup&quot;)
+        yield savepoint.acquire(self)
+        try:
+            yield self._addDelegateGroupQuery.on(
+                self,
+                delegator=delegator.encode(&quot;utf-8&quot;),
+                groupID=delegateGroupID,
+                readWrite=1 if readWrite else 0,
+                isExternal=1 if isExternal else 0
+            )
+        except Exception:
+            # Row already exists
+            yield savepoint.rollback(self)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def removeDelegate(self, delegator, delegate, readWrite):
</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 (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -8,7 +8,7 @@
</span><span class="cx">     &quot;HOSTNAME&quot; nvarchar2(255),
</span><span class="cx">     &quot;PID&quot; integer not null,
</span><span class="cx">     &quot;PORT&quot; integer not null,
</span><del>-    &quot;TIME&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC' not null,
</del><ins>+    &quot;TIME&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC' not null, 
</ins><span class="cx">     primary key(&quot;HOSTNAME&quot;, &quot;PORT&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -80,7 +80,7 @@
</span><span class="cx">     &quot;NOTIFICATION_DATA&quot; nclob,
</span><span class="cx">     &quot;MD5&quot; nchar(32),
</span><span class="cx">     &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</span><del>-    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</del><ins>+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
</ins><span class="cx">     unique(&quot;NOTIFICATION_UID&quot;, &quot;NOTIFICATION_HOME_RESOURCE_ID&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -98,8 +98,8 @@
</span><span class="cx">     &quot;ALARM_VEVENT_ALLDAY&quot; nclob default null,
</span><span class="cx">     &quot;ALARM_VTODO_TIMED&quot; nclob default null,
</span><span class="cx">     &quot;ALARM_VTODO_ALLDAY&quot; nclob default null,
</span><del>-    &quot;TIMEZONE&quot; nclob default null,
-    primary key(&quot;CALENDAR_HOME_RESOURCE_ID&quot;, &quot;CALENDAR_RESOURCE_ID&quot;),
</del><ins>+    &quot;TIMEZONE&quot; nclob default null, 
+    primary key(&quot;CALENDAR_HOME_RESOURCE_ID&quot;, &quot;CALENDAR_RESOURCE_ID&quot;), 
</ins><span class="cx">     unique(&quot;CALENDAR_HOME_RESOURCE_ID&quot;, &quot;CALENDAR_RESOURCE_NAME&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -149,7 +149,7 @@
</span><span class="cx">     &quot;PRIVATE_COMMENTS&quot; integer default 0 not null,
</span><span class="cx">     &quot;MD5&quot; nchar(32),
</span><span class="cx">     &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</span><del>-    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</del><ins>+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
</ins><span class="cx">     unique(&quot;CALENDAR_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -213,8 +213,8 @@
</span><span class="cx"> create table ATTACHMENT_CALENDAR_OBJECT (
</span><span class="cx">     &quot;ATTACHMENT_ID&quot; integer not null references ATTACHMENT on delete cascade,
</span><span class="cx">     &quot;MANAGED_ID&quot; nvarchar2(255),
</span><del>-    &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;),
</del><ins>+    &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;), 
</ins><span class="cx">     unique(&quot;MANAGED_ID&quot;, &quot;CALENDAR_OBJECT_RESOURCE_ID&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -222,7 +222,7 @@
</span><span class="cx">     &quot;RESOURCE_ID&quot; integer not null,
</span><span class="cx">     &quot;NAME&quot; nvarchar2(255),
</span><span class="cx">     &quot;VALUE&quot; nclob,
</span><del>-    &quot;VIEWER_UID&quot; nvarchar2(255),
</del><ins>+    &quot;VIEWER_UID&quot; nvarchar2(255), 
</ins><span class="cx">     primary key(&quot;RESOURCE_ID&quot;, &quot;NAME&quot;, &quot;VIEWER_UID&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -249,8 +249,8 @@
</span><span class="cx">     &quot;BIND_MODE&quot; integer not null,
</span><span class="cx">     &quot;BIND_STATUS&quot; integer not null,
</span><span class="cx">     &quot;BIND_REVISION&quot; integer default 0 not null,
</span><del>-    &quot;MESSAGE&quot; nclob,
-    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;OWNER_HOME_RESOURCE_ID&quot;),
</del><ins>+    &quot;MESSAGE&quot; nclob, 
+    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;OWNER_HOME_RESOURCE_ID&quot;), 
</ins><span class="cx">     unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;ADDRESSBOOK_RESOURCE_NAME&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -263,8 +263,8 @@
</span><span class="cx">     &quot;KIND&quot; integer not null,
</span><span class="cx">     &quot;MD5&quot; nchar(32),
</span><span class="cx">     &quot;CREATED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</span><del>-    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
-    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;),
</del><ins>+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
+    unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;), 
</ins><span class="cx">     unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;VCARD_UID&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -283,14 +283,14 @@
</span><span class="cx">     &quot;MEMBER_ID&quot; integer not null,
</span><span class="cx">     &quot;REVISION&quot; integer not null,
</span><span class="cx">     &quot;REMOVED&quot; integer default 0 not null,
</span><del>-    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</del><ins>+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
</ins><span class="cx">     primary key(&quot;GROUP_ID&quot;, &quot;MEMBER_ID&quot;, &quot;REVISION&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table ABO_FOREIGN_MEMBERS (
</span><span class="cx">     &quot;GROUP_ID&quot; integer not null references ADDRESSBOOK_OBJECT on delete cascade,
</span><span class="cx">     &quot;ADDRESSBOOK_ID&quot; integer not null references ADDRESSBOOK_HOME on delete cascade,
</span><del>-    &quot;MEMBER_ADDRESS&quot; nvarchar2(255),
</del><ins>+    &quot;MEMBER_ADDRESS&quot; nvarchar2(255), 
</ins><span class="cx">     primary key(&quot;GROUP_ID&quot;, &quot;MEMBER_ADDRESS&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -302,8 +302,8 @@
</span><span class="cx">     &quot;BIND_MODE&quot; integer not null,
</span><span class="cx">     &quot;BIND_STATUS&quot; integer not null,
</span><span class="cx">     &quot;BIND_REVISION&quot; integer default 0 not null,
</span><del>-    &quot;MESSAGE&quot; nclob,
-    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;GROUP_RESOURCE_ID&quot;),
</del><ins>+    &quot;MESSAGE&quot; nclob, 
+    primary key(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;GROUP_RESOURCE_ID&quot;), 
</ins><span class="cx">     unique(&quot;ADDRESSBOOK_HOME_RESOURCE_ID&quot;, &quot;GROUP_ADDRESSBOOK_NAME&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -333,7 +333,7 @@
</span><span class="cx">     &quot;RESOURCE_NAME&quot; nvarchar2(255),
</span><span class="cx">     &quot;REVISION&quot; integer not null,
</span><span class="cx">     &quot;DELETED&quot; integer not null,
</span><del>-    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</del><ins>+    &quot;MODIFIED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
</ins><span class="cx">     unique(&quot;NOTIFICATION_HOME_RESOURCE_ID&quot;, &quot;RESOURCE_NAME&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -343,7 +343,7 @@
</span><span class="cx">     &quot;MODIFIED&quot; integer not null,
</span><span class="cx">     &quot;SUBSCRIBER_GUID&quot; nvarchar2(255),
</span><span class="cx">     &quot;USER_AGENT&quot; nvarchar2(255) default null,
</span><del>-    &quot;IP_ADDR&quot; nvarchar2(255) default null,
</del><ins>+    &quot;IP_ADDR&quot; nvarchar2(255) default null, 
</ins><span class="cx">     primary key(&quot;TOKEN&quot;, &quot;RESOURCE_KEY&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -352,7 +352,7 @@
</span><span class="cx">     &quot;ORGANIZER&quot; nvarchar2(255),
</span><span class="cx">     &quot;ATTENDEE&quot; nvarchar2(255),
</span><span class="cx">     &quot;ICALUID&quot; nvarchar2(255),
</span><del>-    &quot;ACCESSED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC',
</del><ins>+    &quot;ACCESSED&quot; timestamp default CURRENT_TIMESTAMP at time zone 'UTC', 
</ins><span class="cx">     primary key(&quot;ORGANIZER&quot;, &quot;ATTENDEE&quot;, &quot;ICALUID&quot;)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -426,18 +426,20 @@
</span><span class="cx"> create table DELEGATES (
</span><span class="cx">     &quot;DELEGATOR&quot; nvarchar2(255),
</span><span class="cx">     &quot;DELEGATE&quot; nvarchar2(255),
</span><del>-    &quot;READ_WRITE&quot; integer not null
</del><ins>+    &quot;READ_WRITE&quot; integer not null, 
+    primary key(&quot;DELEGATOR&quot;, &quot;READ_WRITE&quot;, &quot;DELEGATE&quot;)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table DELEGATE_GROUPS (
</span><span class="cx">     &quot;DELEGATOR&quot; nvarchar2(255),
</span><span class="cx">     &quot;GROUP_ID&quot; integer not null,
</span><span class="cx">     &quot;READ_WRITE&quot; integer not null,
</span><del>-    &quot;IS_EXTERNAL&quot; integer not null
</del><ins>+    &quot;IS_EXTERNAL&quot; integer not null, 
+    primary key(&quot;DELEGATOR&quot;, &quot;READ_WRITE&quot;, &quot;GROUP_ID&quot;)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table EXTERNAL_DELEGATE_GROUPS (
</span><del>-    &quot;DELEGATOR&quot; nvarchar2(255),
</del><ins>+    &quot;DELEGATOR&quot; nvarchar2(255) primary key,
</ins><span class="cx">     &quot;GROUP_UID_READ&quot; nvarchar2(255),
</span><span class="cx">     &quot;GROUP_UID_WRITE&quot; nvarchar2(255)
</span><span class="cx"> );
</span><span class="lines">@@ -698,7 +700,7 @@
</span><span class="cx">     JOB_ID
</span><span class="cx"> );
</span><span class="cx"> 
</span><del>-create index GROUPS_GROUP_UID_ebf7a1d4 on GROUPS (
</del><ins>+create index GROUPS_GROUP_UID_b35cce23 on GROUPS (
</ins><span class="cx">     GROUP_UID
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -710,6 +712,12 @@
</span><span class="cx">     MEMBER_UID
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create index DELEGATE_TO_DELEGATOR_5e149b11 on DELEGATES (
+    DELEGATE,
+    READ_WRITE,
+    DELEGATOR
+);
+
</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 class="lines">@@ -800,12 +808,12 @@
</span><span class="cx"> -- Extras
</span><span class="cx"> 
</span><span class="cx"> create or replace function next_job return integer is
</span><del>-  cursor c1 is select ID from JOB for update skip locked;
</del><ins>+  cursor c1 is select JOB_ID from JOB for update skip locked;
</ins><span class="cx">   result integer;
</span><span class="cx"> begin
</span><span class="cx">   open c1;
</span><span class="cx">   fetch c1 into result;
</span><del>-  select ID from JOB where ID = result for update;
</del><ins>+  select JOB_ID from JOB where ID = result for update;
</ins><span class="cx">   return result;
</span><span class="cx"> end;
</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 (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -806,18 +806,24 @@
</span><span class="cx"> create table DELEGATES (
</span><span class="cx">   DELEGATOR                     varchar(255) not null,
</span><span class="cx">   DELEGATE                      varchar(255) not null,
</span><del>-  READ_WRITE                    integer      not null -- 1 = ReadWrite, 0 = ReadOnly
</del><ins>+  READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+
+  primary key (DELEGATOR, READ_WRITE, DELEGATE)
</ins><span class="cx"> );
</span><ins>+create index DELEGATE_TO_DELEGATOR on
+  DELEGATES(DELEGATE, READ_WRITE, DELEGATOR);
</ins><span class="cx"> 
</span><span class="cx"> create table DELEGATE_GROUPS (
</span><span class="cx">   DELEGATOR                     varchar(255) not null,
</span><span class="cx">   GROUP_ID                      integer      not null,
</span><span class="cx">   READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
</span><del>-  IS_EXTERNAL                   integer      not null -- 1 = ReadWrite, 0 = ReadOnly
</del><ins>+  IS_EXTERNAL                   integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+
+  primary key (DELEGATOR, READ_WRITE, GROUP_ID)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table EXTERNAL_DELEGATE_GROUPS (
</span><del>-  DELEGATOR                     varchar(255) not null,
</del><ins>+  DELEGATOR                     varchar(255) primary key not null,
</ins><span class="cx">   GROUP_UID_READ                varchar(255),
</span><span class="cx">   GROUP_UID_WRITE               varchar(255)
</span><span class="cx"> );
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradesoracledialectupgrade_from_34_to_35sql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_34_to_35.sql        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -55,24 +55,26 @@
</span><span class="cx"> create table DELEGATES (
</span><span class="cx">     &quot;DELEGATOR&quot; nvarchar2(255),
</span><span class="cx">     &quot;DELEGATE&quot; nvarchar2(255),
</span><del>-    &quot;READ_WRITE&quot; integer not null
</del><ins>+    &quot;READ_WRITE&quot; integer not null,
+    primary key(&quot;DELEGATOR&quot;, &quot;READ_WRITE&quot;, &quot;DELEGATE&quot;)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table DELEGATE_GROUPS (
</span><span class="cx">     &quot;DELEGATOR&quot; nvarchar2(255),
</span><span class="cx">     &quot;GROUP_ID&quot; integer not null,
</span><span class="cx">     &quot;READ_WRITE&quot; integer not null,
</span><del>-    &quot;IS_EXTERNAL&quot; integer not null
</del><ins>+    &quot;IS_EXTERNAL&quot; integer not null,
+    primary key(&quot;DELEGATOR&quot;, &quot;READ_WRITE&quot;, &quot;GROUP_ID&quot;)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table EXTERNAL_DELEGATE_GROUPS (
</span><del>-    &quot;DELEGATOR&quot; nvarchar2(255),
</del><ins>+    &quot;DELEGATOR&quot; nvarchar2(255) primary key,
</ins><span class="cx">     &quot;GROUP_UID_READ&quot; nvarchar2(255),
</span><span class="cx">     &quot;GROUP_UID_WRITE&quot; nvarchar2(255)
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-create index GROUPS_GROUP_UID_ebf7a1d4 on GROUPS (
</del><ins>+create index GROUPS_GROUP_UID_b35cce23 on GROUPS (
</ins><span class="cx">     GROUP_UID
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -84,7 +86,13 @@
</span><span class="cx">     MEMBER_UID
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create index DELEGATE_TO_DELEGATOR_5e149b11 on DELEGATES (
+    DELEGATE,
+    READ_WRITE,
+    DELEGATOR
+);
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> -- Now update the version
</span><span class="cx"> -- No data upgrades
</span><span class="cx"> update CALENDARSERVER set VALUE = '35' where NAME = 'VERSION';
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradespostgresdialectupgrade_from_34_to_35sql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_34_to_35.sql        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -38,7 +38,7 @@
</span><span class="cx"> create table GROUPS (
</span><span class="cx">   GROUP_ID                      integer      primary key default nextval('RESOURCE_ID_SEQ'),    -- implicit index
</span><span class="cx">   NAME                          varchar(255) not null,
</span><del>-  GROUP_UID                    varchar(255) not null,
</del><ins>+  GROUP_UID                     varchar(255) not null,
</ins><span class="cx">   MEMBERSHIP_HASH               varchar(255) not null,
</span><span class="cx">   EXTANT                        integer default 1,
</span><span class="cx">   CREATED                       timestamp default timezone('UTC', CURRENT_TIMESTAMP),
</span><span class="lines">@@ -48,7 +48,7 @@
</span><span class="cx"> 
</span><span class="cx"> create table GROUP_MEMBERSHIP (
</span><span class="cx">   GROUP_ID                      integer,
</span><del>-  MEMBER_UID                   varchar(255) not null
</del><ins>+  MEMBER_UID                    varchar(255) not null
</ins><span class="cx"> );
</span><span class="cx"> create index GROUP_MEMBERSHIP_GROUP on GROUP_MEMBERSHIP(GROUP_ID);
</span><span class="cx"> create index GROUP_MEMBERSHIP_MEMBER on GROUP_MEMBERSHIP(MEMBER_UID);
</span><span class="lines">@@ -66,20 +66,27 @@
</span><span class="cx"> create table DELEGATES (
</span><span class="cx">   DELEGATOR                     varchar(255) not null,
</span><span class="cx">   DELEGATE                      varchar(255) not null,
</span><del>-  READ_WRITE                    integer      not null -- 1 = ReadWrite, 0 = ReadOnly
</del><ins>+  READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+
+  primary key (DELEGATOR, READ_WRITE, DELEGATE)
</ins><span class="cx"> );
</span><ins>+create index DELEGATE_TO_DELEGATOR on
+  DELEGATES(DELEGATE, READ_WRITE, DELEGATOR);
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> create table DELEGATE_GROUPS (
</span><span class="cx">   DELEGATOR                     varchar(255) not null,
</span><span class="cx">   GROUP_ID                      integer      not null,
</span><span class="cx">   READ_WRITE                    integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
</span><del>-  IS_EXTERNAL                   integer      not null -- 1 = ReadWrite, 0 = ReadOnly
</del><ins>+  IS_EXTERNAL                   integer      not null, -- 1 = ReadWrite, 0 = ReadOnly
+
+  primary key (DELEGATOR, READ_WRITE, GROUP_ID)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> create table EXTERNAL_DELEGATE_GROUPS (
</span><del>-  DELEGATOR                     varchar(255) not null,
-  GROUP_UID_READ               varchar(255),
-  GROUP_UID_WRITE              varchar(255)
</del><ins>+  DELEGATOR                     varchar(255) primary key not null,
+  GROUP_UID_READ                varchar(255),
+  GROUP_UID_WRITE               varchar(255)
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> -- Now update the version
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_delegatespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/test/test_delegates.py (13197 => 13198)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_delegates.py        2014-04-08 16:31:32 UTC (rev 13197)
+++ CalendarServer/trunk/txdav/who/test/test_delegates.py        2014-04-08 18:01:59 UTC (rev 13198)
</span><span class="lines">@@ -39,7 +39,7 @@
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_directDelegation(self):
</span><del>-        txn = self.store.newTransaction()
</del><ins>+        txn = self.store.newTransaction(label=&quot;test_directDelegation&quot;)
</ins><span class="cx"> 
</span><span class="cx">         delegator = yield self.directory.recordWithUID(u&quot;__wsanchez1__&quot;)
</span><span class="cx">         delegate1 = yield self.directory.recordWithUID(u&quot;__sagen1__&quot;)
</span><span class="lines">@@ -53,7 +53,7 @@
</span><span class="cx">         self.assertEquals([u&quot;__wsanchez1__&quot;], [d.uid for d in delegators])
</span><span class="cx"> 
</span><span class="cx">         yield txn.commit()  # So delegateService will see the changes
</span><del>-        txn = self.store.newTransaction()
</del><ins>+        txn = self.store.newTransaction(label=&quot;test_directDelegation&quot;)
</ins><span class="cx"> 
</span><span class="cx">         # The &quot;proxy-write&quot; pseudoGroup will have one member
</span><span class="cx">         pseudoGroup = yield self.directory.recordWithShortName(
</span><span class="lines">@@ -130,7 +130,7 @@
</span><span class="cx">         yield pseudoGroup.setMembers([delegate1, delegate2])
</span><span class="cx"> 
</span><span class="cx">         # Verify the assignments were made
</span><del>-        txn = self.store.newTransaction()
</del><ins>+        txn = self.store.newTransaction(label=&quot;test_directDelegation&quot;)
</ins><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True))
</span><span class="cx">         self.assertEquals(
</span><span class="cx">             set([u&quot;__sagen1__&quot;, u&quot;__cdaboo1__&quot;]),
</span><span class="lines">@@ -142,7 +142,7 @@
</span><span class="cx">         yield pseudoGroup.setMembers([delegate2])
</span><span class="cx"> 
</span><span class="cx">         # Verify the assignments were made
</span><del>-        txn = self.store.newTransaction()
</del><ins>+        txn = self.store.newTransaction(label=&quot;test_directDelegation&quot;)
</ins><span class="cx">         delegates = (yield delegatesOf(txn, delegator, True))
</span><span class="cx">         self.assertEquals(
</span><span class="cx">             set([u&quot;__cdaboo1__&quot;]),
</span><span class="lines">@@ -153,7 +153,7 @@
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_indirectDelegation(self):
</span><del>-        txn = self.store.newTransaction()
</del><ins>+        txn = self.store.newTransaction(label=&quot;test_indirectDelegation&quot;)
</ins><span class="cx"> 
</span><span class="cx">         delegator = yield self.directory.recordWithUID(u&quot;__wsanchez1__&quot;)
</span><span class="cx">         delegate1 = yield self.directory.recordWithUID(u&quot;__sagen1__&quot;)
</span><span class="lines">@@ -233,3 +233,55 @@
</span><span class="cx">             set([d.uid for d in delegates])
</span><span class="cx">         )
</span><span class="cx">         yield txn.commit()
</span><ins>+
+
+    @inlineCallbacks
+    def test_noDuplication(self):
+        &quot;&quot;&quot;
+        Make sure addDelegate( ) is idempotent
+        &quot;&quot;&quot;
+        delegator = yield self.directory.recordWithUID(u&quot;__wsanchez1__&quot;)
+
+        # Delegate users:
+        delegate1 = yield self.directory.recordWithUID(u&quot;__sagen1__&quot;)
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        yield addDelegate(txn, delegator, delegate1, True)
+        yield txn.commit()
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        yield addDelegate(txn, delegator, delegate1, True)
+        yield txn.commit()
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        results = (
+            yield txn._selectDelegatesQuery.on(
+                txn,
+                delegator=delegator.uid.encode(&quot;utf-8&quot;),
+                readWrite=1
+            )
+        )
+        yield txn.commit()
+        self.assertEquals([[&quot;__sagen1__&quot;]], results)
+
+        # Delegate groups:
+        group1 = yield self.directory.recordWithUID(u&quot;__top_group_1__&quot;)
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        yield addDelegate(txn, delegator, group1, True)
+        yield txn.commit()
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        yield addDelegate(txn, delegator, group1, True)
+        yield txn.commit()
+
+        txn = self.store.newTransaction(label=&quot;test_noDuplication&quot;)
+        results = (
+            yield txn._selectDelegateGroupsQuery.on(
+                txn,
+                delegator=delegator.uid.encode(&quot;utf-8&quot;),
+                readWrite=1
+            )
+        )
+        yield txn.commit()
+        self.assertEquals([[&quot;__top_group_1__&quot;]], results)
</ins></span></pre>
</div>
</div>

</body>
</html>