[CalendarServer-changes] [11144] CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/ datastore

source_changes at macosforge.org source_changes at macosforge.org
Mon May 6 16:18:24 PDT 2013


Revision: 11144
          http://trac.calendarserver.org//changeset/11144
Author:   gaya at apple.com
Date:     2013-05-06 16:18:24 -0700 (Mon, 06 May 2013)
Log Message:
-----------
fix upgrade

Modified Paths:
--------------
    CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_18_to_19.sql
    CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/addressbook_upgrade_from_1_to_2.py
    CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/calendar_upgrade_from_1_to_2.py
    CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/util.py

Modified: CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_18_to_19.sql
===================================================================
--- CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_18_to_19.sql	2013-05-06 23:17:46 UTC (rev 11143)
+++ CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/sql_schema/upgrades/postgres-dialect/upgrade_from_18_to_19.sql	2013-05-06 23:18:24 UTC (rev 11144)
@@ -137,10 +137,10 @@
 --------------------------------
 
 alter table ADDRESSBOOK_OBJECT
-	add column	KIND	integer	not null;  -- enum ADDRESSBOOK_OBJECT_KIND
+	add column	KIND	integer;  -- enum ADDRESSBOOK_OBJECT_KIND
 
 alter table ADDRESSBOOK_OBJECT
-	add column	ADDRESSBOOK_HOME_RESOURCE_ID	integer	not null references ADDRESSBOOK_HOME on delete cascade;
+	add column	ADDRESSBOOK_HOME_RESOURCE_ID	integer	references ADDRESSBOOK_HOME on delete cascade;
 
 update ADDRESSBOOK_OBJECT
 	set	ADDRESSBOOK_HOME_RESOURCE_ID = (
@@ -160,7 +160,29 @@
 			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_NAME = 'addressbook'
   	);
 
+-- delete rows for shared and non-default address books
+delete 
+	from ADDRESSBOOK_OBJECT
+	where exists (
+		select *
+			from ADDRESSBOOK_BIND
+		where 
+			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_ID = ADDRESSBOOK_OBJECT.ADDRESSBOOK_RESOURCE_ID and (
+				ADDRESSBOOK_BIND.BIND_MODE != 0 or 	-- CALENDAR_BIND_MODE 'own'
+	 			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_NAME != 'addressbook'
+	 		)
+  	);
+  	
+-- add non null constraints after update and delete are complete
 alter table ADDRESSBOOK_OBJECT
+	alter column KIND
+		set not null;
+
+alter table ADDRESSBOOK_OBJECT
+	alter column ADDRESSBOOK_HOME_RESOURCE_ID
+		set not null;
+
+alter table ADDRESSBOOK_OBJECT
 	drop column	ADDRESSBOOK_RESOURCE_ID;
 
 	
@@ -169,7 +191,7 @@
 ------------------------------------------
 
 alter table ADDRESSBOOK_OBJECT_REVISIONS
-	add column	OWNER_ADDRESSBOOK_HOME_RESOURCE_ID	integer	not null references ADDRESSBOOK_HOME on delete cascade;
+	add column	OWNER_ADDRESSBOOK_HOME_RESOURCE_ID	integer	references ADDRESSBOOK_HOME;
 
 update ADDRESSBOOK_OBJECT_REVISIONS
 	set	OWNER_ADDRESSBOOK_HOME_RESOURCE_ID = (
@@ -189,13 +211,28 @@
 			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_NAME = 'addressbook'
   	);
 
+-- delete rows for shared and non-default address books
+delete 
+	from ADDRESSBOOK_OBJECT_REVISIONS
+	where exists (
+		select *
+			from ADDRESSBOOK_BIND
+		where 
+			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_ID = ADDRESSBOOK_OBJECT_REVISIONS.ADDRESSBOOK_RESOURCE_ID and (
+				ADDRESSBOOK_BIND.BIND_MODE != 0 or 	-- CALENDAR_BIND_MODE 'own'
+	 			ADDRESSBOOK_BIND.ADDRESSBOOK_RESOURCE_NAME != 'addressbook'
+	 		)
+  	);
+
 alter table ADDRESSBOOK_OBJECT_REVISIONS
 	drop column	ADDRESSBOOK_RESOURCE_ID;
 
-----------------------------------------------------------------------------
--- delete RESOURCE_PROPERTY rows for shared and non-default address books --
-----------------------------------------------------------------------------
 
