[CalendarServer-changes] [6275] CalendarServer/trunk/txdav/common/datastore

source_changes at macosforge.org source_changes at macosforge.org
Fri Sep 10 13:11:02 PDT 2010


Revision: 6275
          http://trac.macosforge.org/projects/calendarserver/changeset/6275
Author:   glyph at apple.com
Date:     2010-09-10 13:11:01 -0700 (Fri, 10 Sep 2010)
Log Message:
-----------
A little test coverage for full cross-store migration.  Preserve data rather than deleting it.

Modified Paths:
--------------
    CalendarServer/trunk/txdav/common/datastore/util.py

Added Paths:
-----------
    CalendarServer/trunk/txdav/common/datastore/test/test_util.py

Added: CalendarServer/trunk/txdav/common/datastore/test/test_util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/test_util.py	                        (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_util.py	2010-09-10 20:11:01 UTC (rev 6275)
@@ -0,0 +1,84 @@
+##
+# Copyright (c) 2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# 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 "AS IS" 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.
+##
+
+"""
+Tests for L{txdav.common.datastore.util}.
+"""
+
+from twisted.trial.unittest import TestCase
+from twext.python.filepath import CachingFilePath
+
+from twisted.application.service import Service, MultiService
+from txdav.common.datastore.util import UpgradeToDatabaseService
+from txdav.common.datastore.file import CommonDataStore
+from txdav.common.datastore.test.util import theStoreBuilder, \
+    populateCalendarsFrom
+from txdav.caldav.datastore.test.common import StubNotifierFactory, CommonTests
+from twisted.internet.defer import inlineCallbacks
+
+
+class HomeMigrationTests(TestCase):
+    """
+    Tests for L{UpgradeToDatabaseService}.
+    """
+
+    @inlineCallbacks
+    def setUp(self):
+        """
+        Set up two stores to migrate between.
+        """
+        # Add some files to the file store.
+        self.filesPath = CachingFilePath(self.mktemp())
+        self.filesPath.createDirectory()
+        fileStore = CommonDataStore(
+            self.filesPath, StubNotifierFactory(), True, True
+        )
+        self.sqlStore = yield theStoreBuilder.buildStore(
+            self, StubNotifierFactory()
+        )
+        self.stubService = Service()
+        self.topService = MultiService()
+        self.upgrader = UpgradeToDatabaseService(
+            fileStore, self.sqlStore, self.stubService
+        )
+        self.upgrader.setServiceParent(self.topService)
+        requirements = CommonTests.requirements
+        populateCalendarsFrom(requirements, fileStore)
+        self.filesPath.child("calendars").child(
+            "__uids__").child("ho").child("me").child("home1").child(
+            ".some-extra-data").setContent("some extra data")
+
+
+    def test_upgradeCalendarHomes(self):
+        """
+        L{UpgradeToDatabaseService.startService} will do the upgrade, then
+        start its dependent service by adding it to its service hierarchy.
+        """
+        self.topService.startService()
+        # XXX asyncify for attachment migration
+        self.assertEquals(self.stubService.running, True)
+        txn = self.sqlStore.newTransaction()
+        self.addCleanup(txn.commit)
+        for uid in CommonTests.requirements:
+            if CommonTests.requirements[uid] is not None:
+                self.assertNotIdentical(None, txn.calendarHomeWithUID(uid))
+        # Un-migrated data should be preserved.
+        self.assertEquals(self.filesPath.child("calendars-migrated").child(
+            "__uids__").child("ho").child("me").child("home1").child(
+                ".some-extra-data").getContent(),
+                "some extra data"
+        )
+

Modified: CalendarServer/trunk/txdav/common/datastore/util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/util.py	2010-09-10 20:10:16 UTC (rev 6274)
+++ CalendarServer/trunk/txdav/common/datastore/util.py	2010-09-10 20:11:01 UTC (rev 6275)
@@ -1,4 +1,4 @@
-# -*- test-case-name: txdav.caldav.datastore.test.test_sql,txdav.carddav.datastore.test.test_sql -*-
+# -*- test-case-name: txdav.common.datastore.test -*-
 ##
 # Copyright (c) 2010 Apple Inc. All rights reserved.
 #
@@ -21,6 +21,7 @@
 from txdav.common.datastore.sql import CommonDataStore as SqlStore
 from txdav.caldav.datastore.util import migrateHome as migrateCalendarHome
 from txdav.carddav.datastore.util import migrateHome as migrateAddressbookHome
+from twisted.internet.defer import inlineCallbacks
 
 
 class UpgradeToDatabaseService(Service, LoggingMixIn, object):
@@ -77,7 +78,14 @@
         self.sqlStore = sqlStore
 
 
-    def startService(self):
+    @inlineCallbacks
+    def doMigration(self):
+        """
+        Do the migration.  Called by C{startService}, but a different method
+        because C{startService} should return C{None}, not a L{Deferred}.
+
+        @return: a Deferred which fires when the migration is complete.
+        """
         self.log_warn("Beginning filesystem -> database upgrade.")
         for homeType, migrateFunc, eachFunc, destFunc in [
             ("calendar", migrateCalendarHome,
@@ -91,11 +99,22 @@
                 self.log_warn("Migrating %s UID %r" % (homeType, uid))
                 sqlTxn = self.sqlStore.newTransaction()
                 sqlHome = destFunc(uid, sqlTxn)
-                migrateFunc(fileHome, sqlHome)
+                yield migrateFunc(fileHome, sqlHome)
                 fileTxn.commit()
                 sqlTxn.commit()
-                # FIXME: need a public remove...HomeWithUID() for de-provisioning
-                fileHome._path.remove()
+                # FIXME: need a public remove...HomeWithUID() for de-
+                # provisioning
+                storePath = self.fileStore._path
+                fromParent = fileHome._path.segmentsFrom(storePath)
+                fromParent[0] += "-migrated"
+                backupPath = storePath
+                for segment in fromParent:
+                    try:
+                        backupPath.createDirectory()
+                    except OSError:
+                        pass
+                    backupPath = backupPath.child(segment)
+                fileHome._path.moveTo(backupPath)
         for homeType in TOPPATHS:
             homesPath = self.fileStore._path.child(homeType)
             if homesPath.isdir():
@@ -106,4 +125,11 @@
         self.wrappedService.setServiceParent(self.parent)
 
 
+    def startService(self):
+        """
+        Start the service.
+        """
+        self.doMigration()
 
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100910/928a43bf/attachment-0001.html>


More information about the calendarserver-changes mailing list