<!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>[14779] CalendarServer/trunk/txdav/common/datastore</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/14779">14779</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-05-12 08:15:17 -0700 (Tue, 12 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Clean-up migration meta-data.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavcommondatastorepoddingmigrationhome_syncpy">CalendarServer/trunk/txdav/common/datastore/podding/migration/home_sync.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastorepoddingmigrationsync_metadatapy">CalendarServer/trunk/txdav/common/datastore/podding/migration/sync_metadata.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastorepoddingmigrationtesttest_migrationpy">CalendarServer/trunk/txdav/common/datastore/podding/migration/test/test_migration.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_54_to_55sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_54_to_55.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaupgradespostgresdialectupgrade_from_54_to_55sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_54_to_55.sql</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktxdavcommondatastorepoddingmigrationhome_syncpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/podding/migration/home_sync.py (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/podding/migration/home_sync.py        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/podding/migration/home_sync.py        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -22,7 +22,8 @@
</span><span class="cx"> from twistedcaldav.accounting import emitAccounting
</span><span class="cx"> from txdav.caldav.icalendarstore import ComponentUpdateState
</span><span class="cx"> from txdav.common.datastore.podding.migration.sync_metadata import CalendarMigrationRecord, \
</span><del>-    CalendarObjectMigrationRecord, AttachmentMigrationRecord
</del><ins>+    CalendarObjectMigrationRecord, AttachmentMigrationRecord, \
+    MigrationCleanupWork
</ins><span class="cx"> from txdav.caldav.datastore.sql import ManagedAttachment, CalendarBindRecord
</span><span class="cx"> from txdav.common.datastore.sql_external import NotificationCollectionExternal
</span><span class="cx"> from txdav.common.datastore.sql_notification import NotificationCollection
</span><span class="lines">@@ -279,8 +280,12 @@
</span><span class="cx">         # Unpause work items
</span><span class="cx">         yield newhome.unpauseWork()
</span><span class="cx"> 
</span><del>-        # TODO: remove migration state
-        pass
</del><ins>+        # Remove migration state
+        yield MigrationCleanupWork.reschedule(
+            txn,
+            MigrationCleanupWork.notBeforeDelay,
+            homeResourceID=newhome.id(),
+        )
</ins><span class="cx"> 
</span><span class="cx">         # TODO: purge the old ones
</span><span class="cx">         pass
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastorepoddingmigrationsync_metadatapy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/podding/migration/sync_metadata.py (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/podding/migration/sync_metadata.py        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/podding/migration/sync_metadata.py        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -16,8 +16,9 @@
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import Record, fromTable
</span><span class="cx"> from twext.enterprise.dal.syntax import Parameter, Delete
</span><ins>+from twext.enterprise.jobqueue import SingletonWorkItem
+from twisted.internet.defer import inlineCallbacks
</ins><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><del>-from twisted.internet.defer import inlineCallbacks
</del><span class="cx"> 
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> Module that manages store-level metadata objects used during the migration process.
</span><span class="lines">@@ -56,3 +57,30 @@
</span><span class="cx">     L{Record} for L{schema.ATTACHMENT_MIGRATION}.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     pass
</span><ins>+
+
+
+class MigrationCleanupWork(SingletonWorkItem, fromTable(schema.MIGRATION_CLEANUP_WORK)):
+
+    group = &quot;homeResourceID&quot;
+
+    notBeforeDelay = 300    # 5 minutes
+
+    @inlineCallbacks
+    def doWork(self):
+        &quot;&quot;&quot;
+        Delete all the corresponding migration records.
+        &quot;&quot;&quot;
+
+        yield CalendarMigrationRecord.deletesome(
+            self.transaction,
+            CalendarMigrationRecord.calendarHomeResourceID == self.homeResourceID,
+        )
+        yield CalendarObjectMigrationRecord.deletesome(
+            self.transaction,
+            CalendarObjectMigrationRecord.calendarHomeResourceID == self.homeResourceID,
+        )
+        yield AttachmentMigrationRecord.deletesome(
+            self.transaction,
+            AttachmentMigrationRecord.calendarHomeResourceID == self.homeResourceID,
+        )
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastorepoddingmigrationtesttest_migrationpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/podding/migration/test/test_migration.py (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/podding/migration/test/test_migration.py        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/podding/migration/test/test_migration.py        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -15,23 +15,33 @@
</span><span class="cx"> ##
</span><span class="cx"> 
</span><span class="cx"> from pycalendar.datetime import DateTime
</span><ins>+
+from twext.enterprise.jobqueue import JobItem
+
+from twisted.internet import reactor
</ins><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="cx"> from twisted.python.filepath import FilePath
</span><ins>+
</ins><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from twistedcaldav.ical import Component
</span><ins>+
+from txdav.caldav.datastore.scheduling.ischedule.delivery import IScheduleRequest
+from txdav.caldav.datastore.scheduling.ischedule.resource import IScheduleInboxResource
+from txdav.caldav.datastore.test.common import CaptureProtocol
</ins><span class="cx"> from txdav.common.datastore.podding.migration.home_sync import CrossPodHomeSync
</span><ins>+from txdav.common.datastore.podding.migration.sync_metadata import CalendarMigrationRecord, \
+    AttachmentMigrationRecord, CalendarObjectMigrationRecord, \
+    MigrationCleanupWork
</ins><span class="cx"> from txdav.common.datastore.podding.test.util import MultiStoreConduitTest
</span><span class="cx"> from txdav.common.datastore.sql_tables import _BIND_MODE_READ, \
</span><span class="cx">     _HOME_STATUS_DISABLED, _HOME_STATUS_NORMAL, _HOME_STATUS_EXTERNAL, \
</span><span class="cx">     _HOME_STATUS_MIGRATING
</span><span class="cx"> from txdav.common.datastore.test.util import populateCalendarsFrom
</span><span class="cx"> from txdav.who.delegates import Delegates
</span><ins>+
+from txweb2.dav.test.util import SimpleRequest
</ins><span class="cx"> from txweb2.http_headers import MimeType
</span><span class="cx"> from txweb2.stream import MemoryStream
</span><del>-from txdav.caldav.datastore.scheduling.ischedule.delivery import IScheduleRequest
-from txdav.caldav.datastore.scheduling.ischedule.resource import IScheduleInboxResource
-from txweb2.dav.test.util import SimpleRequest
-from txdav.caldav.datastore.test.common import CaptureProtocol
</del><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> class TestCompleteMigrationCycle(MultiStoreConduitTest):
</span><span class="lines">@@ -66,7 +76,10 @@
</span><span class="cx">         yield super(TestCompleteMigrationCycle, self).setUp()
</span><span class="cx">         yield self.populate()
</span><span class="cx"> 
</span><ins>+        # Speed up work
+        self.patch(MigrationCleanupWork, &quot;notBeforeDelay&quot;, 1)
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">     def configure(self):
</span><span class="cx">         super(TestCompleteMigrationCycle, self).configure()
</span><span class="cx">         config.GroupAttendees.Enabled = True
</span><span class="lines">@@ -648,7 +661,14 @@
</span><span class="cx">         data = yield self.attachmentToString(attachment)
</span><span class="cx">         self.assertEqual(data, &quot;Here is some text #1.&quot;)
</span><span class="cx"> 
</span><ins>+        # No migration data left
+        txn = self.theTransactionUnderTest(1)
+        yield JobItem.waitEmpty(self.theStoreUnderTest(1).newTransaction, reactor, 60)
+        for migrationType in (CalendarMigrationRecord, CalendarObjectMigrationRecord, AttachmentMigrationRecord,):
+            records = yield migrationType.all(txn)
+            self.assertEqual(len(records), 0, msg=migrationType.__name__)
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_migration(self):
</span><span class="cx">         &quot;&quot;&quot;
</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 (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current-oracle-dialect.sql        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -646,6 +646,12 @@
</span><span class="cx">     &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create table MIGRATION_CLEANUP_WORK (
+    &quot;WORK_ID&quot; integer primary key,
+    &quot;JOB_ID&quot; integer not null references JOB,
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade
+);
+
</ins><span class="cx"> create table CALENDARSERVER (
</span><span class="cx">     &quot;NAME&quot; nvarchar2(255) primary key,
</span><span class="cx">     &quot;VALUE&quot; nvarchar2(255)
</span><span class="lines">@@ -997,4 +1003,8 @@
</span><span class="cx">     &quot;HOME_RESOURCE_ID&quot;
</span><span class="cx"> );
</span><span class="cx"> 
</span><ins>+create index MIGRATION_CLEANUP_WOR_8c23cc35 on MIGRATION_CLEANUP_WORK (
+    &quot;JOB_ID&quot;
+);
+
</ins><span class="cx"> -- Extra schema to add to current-oracle-dialect.sql
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemacurrentsql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -1219,6 +1219,19 @@
</span><span class="cx">   PRINCIPAL_PURGE_HOME_WORK(HOME_RESOURCE_ID);
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+----------------------------
+-- Migration Cleanup Work --
+----------------------------
+
+create table MIGRATION_CLEANUP_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ'), -- implicit index
+  JOB_ID                        integer      references JOB not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade
+);
+
+create index MIGRATION_CLEANUP_WORK_JOB_ID on
+  MIGRATION_CLEANUP_WORK(JOB_ID);
+
</ins><span class="cx"> --------------------
</span><span class="cx"> -- Schema Version --
</span><span class="cx"> --------------------
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradesoracledialectupgrade_from_54_to_55sql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_54_to_55.sql (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_54_to_55.sql        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/oracle-dialect/upgrade_from_54_to_55.sql        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -22,5 +22,16 @@
</span><span class="cx"> alter table JOB
</span><span class="cx">   add (&quot;PAUSE&quot; integer default 0);
</span><span class="cx"> 
</span><ins>+-- New table
+create table MIGRATION_CLEANUP_WORK (
+    &quot;WORK_ID&quot; integer primary key,
+    &quot;JOB_ID&quot; integer not null references JOB,
+    &quot;HOME_RESOURCE_ID&quot; integer not null references CALENDAR_HOME on delete cascade
+);
+
+create index MIGRATION_CLEANUP_WOR_8c23cc35 on MIGRATION_CLEANUP_WORK (
+    &quot;JOB_ID&quot;
+);
+
</ins><span class="cx"> -- update the version
</span><span class="cx"> update CALENDARSERVER set VALUE = '55' where NAME = 'VERSION';
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradespostgresdialectupgrade_from_54_to_55sql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_54_to_55.sql (14778 => 14779)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_54_to_55.sql        2015-05-12 00:44:44 UTC (rev 14778)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_54_to_55.sql        2015-05-12 15:15:17 UTC (rev 14779)
</span><span class="lines">@@ -22,5 +22,15 @@
</span><span class="cx"> alter table JOB
</span><span class="cx">   add column PAUSE integer default 0;
</span><span class="cx"> 
</span><ins>+-- New Table
+create table MIGRATION_CLEANUP_WORK (
+  WORK_ID                       integer      primary key default nextval('WORKITEM_SEQ'), -- implicit index
+  JOB_ID                        integer      references JOB not null,
+  HOME_RESOURCE_ID              integer      not null references CALENDAR_HOME on delete cascade
+);
+
+create index MIGRATION_CLEANUP_WORK_JOB_ID on
+  MIGRATION_CLEANUP_WORK(JOB_ID);
+
</ins><span class="cx"> -- update the version
</span><span class="cx"> update CALENDARSERVER set VALUE = '55' where NAME = 'VERSION';
</span></span></pre>
</div>
</div>

</body>
</html>