+-------------------------------
+-- change  RESOURCE_PROPERTY --
+-------------------------------
+
+-- delete rows for shared and non-default address books
 delete 
 	from RESOURCE_PROPERTY
 	where exists (

Modified: CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/addressbook_upgrade_from_1_to_2.py
===================================================================
--- CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/addressbook_upgrade_from_1_to_2.py	2013-05-06 23:17:46 UTC (rev 11143)
+++ CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/addressbook_upgrade_from_1_to_2.py	2013-05-06 23:18:24 UTC (rev 11144)
@@ -16,8 +16,13 @@
 # #
 
 from twisted.internet.defer import inlineCallbacks
-from txdav.common.datastore.upgrade.sql.upgrades.util import updateAddressBookDataVersion
-
+#from txdav.carddav.datastore.util import validateAddressBookComponent
+from txdav.common.datastore.sql_tables import _ABO_KIND_PERSON, \
+    _ABO_KIND_GROUP, _ABO_KIND_RESOURCE, _ABO_KIND_LOCATION, schema
+    #KIND, RESOURCE_ID
+from txdav.common.datastore.upgrade.sql.upgrades.util import updateAddressBookDataVersion, \
+    doToEachHomeNotAtVersion
+from twext.enterprise.dal.syntax import Update
 """
 AddressBook Data upgrade from database version 1 to 2
 """
@@ -27,8 +32,43 @@
 @inlineCallbacks
 def doUpgrade(sqlStore):
     """
-    Do the required upgrade steps.
+    fill in members tables and increment data version
     """
+    yield populateMembersTables(sqlStore)
 
     # bump data version
     yield updateAddressBookDataVersion(sqlStore, UPGRADE_TO_VERSION)
+
+
+ at inlineCallbacks
+def populateMembersTables(sqlStore):
+    """
+    Set the group kind and and members tables
+    """
+    @inlineCallbacks
+    def doIt(txn, homeResourceID):
+        """
+        KIND is set to person by schema upgrade.
+        To upgrade MEMBERS and FOREIGN_MEMBERS:
+            1. Set group KIND (avoids assert)
+            2. Write groups.  Write logic will fill in MEMBERS and FOREIGN_MEMBERS
+                (Remember that all members resource IDs must already be in the address book).
+        """
+        home = yield txn.addressbookHomeWithResourceID(homeResourceID)
+        abObjectResources = yield home.addressbook().objectResources()
+        for abObject in abObjectResources:
+            component = yield abObject.component()
+            lcResourceKind = component.resourceKind().lower() if component.resourceKind() else component.resourceKind();
+            if lcResourceKind == "group":
+                # update kind
+                abo = schema.ADDRESSBOOK_OBJECT
+                yield Update({abo.KIND: _ABO_KIND_GROUP},
+                                Where=abo.RESOURCE_ID == abObject._resourceID,
+                                ).on(txn)
+                abObject._kind = _ABO_KIND_GROUP
+                #update rest
+                yield abObject.setComponent(component)
+
+
+    # Do this to each calendar home not already at version 2
+    yield doToEachHomeNotAtVersion(sqlStore, schema.ADDRESSBOOK_HOME, UPGRADE_TO_VERSION, doIt)

Modified: CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/calendar_upgrade_from_1_to_2.py
===================================================================
--- CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/calendar_upgrade_from_1_to_2.py	2013-05-06 23:17:46 UTC (rev 11143)
+++ CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/calendar_upgrade_from_1_to_2.py	2013-05-06 23:18:24 UTC (rev 11144)
@@ -79,10 +79,11 @@
     """
 
     @inlineCallbacks
-    def doIt(home):
+    def doIt(txn, homeResourceID):
         """
         Split each regular calendar in the home.
         """
+        home = yield txn.calendarHomeWithResourceID(homeResourceID)
         yield home.splitCalendars()
             
     # Do this to each calendar home not already at version 2

Modified: CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/util.py
===================================================================
--- CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/util.py	2013-05-06 23:17:46 UTC (rev 11143)
+++ CalendarServer/branches/users/gaya/sharedgroups-3/txdav/common/datastore/upgrade/sql/upgrades/util.py	2013-05-06 23:18:24 UTC (rev 11144)
@@ -19,19 +19,21 @@
 from txdav.base.propertystore.base import PropertyName
 from txdav.common.datastore.sql_tables import schema
 
+
 @inlineCallbacks
 def rowsForProperty(txn, propelement):
     pname = PropertyName.fromElement(propelement)
 
     rp = schema.RESOURCE_PROPERTY
     rows = yield Select(
-        [rp.RESOURCE_ID, rp.VALUE,],
+        [rp.RESOURCE_ID, rp.VALUE, ],
         From=rp,
         Where=rp.NAME == pname.toString(),
     ).on(txn)
-    
+
     returnValue(rows)
 
+
 @inlineCallbacks
 def removeProperty(txn, propelement):
     pname = PropertyName.fromElement(propelement)
@@ -42,6 +44,7 @@
         Where=rp.NAME == pname.toString(),
     ).on(txn)
 
+
 @inlineCallbacks
 def _updateDataVersion(store, key, version):
 
@@ -53,12 +56,15 @@
     ).on(txn)
     yield txn.commit()
 
+
 def updateCalendarDataVersion(store, version):
     return _updateDataVersion(store, "CALENDAR-DATAVERSION", version)
 
+
 def updateAddressBookDataVersion(store, version):
     return _updateDataVersion(store, "ADDRESSBOOK-DATAVERSION", version)
 
+
 @inlineCallbacks
 def doToEachHomeNotAtVersion(store, homeSchema, version, doIt):
     """
@@ -67,10 +73,10 @@
     """
 
     while True:
-        
+
         # Get the next home with an old version
-        txn = store.newTransaction("updateDataVersion")   
-        try: 
+        txn = store.newTransaction("updateDataVersion")
+        try:
             rows = yield Select(
                 [homeSchema.RESOURCE_ID, homeSchema.OWNER_UID, ],
                 From=homeSchema,
@@ -78,20 +84,19 @@
                 OrderBy=homeSchema.OWNER_UID,
                 Limit=1,
             ).on(txn)
-            
+
             if len(rows) == 0:
                 yield txn.commit()
                 returnValue(None)
-            
+
             # Apply to the home
-            resource_id, _ignore_owner_uid = rows[0]
-            home = yield txn.calendarHomeWithResourceID(resource_id)
-            yield doIt(home)
-    
+            homeResourceID, _ignore_owner_uid = rows[0]
+            yield doIt(txn, homeResourceID)
+
             # Update the home to the current version
             yield Update(
                 {homeSchema.DATAVERSION: version},
-                Where=homeSchema.RESOURCE_ID == resource_id,
+                Where=homeSchema.RESOURCE_ID == homeResourceID,
             ).on(txn)
             yield txn.commit()
         except RuntimeError:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130506/25473640/attachment-0001.html>


More information about the calendarserver-changes mailing list