<!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>[13814] 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/13814">13814</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2014-07-31 19:18:10 -0700 (Thu, 31 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fix proxy and mailgateway migration steps.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavdirectorycalendaruserproxypy">CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavdirectorycalendaruserproxyloaderpy">CalendarServer/trunk/twistedcaldav/directory/calendaruserproxyloader.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavtesttest_upgradepy">CalendarServer/trunk/twistedcaldav/test/test_upgrade.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavupgradepy">CalendarServer/trunk/twistedcaldav/upgrade.py</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastoreschedulingimipmailgatewaypy">CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/mailgateway.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavdirectorycalendaruserproxypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py (13813 => 13814)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py        2014-08-01 01:26:51 UTC (rev 13813)
+++ CalendarServer/trunk/twistedcaldav/directory/calendaruserproxy.py        2014-08-01 02:18:10 UTC (rev 13814)
</span><span class="lines">@@ -880,8 +880,9 @@
</span><span class="cx"> 
</span><span class="cx">     def __init__(self, dbpath):
</span><span class="cx"> 
</span><ins>+        self.dbpath = fullServerPath(config.DataRoot, dbpath)
</ins><span class="cx">         ADBAPISqliteMixin.__init__(self)
</span><del>-        ProxyDB.__init__(self, &quot;Proxies&quot;, &quot;sqlite3&quot;, (fullServerPath(config.DataRoot, dbpath),))
</del><ins>+        ProxyDB.__init__(self, &quot;Proxies&quot;, &quot;sqlite3&quot;, (self.dbpath,))
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavdirectorycalendaruserproxyloaderpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/directory/calendaruserproxyloader.py (13813 => 13814)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/directory/calendaruserproxyloader.py        2014-08-01 01:26:51 UTC (rev 13813)
+++ CalendarServer/trunk/twistedcaldav/directory/calendaruserproxyloader.py        2014-08-01 02:18:10 UTC (rev 13814)
</span><span class="lines">@@ -31,6 +31,8 @@
</span><span class="cx"> from twistedcaldav.config import config, fullServerPath
</span><span class="cx"> from twistedcaldav.xmlutil import readXML
</span><span class="cx"> 
</span><ins>+from txdav.who.delegates import addDelegate
+
</ins><span class="cx"> log = Logger()
</span><span class="cx"> 
</span><span class="cx"> ELEMENT_PROXIES = &quot;proxies&quot;
</span><span class="lines">@@ -52,11 +54,10 @@
</span><span class="cx">         return &quot;&lt;%s %r&gt;&quot; % (self.__class__.__name__, self.xmlFile)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def __init__(self, xmlFile, service):
</del><ins>+    def __init__(self, xmlFile):
</ins><span class="cx"> 
</span><span class="cx">         self.items = []
</span><span class="cx">         self.xmlFile = fullServerPath(config.DataRoot, xmlFile)
</span><del>-        self.service = service
</del><span class="cx"> 
</span><span class="cx">         # Read in XML
</span><span class="cx">         try:
</span><span class="lines">@@ -130,10 +131,42 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def updateProxyDB(self):
</del><ins>+    def updateProxyDB(self, db):
+        &quot;&quot;&quot;
+        Move the XML file proxies over to the proxyDB.
+        &quot;&quot;&quot;
</ins><span class="cx"> 
</span><del>-        db = self.service
</del><span class="cx">         for item in self.items:
</span><span class="cx">             guid, write_proxies, read_proxies = item
</span><span class="cx">             yield db.setGroupMembers(&quot;%s#%s&quot; % (guid, &quot;calendar-proxy-write&quot;), write_proxies)
</span><span class="cx">             yield db.setGroupMembers(&quot;%s#%s&quot; % (guid, &quot;calendar-proxy-read&quot;), read_proxies)
</span><ins>+
+
+    @inlineCallbacks
+    def updateProxyStore(self, store):
+        &quot;&quot;&quot;
+        Move the XML file proxies over to the store database.
+        &quot;&quot;&quot;
+
+        directory = store.directoryService()
+        txn = store.newTransaction(label=&quot;xmlDelegatesToStore&quot;)
+        for item in self.items:
+            guid, write_proxies, read_proxies = item
+            delegatorRecord = yield directory.recordWithUID(guid)
+            if delegatorRecord is None:
+                continue
+            for proxy in write_proxies:
+                delegateRecord = yield directory.recordWithUID(proxy)
+                if delegateRecord is None:
+                    continue
+
+                yield addDelegate(txn, delegatorRecord, delegateRecord, True)
+
+            for proxy in read_proxies:
+                delegateRecord = yield directory.recordWithUID(proxy)
+                if delegateRecord is None:
+                    continue
+
+                yield addDelegate(txn, delegatorRecord, delegateRecord, False)
+
+        yield txn.commit()
</ins></span></pre></div>
<a id="CalendarServertrunktwistedcaldavtesttest_upgradepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/test/test_upgrade.py (13813 => 13814)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/test/test_upgrade.py        2014-08-01 01:26:51 UTC (rev 13813)
+++ CalendarServer/trunk/twistedcaldav/test/test_upgrade.py        2014-08-01 02:18:10 UTC (rev 13814)
</span><span class="lines">@@ -27,7 +27,7 @@
</span><span class="cx"> from twistedcaldav.upgrade import (
</span><span class="cx">     xattrname, upgradeData, updateFreeBusySet,
</span><span class="cx">     removeIllegalCharacters, normalizeCUAddrs,
</span><del>-    loadDelegatesFromXML, migrateDelegatesToStore,
</del><ins>+    loadDelegatesFromXMLintoProxyDB, migrateDelegatesToStore,
</ins><span class="cx">     upgradeResourcesXML
</span><span class="cx"> )
</span><span class="cx"> from txdav.caldav.datastore.index_file import db_basename
</span><span class="lines">@@ -319,7 +319,7 @@
</span><span class="cx">             &quot;.calendarserver_version&quot; : {
</span><span class="cx">                 &quot;@contents&quot; : &quot;2&quot;,
</span><span class="cx">             },
</span><del>-            MailGatewayTokensDatabase.dbFilename : { &quot;@contents&quot; : None },
</del><ins>+            MailGatewayTokensDatabase.dbFilename : {&quot;@contents&quot; : None},
</ins><span class="cx">             &quot;%s-journal&quot; % (MailGatewayTokensDatabase.dbFilename,) : {
</span><span class="cx">                 &quot;@contents&quot; : None,
</span><span class="cx">                 &quot;@optional&quot; : True,
</span><span class="lines">@@ -1477,10 +1477,10 @@
</span><span class="cx">         # Load delegates from xml into sqlite
</span><span class="cx">         sqliteProxyService = ProxySqliteDB(&quot;proxies.sqlite&quot;)
</span><span class="cx">         proxyFile = os.path.join(config.DataRoot, &quot;proxies.xml&quot;)
</span><del>-        yield loadDelegatesFromXML(proxyFile, sqliteProxyService)
</del><ins>+        yield loadDelegatesFromXMLintoProxyDB(proxyFile, sqliteProxyService)
</ins><span class="cx"> 
</span><span class="cx">         # Load delegates from sqlite into store
</span><del>-        yield migrateDelegatesToStore(sqliteProxyService, store)
</del><ins>+        yield migrateDelegatesToStore(store)
</ins><span class="cx"> 
</span><span class="cx">         # Check delegates in store
</span><span class="cx">         txn = store.newTransaction()
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavupgradepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/upgrade.py (13813 => 13814)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/upgrade.py        2014-08-01 01:26:51 UTC (rev 13813)
+++ CalendarServer/trunk/twistedcaldav/upgrade.py        2014-08-01 02:18:10 UTC (rev 13814)
</span><span class="lines">@@ -600,8 +600,8 @@
</span><span class="cx"> #                                scanCollection(childCollection)
</span><span class="cx"> 
</span><span class="cx">                 if os.path.isdir(calPath):
</span><del>-                    #log.debug(&quot;Regular collection scan: %s&quot; % (calPath,))
-                    #scanCollection(calPath)
</del><ins>+                    # log.debug(&quot;Regular collection scan: %s&quot; % (calPath,))
+                    # scanCollection(calPath)
</ins><span class="cx">                     log.warn(&quot;Regular collection hidden: %s&quot; % (calPath,))
</span><span class="cx">                     os.rename(calPath, os.path.join(calHome, &quot;.collection.&quot; + os.path.basename(calPath)))
</span><span class="cx"> 
</span><span class="lines">@@ -652,6 +652,7 @@
</span><span class="cx">         raise UpgradeError(&quot;Data upgrade failed, see error.log for details&quot;)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> def upgradeResourcesXML(resourcesFilePath):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Convert the old XML format to the twext.who.xml format
</span><span class="lines">@@ -986,14 +987,31 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-def loadDelegatesFromXML(xmlFile, service):
-    loader = XMLCalendarUserProxyLoader(xmlFile, service)
-    return loader.updateProxyDB()
</del><ins>+def loadDelegatesFromXMLintoProxyDB(xmlFile, service):
+    loader = XMLCalendarUserProxyLoader(xmlFile)
+    return loader.updateProxyDB(service)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+def loadDelegatesFromXMLintoStore(xmlFile, store):
+    loader = XMLCalendarUserProxyLoader(xmlFile)
+    return loader.updateProxyStore(store)
+
+
+
</ins><span class="cx"> @inlineCallbacks
</span><del>-def migrateDelegatesToStore(service, store):
</del><ins>+def migrateDelegatesToStore(store):
+    &quot;&quot;&quot;
+    Migrate the old sqlite proxyDB data into the store. Remove the sqlite file
+    afterwards.
+
+    @param store: the store to migrate into
+    @type store: L{CommonDataStore}
+    &quot;&quot;&quot;
+
+    log.warn(&quot;Migrating delegates to the store&quot;)
+
+    service = ProxySqliteDB(&quot;proxies.sqlite&quot;)
</ins><span class="cx">     directory = store.directoryService()
</span><span class="cx">     txn = store.newTransaction(label=&quot;migrateDelegatesToStore&quot;)
</span><span class="cx">     for groupName, memberUID in (
</span><span class="lines">@@ -1018,8 +1036,14 @@
</span><span class="cx"> 
</span><span class="cx">     yield txn.commit()
</span><span class="cx"> 
</span><ins>+    # Remove the old file
+    os.remove(service.dbpath)
+    journalPath = service.dbpath + &quot;-journal&quot;
+    if os.path.exists(journalPath):
+        os.remove(journalPath)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class UpgradeFileSystemFormatStep(object):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Upgrade filesystem from previous versions.
</span><span class="lines">@@ -1089,24 +1113,43 @@
</span><span class="cx">     def stepWithResult(self, result):
</span><span class="cx">         if self.doPostImport:
</span><span class="cx"> 
</span><del>-            # Load proxy assignments from XML if specified
-            if (
-                self.config.ProxyLoadFromFile and
-                os.path.exists(self.config.ProxyLoadFromFile) and
-                not os.path.exists(
-                    os.path.join(
-                        self.config.DataRoot, &quot;proxies.sqlite&quot;
-                    )
-                )
</del><ins>+            # Migrate any proxyDB file that exists - remove it after migration
+            loadDoneFilePath = os.path.join(self.config.DataRoot, &quot;proxies-loaded&quot;)
+            if os.path.exists(
+                os.path.join(self.config.DataRoot, &quot;proxies.sqlite&quot;)
</ins><span class="cx">             ):
</span><del>-                sqliteProxyService = ProxySqliteDB(&quot;proxies.sqlite&quot;)
-                yield loadDelegatesFromXML(
-                    self.config.ProxyLoadFromFile,
-                    sqliteProxyService
</del><ins>+                # Migrate delegate assignments from sqlite to store
+                yield migrateDelegatesToStore(
+                    self.store
</ins><span class="cx">                 )
</span><ins>+
+                # If migration happened and the XML load option is on, write a stub file to prevent
+                # it being loaded on the next restart
+                if (
+                    self.config.ProxyLoadFromFile and
+                    os.path.exists(self.config.ProxyLoadFromFile) and
+                    not os.path.exists(loadDoneFilePath)
+                ):
+                    # Write stub file as indicator loading is done
+                    FilePath(loadDoneFilePath).touch()
+
+            # If no migration, see if there is an XML file to load into the store (once only)
</ins><span class="cx">             else:
</span><del>-                sqliteProxyService = None
</del><ins>+                if (
+                    self.config.ProxyLoadFromFile and
+                    os.path.exists(self.config.ProxyLoadFromFile) and
+                    not os.path.exists(loadDoneFilePath)
+                ):
+                    log.warn(&quot;Loading delegate assignments from XML&quot;)
+                    yield loadDelegatesFromXMLintoStore(
+                        self.config.ProxyLoadFromFile,
+                        self.store
+                    )
</ins><span class="cx"> 
</span><ins>+                    # Write stub file as indicator loading is done
+                    FilePath(loadDoneFilePath).touch()
+
+
</ins><span class="cx">             # # Populate the group membership cache
</span><span class="cx">             # if (self.config.GroupCaching.Enabled and
</span><span class="cx">             #     self.config.GroupCaching.EnableUpdater):
</span><span class="lines">@@ -1125,11 +1168,6 @@
</span><span class="cx">             #         useExternalProxies=self.config.GroupCaching.UseExternalProxies)
</span><span class="cx">             #     yield updater.updateCache(fast=True)
</span><span class="cx"> 
</span><del>-            uid, gid = getCalendarServerIDs(self.config)
-            dbPath = os.path.join(self.config.DataRoot, &quot;proxies.sqlite&quot;)
-            if os.path.exists(dbPath):
-                os.chown(dbPath, uid, gid)
-
</del><span class="cx">             # Process old inbox items
</span><span class="cx">             self.store.setMigrating(True)
</span><span class="cx">             yield self.processInboxItems()
</span><span class="lines">@@ -1138,12 +1176,7 @@
</span><span class="cx">             # Migrate mail tokens from sqlite to store
</span><span class="cx">             yield migrateTokensToStore(self.config.DataRoot, self.store)
</span><span class="cx"> 
</span><del>-            # Migrate delegate assignments from sqlite to store
-            if sqliteProxyService is None:
-                sqliteProxyService = ProxySqliteDB(&quot;proxies.sqlite&quot;)
-            yield migrateDelegatesToStore(sqliteProxyService, self.store)
</del><span class="cx"> 
</span><del>-
</del><span class="cx">     @inlineCallbacks
</span><span class="cx">     def processInboxItems(self):
</span><span class="cx">         &quot;&quot;&quot;
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastoreschedulingimipmailgatewaypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/mailgateway.py (13813 => 13814)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/mailgateway.py        2014-08-01 01:26:51 UTC (rev 13813)
+++ CalendarServer/trunk/txdav/caldav/datastore/scheduling/imip/mailgateway.py        2014-08-01 02:18:10 UTC (rev 13814)
</span><span class="lines">@@ -229,11 +229,13 @@
</span><span class="cx">     @type store: L{CommonDataStore}
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     oldDB = MailGatewayTokensDatabase(path)
</span><del>-    txn = store.newTransaction(label=&quot;migrateTokensToStore&quot;)
-    for token, organizer, attendee, icaluid in oldDB.getAllTokens():
-        yield txn.imipCreateToken(organizer, attendee, icaluid, token=token)
-    yield txn.commit()
-    os.remove(oldDB.dbpath)
-    journalPath = oldDB.dbpath + &quot;-journal&quot;
-    if os.path.exists(journalPath):
-        os.remove(journalPath)
</del><ins>+    if os.path.exists(oldDB.dbpath):
+        txn = store.newTransaction(label=&quot;migrateTokensToStore&quot;)
+        for token, organizer, attendee, icaluid in oldDB.getAllTokens():
+            yield txn.imipCreateToken(organizer, attendee, icaluid, token=token)
+        yield txn.commit()
+
+        os.remove(oldDB.dbpath)
+        journalPath = oldDB.dbpath + &quot;-journal&quot;
+        if os.path.exists(journalPath):
+            os.remove(journalPath)
</ins></span></pre>
</div>
</div>

</body>
</html>