<!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>[8015] 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.macosforge.org/projects/calendarserver/changeset/8015">8015</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2011-08-25 07:32:53 -0700 (Thu, 25 Aug 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make sure missing schedule-object state results in a recalculation of the proper state to aid migration from non-implicit.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavschedulingimplicitpy">CalendarServer/trunk/twistedcaldav/scheduling/implicit.py</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastorefilepy">CalendarServer/trunk/txdav/caldav/datastore/file.py</a></li>
<li><a href="#CalendarServertrunktxdavcaldavdatastoretestcommonpy">CalendarServer/trunk/txdav/caldav/datastore/test/common.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemacurrentsql">CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaupgradesupgrade_templatesql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoretesttest_sql_tablespy">CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaoldv3sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/old/v3.sql</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoresql_schemaupgradesupgrade_from_3_to_4sql">CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_from_3_to_4.sql</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavschedulingimplicitpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/scheduling/implicit.py (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/scheduling/implicit.py        2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/twistedcaldav/scheduling/implicit.py   2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -67,7 +67,7 @@
</span><span class="cx">         self.internal_request = internal_request
</span><span class="cx"> 
</span><span class="cx">         existing_resource = resource.exists()
</span><del>-        is_scheduling_object = self.checkSchedulingObjectResource(resource)
</del><ins>+        is_scheduling_object = (yield self.checkSchedulingObjectResource(resource))
</ins><span class="cx">         existing_type = "schedule" if is_scheduling_object else "calendar"
</span><span class="cx">         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
</span><span class="cx"> 
</span><span class="lines">@@ -114,8 +114,8 @@
</span><span class="cx">         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
</span><span class="cx"> 
</span><span class="cx">         dest_exists = destresource.exists()
</span><del>-        dest_is_implicit = self.checkSchedulingObjectResource(destresource)
-        src_is_implicit = self.checkSchedulingObjectResource(srcresource) or new_type == "schedule"
</del><ins>+        dest_is_implicit = (yield self.checkSchedulingObjectResource(destresource))
+        src_is_implicit = (yield self.checkSchedulingObjectResource(srcresource)) or new_type == "schedule"
</ins><span class="cx"> 
</span><span class="cx">         if srccal and destcal:
</span><span class="cx">             if src_is_implicit and dest_exists or dest_is_implicit:
</span><span class="lines">@@ -148,8 +148,8 @@
</span><span class="cx"> 
</span><span class="cx">         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
</span><span class="cx"> 
</span><del>-        dest_is_implicit = self.checkSchedulingObjectResource(destresource)
-        src_is_implicit = self.checkSchedulingObjectResource(srcresource) or new_type == "schedule"
</del><ins>+        dest_is_implicit = (yield self.checkSchedulingObjectResource(destresource))
+        src_is_implicit = (yield self.checkSchedulingObjectResource(srcresource)) or new_type == "schedule"
</ins><span class="cx"> 
</span><span class="cx">         if srccal and destcal:
</span><span class="cx">             if src_is_implicit or dest_is_implicit:
</span><span class="lines">@@ -181,15 +181,36 @@
</span><span class="cx"> 
</span><span class="cx">         yield self.checkImplicitState()
</span><span class="cx"> 
</span><del>-        is_scheduling_object = self.checkSchedulingObjectResource(resource)
</del><ins>+        is_scheduling_object = (yield self.checkSchedulingObjectResource(resource))
</ins><span class="cx">         resource_type = "schedule" if is_scheduling_object else "calendar"
</span><span class="cx">         self.action = "remove" if resource_type == "schedule" else "none"
</span><span class="cx"> 
</span><span class="cx">         returnValue((self.action != "none", False,))
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
</ins><span class="cx">     def checkSchedulingObjectResource(self, resource):
</span><span class="cx">         
</span><del>-        return resource.isScheduleObject if resource and resource.exists() else False
</del><ins>+        if resource and resource.exists():
+            implicit = resource.isScheduleObject
+            if implicit is not None:
+                returnValue(implicit)
+            else:
+                calendar = (yield self.resource.iCalendarForUser(self.request))
+                # Get the ORGANIZER and verify it is the same for all components
+                try:
+                    organizer = calendar.validOrganizerForScheduling()
+                except ValueError:
+                    # We have different ORGANIZERs in the same iCalendar object - this is an error
+                    returnValue(False)
+                organizerPrincipal = resource.principalForCalendarUserAddress(organizer) if organizer else None
+                implicit = organizerPrincipal != None
+                log.debug("Implicit - checked scheduling object resource state for UID: '%s', result: %s" % (
+                    calendar.resourceUID(),
+                    implicit,
+                ))
+                returnValue(implicit)
+
+        returnValue(False)
</ins><span class="cx">         
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def checkImplicitState(self):
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastorefilepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/file.py (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/file.py      2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/txdav/caldav/datastore/file.py 2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -439,11 +439,18 @@
</span><span class="cx">     accessMode = property(_get_accessMode, _set_accessMode)
</span><span class="cx"> 
</span><span class="cx">     def _get_isScheduleObject(self):
</span><del>-        return str(self.properties().get(PropertyName.fromElement(customxml.TwistedSchedulingObjectResource), "false")) == "true"
</del><ins>+        """
+        If the property is not present, then return None, else return a bool based on the
+        str "true" or "false" value.
+        """
+        prop = self.properties().get(PropertyName.fromElement(customxml.TwistedSchedulingObjectResource))
+        if prop is not None:
+            prop = str(prop) == "true"
+        return prop
</ins><span class="cx"> 
</span><span class="cx">     def _set_isScheduleObject(self, value):
</span><span class="cx">         pname = PropertyName.fromElement(customxml.TwistedSchedulingObjectResource)
</span><del>-        if value:
</del><ins>+        if value is not None:
</ins><span class="cx">             self.properties()[pname] = customxml.TwistedSchedulingObjectResource.fromString("true" if value else "false")
</span><span class="cx">         elif pname in self.properties():
</span><span class="cx">             del self.properties()[pname]
</span></span></pre></div>
<a id="CalendarServertrunktxdavcaldavdatastoretestcommonpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/caldav/datastore/test/common.py (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/caldav/datastore/test/common.py       2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/txdav/caldav/datastore/test/common.py  2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -175,7 +175,7 @@
</span><span class="cx">     }
</span><span class="cx">     metadata3 = {
</span><span class="cx">         "accessMode": "PUBLIC",
</span><del>-        "isScheduleObject": True,
</del><ins>+        "isScheduleObject": None,
</ins><span class="cx">         "scheduleTag": "abc",
</span><span class="cx">         "scheduleEtags": (),
</span><span class="cx">         "hasPrivateComment": True,
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemacurrentsql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql       2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql  2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -174,7 +174,7 @@
</span><span class="cx">   ORGANIZER_OBJECT     integer      references CALENDAR_OBJECT,
</span><span class="cx">   RECURRANCE_MAX       date,        -- maximum date that recurrences have been expanded to.
</span><span class="cx">   ACCESS               integer      default 0 not null,
</span><del>-  SCHEDULE_OBJECT      boolean      default false not null,
</del><ins>+  SCHEDULE_OBJECT      boolean      default false,
</ins><span class="cx">   SCHEDULE_TAG         varchar(36)  default null,
</span><span class="cx">   SCHEDULE_ETAGS       text         default null,
</span><span class="cx">   PRIVATE_COMMENTS     boolean      default false not null,
</span><span class="lines">@@ -479,5 +479,5 @@
</span><span class="cx">   unique(NAME)
</span><span class="cx"> );
</span><span class="cx"> 
</span><del>-insert into CALENDARSERVER values ('VERSION', '3');
</del><ins>+insert into CALENDARSERVER values ('VERSION', '4');
</ins><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaoldv3sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/old/v3.sql (0 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/old/v3.sql                                (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/old/v3.sql   2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -0,0 +1,483 @@
</span><ins>+-- -*- test-case-name: txdav.caldav.datastore.test.test_sql,txdav.carddav.datastore.test.test_sql -*-
+
+----
+-- Copyright (c) 2010-2011 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.
+----
+
+-----------------
+-- Resource ID --
+-----------------
+
+create sequence RESOURCE_ID_SEQ;
+
+
+-------------------
+-- Calendar Home --
+-------------------
+
+create table CALENDAR_HOME (
+  RESOURCE_ID      integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  OWNER_UID        varchar(255) not null unique
+);
+
+create index CALENDAR_HOME_OWNER_UID on CALENDAR_HOME(OWNER_UID);
+
+----------------------------
+-- Calendar Home Metadata --
+----------------------------
+
+create table CALENDAR_HOME_METADATA (
+  RESOURCE_ID      integer      not null references CALENDAR_HOME on delete cascade,
+  QUOTA_USED_BYTES integer      default 0 not null
+);
+
+create index CALENDAR_HOME_METADATA_RESOURCE_ID
+    on CALENDAR_HOME_METADATA(RESOURCE_ID);
+
+--------------
+-- Calendar --
+--------------
+
+create table CALENDAR (
+  RESOURCE_ID integer   primary key default nextval('RESOURCE_ID_SEQ'),
+  CREATED     timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED    timestamp default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+------------------------
+-- Sharing Invitation --
+------------------------
+
+create table INVITE (
+    INVITE_UID         varchar(255) not null,
+    NAME               varchar(255) not null,
+    RECIPIENT_ADDRESS  varchar(255) not null,
+    HOME_RESOURCE_ID   integer      not null,
+    RESOURCE_ID        integer      not null
+);
+
+create index INVITE_INVITE_UID on INVITE(INVITE_UID);
+create index INVITE_RESOURCE_ID on INVITE(INVITE_UID);
+create index INVITE_HOME_RESOURCE_ID on INVITE(INVITE_UID);
+
+---------------------------
+-- Sharing Notifications --
+---------------------------
+
+create table NOTIFICATION_HOME (
+  RESOURCE_ID integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  OWNER_UID   varchar(255) not null unique
+);
+
+create index NOTIFICATION_HOME_OWNER_UID on NOTIFICATION_HOME(OWNER_UID);
+
+
+create table NOTIFICATION (
+  RESOURCE_ID                   integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  NOTIFICATION_HOME_RESOURCE_ID integer      not null references NOTIFICATION_HOME,
+  NOTIFICATION_UID              varchar(255) not null,
+  XML_TYPE                      varchar(255) not null,
+  XML_DATA                      text         not null,
+  MD5                           char(32)     not null,
+  CREATED                       timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                      timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique(NOTIFICATION_UID, NOTIFICATION_HOME_RESOURCE_ID)
+);
+
+create index NOTIFICATION_NOTIFICATION_HOME_RESOURCE_ID on
+  NOTIFICATION(NOTIFICATION_HOME_RESOURCE_ID);
+
+create index NOTIFICATION_NOTIFICATION_UID on NOTIFICATION(NOTIFICATION_UID);
+
+-------------------
+-- Calendar Bind --
+-------------------
+
+-- Joins CALENDAR_HOME and CALENDAR
+
+create table CALENDAR_BIND (
+  CALENDAR_HOME_RESOURCE_ID integer      not null references CALENDAR_HOME,
+  CALENDAR_RESOURCE_ID      integer      not null references CALENDAR on delete cascade,
+  
+  -- An invitation which hasn't been accepted yet will not yet have a resource
+  -- name, so this field may be null.
+  
+  CALENDAR_RESOURCE_NAME    varchar(255),
+  BIND_MODE                 integer      not null, -- enum CALENDAR_BIND_MODE
+  BIND_STATUS               integer      not null, -- enum CALENDAR_BIND_STATUS
+  SEEN_BY_OWNER             boolean      not null,
+  SEEN_BY_SHAREE            boolean      not null,
+  MESSAGE                   text,
+
+  primary key(CALENDAR_HOME_RESOURCE_ID, CALENDAR_RESOURCE_ID),
+  unique(CALENDAR_HOME_RESOURCE_ID, CALENDAR_RESOURCE_NAME)
+);
+
+create index CALENDAR_BIND_HOME_RESOURCE_ID on
+  CALENDAR_BIND(CALENDAR_HOME_RESOURCE_ID);
+create index CALENDAR_BIND_RESOURCE_ID on
+  CALENDAR_BIND(CALENDAR_RESOURCE_ID);
+
+-- Enumeration of calendar bind modes
+
+create table CALENDAR_BIND_MODE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_BIND_MODE values (0, 'own'  );
+insert into CALENDAR_BIND_MODE values (1, 'read' );
+insert into CALENDAR_BIND_MODE values (2, 'write');
+insert into CALENDAR_BIND_MODE values (3, 'direct');
+
+-- Enumeration of statuses
+
+create table CALENDAR_BIND_STATUS (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_BIND_STATUS values (0, 'invited' );
+insert into CALENDAR_BIND_STATUS values (1, 'accepted');
+insert into CALENDAR_BIND_STATUS values (2, 'declined');
+insert into CALENDAR_BIND_STATUS values (3, 'invalid');
+
+
+---------------------
+-- Calendar Object --
+---------------------
+
+create table CALENDAR_OBJECT (
+  RESOURCE_ID          integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  CALENDAR_RESOURCE_ID integer      not null references CALENDAR on delete cascade,
+  RESOURCE_NAME        varchar(255) not null,
+  ICALENDAR_TEXT       text         not null,
+  ICALENDAR_UID        varchar(255) not null,
+  ICALENDAR_TYPE       varchar(255) not null,
+  ATTACHMENTS_MODE     integer      default 0 not null, -- enum CALENDAR_OBJECT_ATTACHMENTS_MODE
+  DROPBOX_ID           varchar(255),
+  ORGANIZER            varchar(255),
+  ORGANIZER_OBJECT     integer      references CALENDAR_OBJECT,
+  RECURRANCE_MAX       date,        -- maximum date that recurrences have been expanded to.
+  ACCESS               integer      default 0 not null,
+  SCHEDULE_OBJECT      boolean      default false not null,
+  SCHEDULE_TAG         varchar(36)  default null,
+  SCHEDULE_ETAGS       text         default null,
+  PRIVATE_COMMENTS     boolean      default false not null,
+  MD5                  char(32)     not null,
+  CREATED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED             timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique(CALENDAR_RESOURCE_ID, RESOURCE_NAME)
+
+  -- since the 'inbox' is a 'calendar resource' for the purpose of storing
+  -- calendar objects, this constraint has to be selectively enforced by the
+  -- application layer.
+
+  -- unique(CALENDAR_RESOURCE_ID, ICALENDAR_UID)
+);
+
+create index CALENDAR_OBJECT_CALENDAR_RESOURCE_ID on
+  CALENDAR_OBJECT(CALENDAR_RESOURCE_ID);
+
+create index CALENDAR_OBJECT_CALENDAR_RESOURCE_ID_AND_ICALENDAR_UID on
+  CALENDAR_OBJECT(CALENDAR_RESOURCE_ID, ICALENDAR_UID);

+create index CALENDAR_OBJECT_CALENDAR_RESOURCE_ID_RECURRANCE_MAX on
+  CALENDAR_OBJECT(CALENDAR_RESOURCE_ID, RECURRANCE_MAX);
+
+create index CALENDAR_OBJECT_ORGANIZER_OBJECT on
+  CALENDAR_OBJECT(ORGANIZER_OBJECT);
+
+create index CALENDAR_OBJECT_DROPBOX_ID on
+  CALENDAR_OBJECT(DROPBOX_ID);
+
+-- Enumeration of attachment modes
+
+create table CALENDAR_OBJECT_ATTACHMENTS_MODE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (0, 'none' );
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (1, 'read' );
+insert into CALENDAR_OBJECT_ATTACHMENTS_MODE values (2, 'write');
+
+
+-- Enumeration of calendar access types
+
+create table CALENDAR_ACCESS_TYPE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(32) not null unique
+);
+
+insert into CALENDAR_ACCESS_TYPE values (0, ''             );
+insert into CALENDAR_ACCESS_TYPE values (1, 'public'       );
+insert into CALENDAR_ACCESS_TYPE values (2, 'private'      );
+insert into CALENDAR_ACCESS_TYPE values (3, 'confidential' );
+insert into CALENDAR_ACCESS_TYPE values (4, 'restricted'   );
+
+-----------------
+-- Instance ID --
+-----------------
+
+create sequence INSTANCE_ID_SEQ;
+
+
+----------------
+-- Time Range --
+----------------
+
+create table TIME_RANGE (
+  INSTANCE_ID                 integer        primary key default nextval('INSTANCE_ID_SEQ'),
+  CALENDAR_RESOURCE_ID        integer        not null references CALENDAR on delete cascade,
+  CALENDAR_OBJECT_RESOURCE_ID integer        not null references CALENDAR_OBJECT on delete cascade,
+  FLOATING                    boolean        not null,
+  START_DATE                  timestamp      not null,
+  END_DATE                    timestamp      not null,
+  FBTYPE                      integer        not null,
+  TRANSPARENT                 boolean        not null
+);
+
+create index TIME_RANGE_CALENDAR_RESOURCE_ID on
+  TIME_RANGE(CALENDAR_RESOURCE_ID);
+create index TIME_RANGE_CALENDAR_OBJECT_RESOURCE_ID on
+  TIME_RANGE(CALENDAR_OBJECT_RESOURCE_ID);
+
+
+-- Enumeration of free/busy types
+
+create table FREE_BUSY_TYPE (
+  ID          integer     primary key,
+  DESCRIPTION varchar(16) not null unique
+);
+
+insert into FREE_BUSY_TYPE values (0, 'unknown'         );
+insert into FREE_BUSY_TYPE values (1, 'free'            );
+insert into FREE_BUSY_TYPE values (2, 'busy'            );
+insert into FREE_BUSY_TYPE values (3, 'busy-unavailable');
+insert into FREE_BUSY_TYPE values (4, 'busy-tentative'  );
+
+
+------------------
+-- Transparency --
+------------------
+
+create table TRANSPARENCY (
+  TIME_RANGE_INSTANCE_ID      integer      not null references TIME_RANGE on delete cascade,
+  USER_ID                     varchar(255) not null,
+  TRANSPARENT                 boolean      not null
+);
+
+create index TRANSPARENCY_TIME_RANGE_INSTANCE_ID on
+  TRANSPARENCY(TIME_RANGE_INSTANCE_ID);
+
+----------------
+-- Attachment --
+----------------
+
+create table ATTACHMENT (
+  CALENDAR_HOME_RESOURCE_ID   integer       not null references CALENDAR_HOME,
+  DROPBOX_ID                  varchar(255)  not null,
+  CONTENT_TYPE                varchar(255)  not null,
+  SIZE                        integer       not null,
+  MD5                         char(32)      not null,
+  CREATED                     timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                    timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  PATH                        varchar(1024) not null,
+
+  unique(DROPBOX_ID, PATH)
+);
+
+create index ATTACHMENT_DROPBOX_ID on ATTACHMENT(DROPBOX_ID);
+
+
+-----------------------
+-- Resource Property --
+-----------------------
+
+create table RESOURCE_PROPERTY (
+  RESOURCE_ID integer      not null, -- foreign key: *.RESOURCE_ID
+  NAME        varchar(255) not null,
+  VALUE       text         not null, -- FIXME: xml?
+  VIEWER_UID  varchar(255),
+
+  primary key(RESOURCE_ID, NAME, VIEWER_UID)
+);
+
+
+----------------------
+-- AddressBook Home --
+----------------------
+
+create table ADDRESSBOOK_HOME (
+  RESOURCE_ID      integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  OWNER_UID        varchar(255) not null unique
+);
+
+create index ADDRESSBOOK_HOME_OWNER_UID on ADDRESSBOOK_HOME(OWNER_UID);
+
+--------------------------------
+-- AddressBook Home Meta-data --
+--------------------------------
+
+create table ADDRESSBOOK_HOME_METADATA (
+  RESOURCE_ID      integer      not null references ADDRESSBOOK_HOME on delete cascade,
+  QUOTA_USED_BYTES integer      default 0 not null
+);
+
+create index ADDRESSBOOK_HOME_METADATA_RESOURCE_ID
+    on ADDRESSBOOK_HOME_METADATA(RESOURCE_ID);
+
+-----------------
+-- AddressBook --
+-----------------
+
+create table ADDRESSBOOK (
+  RESOURCE_ID integer   primary key default nextval('RESOURCE_ID_SEQ'),
+  CREATED     timestamp default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED    timestamp default timezone('UTC', CURRENT_TIMESTAMP)
+);
+
+
+----------------------
+-- AddressBook Bind --
+----------------------
+
+-- Joins ADDRESSBOOK_HOME and ADDRESSBOOK
+
+create table ADDRESSBOOK_BIND (
+  ADDRESSBOOK_HOME_RESOURCE_ID integer      not null references ADDRESSBOOK_HOME,
+  ADDRESSBOOK_RESOURCE_ID      integer      not null references ADDRESSBOOK on delete cascade,
+
+  -- An invitation which hasn't been accepted yet will not yet have a resource
+  -- name, so this field may be null.
+
+  ADDRESSBOOK_RESOURCE_NAME    varchar(255),
+  BIND_MODE                    integer      not null, -- enum CALENDAR_BIND_MODE
+  BIND_STATUS                  integer      not null, -- enum CALENDAR_BIND_STATUS
+  SEEN_BY_OWNER                boolean      not null,
+  SEEN_BY_SHAREE               boolean      not null,
+  MESSAGE                      text,                  -- FIXME: xml?
+
+  primary key(ADDRESSBOOK_HOME_RESOURCE_ID, ADDRESSBOOK_RESOURCE_ID),
+  unique(ADDRESSBOOK_HOME_RESOURCE_ID, ADDRESSBOOK_RESOURCE_NAME)
+);
+
+create index ADDRESSBOOK_BIND_HOME_RESOURCE_ID on
+  ADDRESSBOOK_BIND(ADDRESSBOOK_HOME_RESOURCE_ID);
+create index ADDRESSBOOK_BIND_RESOURCE_ID on
+  ADDRESSBOOK_BIND(ADDRESSBOOK_RESOURCE_ID);
+
+create table ADDRESSBOOK_OBJECT (
+  RESOURCE_ID             integer      primary key default nextval('RESOURCE_ID_SEQ'),
+  ADDRESSBOOK_RESOURCE_ID integer      not null references ADDRESSBOOK on delete cascade,
+  RESOURCE_NAME           varchar(255) not null,
+  VCARD_TEXT              text         not null,
+  VCARD_UID               varchar(255) not null,
+  MD5                     char(32)     not null,
+  CREATED                 timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+  MODIFIED                timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
+
+  unique(ADDRESSBOOK_RESOURCE_ID, RESOURCE_NAME),
+  unique(ADDRESSBOOK_RESOURCE_ID, VCARD_UID)
+);
+
+create index ADDRESSBOOK_OBJECT_ADDRESSBOOK_RESOURCE_ID on
+  ADDRESSBOOK_OBJECT(ADDRESSBOOK_RESOURCE_ID);
+
+---------------
+-- Revisions --
+---------------
+
+create sequence REVISION_SEQ;
+
+
+---------------
+-- Revisions --
+---------------
+
+create table CALENDAR_OBJECT_REVISIONS (
+  CALENDAR_HOME_RESOURCE_ID integer      not null references CALENDAR_HOME,
+  CALENDAR_RESOURCE_ID      integer      references CALENDAR,
+  CALENDAR_NAME             varchar(255) default null,
+  RESOURCE_NAME             varchar(255),
+  REVISION                  integer      default nextval('REVISION_SEQ') not null,
+  DELETED                   boolean      not null,
+
+  unique(CALENDAR_RESOURCE_ID, RESOURCE_NAME)
+);
+
+
+create index CALENDAR_OBJECT_REVISIONS_HOME_RESOURCE_ID
+  on CALENDAR_OBJECT_REVISIONS(CALENDAR_HOME_RESOURCE_ID);
+
+create index CALENDAR_OBJECT_REVISIONS_RESOURCE_ID
+  on CALENDAR_OBJECT_REVISIONS(CALENDAR_RESOURCE_ID);
+
+
+-------------------------------
+-- AddressBook Object Revisions --
+-------------------------------
+
+create table ADDRESSBOOK_OBJECT_REVISIONS (
+  ADDRESSBOOK_HOME_RESOURCE_ID integer      not null references ADDRESSBOOK_HOME,
+  ADDRESSBOOK_RESOURCE_ID      integer      references ADDRESSBOOK,
+  ADDRESSBOOK_NAME             varchar(255) default null,
+  RESOURCE_NAME                varchar(255),
+  REVISION                     integer      default nextval('REVISION_SEQ') not null,
+  DELETED                      boolean      not null,
+
+  unique(ADDRESSBOOK_RESOURCE_ID, RESOURCE_NAME)
+);
+
+create index ADDRESSBOOK_OBJECT_REVISIONS_HOME_RESOURCE_ID
+  on ADDRESSBOOK_OBJECT_REVISIONS(ADDRESSBOOK_HOME_RESOURCE_ID);
+
+create index ADDRESSBOOK_OBJECT_REVISIONS_RESOURCE_ID
+  on ADDRESSBOOK_OBJECT_REVISIONS(ADDRESSBOOK_RESOURCE_ID);
+
+-----------------------------------
+-- Notification Object Revisions --
+-----------------------------------
+
+create table NOTIFICATION_OBJECT_REVISIONS (
+  NOTIFICATION_HOME_RESOURCE_ID integer      not null references NOTIFICATION_HOME on delete cascade,
+  RESOURCE_NAME                 varchar(255),
+  REVISION                      integer      default nextval('REVISION_SEQ') not null,
+  DELETED                       boolean      not null,
+
+  unique(NOTIFICATION_HOME_RESOURCE_ID, RESOURCE_NAME)
+);
+
+
+create index NOTIFICATION_OBJECT_REVISIONS_HOME_RESOURCE_ID
+  on NOTIFICATION_OBJECT_REVISIONS(NOTIFICATION_HOME_RESOURCE_ID);
+
+
+--------------------
+-- Schema Version --
+--------------------
+
+create table CALENDARSERVER (
+  NAME                          varchar(255),
+  VALUE                         varchar(255),
+  unique(NAME)
+);
+
+insert into CALENDARSERVER values ('VERSION', '3');
+
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradesupgrade_from_3_to_4sql"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_from_3_to_4.sql (0 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_from_3_to_4.sql                          (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_from_3_to_4.sql     2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -0,0 +1,27 @@
</span><ins>+----
+-- Copyright (c) 2011 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.
+----
+
+-------------------------------------------------
+-- Upgrade database schema from VERSION 3 to 4 --
+-------------------------------------------------
+
+-- Just need to modify one column
+alter table CALENDAR_OBJECT
+ alter column SCHEDULE_OBJECT drop not null;
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '4' where NAME = 'VERSION';
+
</ins></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoresql_schemaupgradesupgrade_templatesql"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql     2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql        2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -14,6 +14,13 @@
</span><span class="cx"> -- limitations under the License.
</span><span class="cx"> ----
</span><span class="cx"> 
</span><ins>+-- The file name must be of the form:
+--
+-- upgrade_from_X_to_Y.sql
+--
+-- where X is the start version number, and Y the end version
+-- number for the upgrade 
+
</ins><span class="cx"> -------------------------------------------------
</span><span class="cx"> -- Upgrade database schema from VERSION X to Y --
</span><span class="cx"> -------------------------------------------------
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoretesttest_sql_tablespy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py (8014 => 8015)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py      2011-08-25 02:29:22 UTC (rev 8014)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py 2011-08-25 14:32:53 UTC (rev 8015)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> from twext.enterprise.dal.parseschema import addSQLToSchema
</span><span class="cx"> from twext.enterprise.dal.model import Schema
</span><span class="cx"> from twext.enterprise.dal.syntax import SchemaSyntax
</span><ins>+from twisted.python.modules import getModule
</ins><span class="cx"> from twisted.trial.unittest import TestCase
</span><span class="cx"> 
</span><span class="cx"> class SampleSomeColumns(TestCase):
</span><span class="lines">@@ -58,10 +59,17 @@
</span><span class="cx">         """
</span><span class="cx">         _translateSchema includes 'insert' rows too.
</span><span class="cx">         """
</span><ins>+        
+        pathObj = getModule(__name__).filePath.parent().sibling("sql_schema").child("current.sql")
+        schema = pathObj.getContent()
+        pos = schema.find("('VERSION', '")
+        version = int(schema[pos+13])
+        
</ins><span class="cx">         io = StringIO()
</span><span class="cx">         _translateSchema(io)
</span><ins>+        
</ins><span class="cx">         self.assertIn("insert into CALENDARSERVER (NAME, VALUE) "
</span><del>-                      "values ('VERSION', '3');",
</del><ins>+                      "values ('VERSION', '%s');" % version,
</ins><span class="cx">                       io.getvalue())
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>