[CalendarServer-changes] [7713] CalendarServer/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Wed Jul 6 10:52:44 PDT 2011
Revision: 7713
http://trac.macosforge.org/projects/calendarserver/changeset/7713
Author: cdaboo at apple.com
Date: 2011-07-06 10:52:43 -0700 (Wed, 06 Jul 2011)
Log Message:
-----------
Automated schema upgrade capability added.
Modified Paths:
--------------
CalendarServer/trunk/calendarserver/tap/caldav.py
CalendarServer/trunk/calendarserver/tap/util.py
CalendarServer/trunk/calendarserver/tools/bootstrapdatabase.py
CalendarServer/trunk/txdav/common/datastore/sql.py
CalendarServer/trunk/txdav/common/datastore/sql_legacy.py
CalendarServer/trunk/txdav/common/datastore/sql_tables.py
CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py
CalendarServer/trunk/txdav/common/datastore/test/test_util.py
CalendarServer/trunk/txdav/common/datastore/test/util.py
CalendarServer/trunk/txdav/common/datastore/util.py
Added Paths:
-----------
CalendarServer/trunk/txdav/common/datastore/sql_schema/
CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql
CalendarServer/trunk/txdav/common/datastore/sql_schema/old/
CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/
CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/current.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/upgrades/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/upgrades/upgrade_from_3_to_4.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/current.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_4.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_5.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_4_to_5.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/current.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/
CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_3_to_4.sql
CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_4_to_5.sql
Removed Paths:
-------------
CalendarServer/trunk/txdav/common/datastore/sql_schema_v1.sql
Modified: CalendarServer/trunk/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/caldav.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/calendarserver/tap/caldav.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -59,7 +59,8 @@
from twext.web2.channel.http import LimitingHTTPFactory, SSLRedirectRequest
from twext.web2.metafd import ConnectionLimiter, ReportingHTTPService
-from txdav.common.datastore.util import UpgradeToDatabaseService
+from txdav.common.datastore.util import UpgradeToDatabaseService,\
+ UpgradeDatabaseSchemaService
from twistedcaldav.config import ConfigurationError
from twistedcaldav.config import config
@@ -961,9 +962,12 @@
store = storeFromConfig(config, cp.connection)
mainService = createMainService(cp, store)
upgradeSvc = UpgradeFileSystemFormatService(config,
- UpgradeToDatabaseService.wrapService(
- CachingFilePath(config.DocumentRoot),
- PostDBImportService(config, store, mainService),
+ UpgradeDatabaseSchemaService.wrapService(
+ UpgradeToDatabaseService.wrapService(
+ CachingFilePath(config.DocumentRoot),
+ PostDBImportService(config, store, mainService),
+ store, uid=uid, gid=gid
+ ),
store, uid=uid, gid=gid
)
)
Modified: CalendarServer/trunk/calendarserver/tap/util.py
===================================================================
--- CalendarServer/trunk/calendarserver/tap/util.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/calendarserver/tap/util.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -82,7 +82,7 @@
from txdav.common.datastore.sql import CommonDataStore as CommonSQLDataStore
from txdav.common.datastore.file import CommonDataStore as CommonFileDataStore
-from txdav.common.datastore.sql import v1_schema
+from txdav.common.datastore.sql import current_sql_schema
from twext.python.filepath import CachingFilePath
from urllib import quote
@@ -112,7 +112,7 @@
# Construct a PostgresService exactly as the parent would, so that we
# can establish connection information.
return PostgresService(
- dbRoot, subServiceFactory, v1_schema,
+ dbRoot, subServiceFactory, current_sql_schema,
databaseName=config.Postgres.DatabaseName,
logFile=config.Postgres.LogFile,
socketDir=config.RunRoot,
Modified: CalendarServer/trunk/calendarserver/tools/bootstrapdatabase.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/bootstrapdatabase.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/calendarserver/tools/bootstrapdatabase.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -16,14 +16,14 @@
from getopt import getopt, GetoptError
import os
+import re
import subprocess
import sys
CONNECTNAME = "_postgres"
USERNAME = "caldav"
DATABASENAME = "caldav"
-SCHEMAFILE = "/usr/share/caldavd/lib/python/txdav/common/datastore/sql_schema_v1.sql"
-SCHEMAVERSION = 3
+SCHEMAFILE = "/usr/share/caldavd/lib/python/txdav/common/datastore/sql_schema/current.sql"
# Executables:
CREATEDB = "/usr/bin/createdb"
@@ -233,19 +233,30 @@
except BootstrapError, e:
version = 0
- if version == SCHEMAVERSION:
- print "Latest schema version (%d) is installed" % (version,)
+ # Retrieve the version number from the schema file
+ try:
+ data = open(SCHEMAFILE).read()
+ except IOError:
+ print "Unable to open the schema file: %s" % (SCHEMAFILE,)
+ else:
+ found = re.search("insert into CALENDARSERVER values \('VERSION', '(\d)+'\);", data)
+ if found is None:
+ print "Schema is missing required schema VERSION insert statement: %s" % (SCHEMAFILE,)
+ else:
+ required_version = int(found.group(1))
+ if version == required_version:
+ print "Latest schema version (%d) is installed" % (version,)
+
+ elif version == 0: # No schema installed
+ installSchema(verbose=verbose)
+ version = getSchemaVersion(verbose=verbose)
+ print "Successfully installed schema version %d" % (version,)
+
+ else: # upgrade needed
+ error(
+ "Schema needs to be upgraded from %d to %d" %
+ (version, required_version)
+ )
- elif version == 0: # No schema installed
- installSchema(verbose=verbose)
- version = getSchemaVersion(verbose=verbose)
- print "Successfully installed schema version %d" % (version,)
-
- else: # upgrade needed
- error(
- "Schema needs to be upgraded from %d to %d" %
- (version, SCHEMAVERSION)
- )
-
if __name__ == "__main__":
main()
Modified: CalendarServer/trunk/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/sql.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -78,7 +78,7 @@
from twistedcaldav.dateops import datetimeMktime, parseSQLTimestamp,\
pyCalendarTodatetime
-v1_schema = getModule(__name__).filePath.sibling("sql_schema_v1.sql").getContent()
+current_sql_schema = getModule(__name__).filePath.sibling("sql_schema").child("current.sql").getContent()
log = Logger()
@@ -219,6 +219,26 @@
return 'PG-TXN<%s>' % (self._label,)
+ @classproperty
+ def _schemaVersion(cls): #@NoSelf
+ cs = schema.CALENDARSERVER
+ return Select(
+ [cs.VALUE,],
+ From=cs,
+ Where=cs.NAME == "VERSION",
+ )
+
+ @inlineCallbacks
+ def schemaVersion(self):
+ result = yield self._schemaVersion.on(self)
+ if result and len(result) == 1:
+ try:
+ returnValue(int(result[0][0]))
+ except ValueError:
+ pass
+ raise RuntimeError("Database schema version cannot be determined.")
+
+
@memoizedKey('uid', '_calendarHomes')
def calendarHomeWithUID(self, uid, create=False):
return self.homeWithUID(ECALENDARTYPE, uid, create=create)
Modified: CalendarServer/trunk/txdav/common/datastore/sql_legacy.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql_legacy.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/sql_legacy.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -855,7 +855,7 @@
"""
Class attributes for 'real' SQL behavior; avoid idiosyncracies of SQLite,
use standard SQL constructions, and depend on the full schema in
- sql_schema_vX.sql rather than the partial one in twistedcaldav which depends
+ sql_schema/current.sql rather than the partial one in twistedcaldav which depends
on the placement of the database in the filesystem for some information.
"""
Copied: CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql (from rev 7702, CalendarServer/trunk/txdav/common/datastore/sql_schema_v1.sql)
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/current.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,483 @@
+-- -*- 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');
+
Added: CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema/upgrades/upgrade_template.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 X to Y --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = 'Y' where NAME = 'VERSION';
+
Deleted: CalendarServer/trunk/txdav/common/datastore/sql_schema_v1.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql_schema_v1.sql 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/sql_schema_v1.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -1,483 +0,0 @@
--- -*- 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');
-
Modified: CalendarServer/trunk/txdav/common/datastore/sql_tables.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/sql_tables.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/sql_tables.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -31,7 +31,7 @@
"""
Generate the global L{SchemaSyntax}.
"""
- pathObj = getModule(__name__).filePath.sibling("sql_schema_v1.sql")
+ pathObj = getModule(__name__).filePath.sibling("sql_schema").child("current.sql")
return SchemaSyntax(schemaFromPath(pathObj))
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/current.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/current.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/current.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,28 @@
+----
+-- 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.
+----
+
+--------------------
+-- Schema Version --
+--------------------
+
+create table CALENDARSERVER (
+ NAME varchar(255),
+ VALUE varchar(255),
+ unique(NAME)
+);
+
+-- Current version of the schema
+insert into CALENDARSERVER values ('VERSION', '4');
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/upgrades/upgrade_from_3_to_4.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/upgrades/upgrade_from_3_to_4.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema1/upgrades/upgrade_from_3_to_4.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '4' where NAME = 'VERSION';
+
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/current.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/current.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/current.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,28 @@
+----
+-- 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.
+----
+
+--------------------
+-- Schema Version --
+--------------------
+
+create table CALENDARSERVER (
+ NAME varchar(255),
+ VALUE varchar(255),
+ unique(NAME)
+);
+
+-- Current version of the schema
+insert into CALENDARSERVER values ('VERSION', '5');
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_4.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_4.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_4.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '4' where NAME = 'VERSION';
+
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_5.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_5.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_3_to_5.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 5 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '5' where NAME = 'VERSION';
+
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_4_to_5.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_4_to_5.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema2/upgrades/upgrade_from_4_to_5.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 4 to 5 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '5' where NAME = 'VERSION';
+
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/current.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/current.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/current.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,28 @@
+----
+-- 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.
+----
+
+--------------------
+-- Schema Version --
+--------------------
+
+create table CALENDARSERVER (
+ NAME varchar(255),
+ VALUE varchar(255),
+ unique(NAME)
+);
+
+-- Current version of the schema
+insert into CALENDARSERVER values ('VERSION', '5');
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_3_to_4.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_3_to_4.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_3_to_4.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '4' where NAME = 'VERSION';
+
Added: CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_4_to_5.sql
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_4_to_5.sql (rev 0)
+++ CalendarServer/trunk/txdav/common/datastore/test/fake_schema3/upgrades/upgrade_from_4_to_5.sql 2011-07-06 17:52:43 UTC (rev 7713)
@@ -0,0 +1,24 @@
+----
+-- 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 4 to 5 --
+-------------------------------------------------
+
+
+-- Now update the version
+update CALENDARSERVER set VALUE = '5' where NAME = 'VERSION';
+
Modified: CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_sql_tables.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -35,7 +35,8 @@
"""
def test_addressbookObjectResourceID(self):
- self.assertEquals(schema.ADDRESSBOOK_OBJECT.RESOURCE_ID.model.name,
+ ao = schema.ADDRESSBOOK_OBJECT
+ self.assertEquals(ao.RESOURCE_ID.model.name,
"RESOURCE_ID")
Modified: CalendarServer/trunk/txdav/common/datastore/test/test_util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/test_util.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/test/test_util.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
##
+from twisted.python.modules import getModule
+import re
"""
Tests for L{txdav.common.datastore.util}.
@@ -32,7 +34,8 @@
from txdav.common.datastore.test.util import theStoreBuilder, \
populateCalendarsFrom, StubNotifierFactory, resetCalendarMD5s,\
populateAddressBooksFrom, resetAddressBookMD5s
-from txdav.common.datastore.util import UpgradeToDatabaseService
+from txdav.common.datastore.util import UpgradeToDatabaseService,\
+ UpgradeDatabaseSchemaService
class HomeMigrationTests(TestCase):
"""
@@ -225,4 +228,83 @@
object = (yield adbk.addressbookObjectWithName(name))
self.assertEquals(object.md5(), md5)
+class SchemaUpgradeTests(TestCase):
+ """
+ Tests for L{UpgradeDatabaseSchemaService}.
+ """
+ def test_scanUpgradeFiles(self):
+
+ upgrader = UpgradeDatabaseSchemaService(None, None)
+
+ upgrader.schemaLocation = getModule(__name__).filePath.sibling("fake_schema1")
+ files = upgrader.scanForUpgradeFiles()
+ self.assertEqual(files,
+ [(3, 4, upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_4.sql"))],
+ )
+
+ upgrader.schemaLocation = getModule(__name__).filePath.sibling("fake_schema2")
+ files = upgrader.scanForUpgradeFiles()
+ self.assertEqual(files,
+ [
+ (3, 4, upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_4.sql")),
+ (3, 5, upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_5.sql")),
+ (4, 5, upgrader.schemaLocation.child("upgrades").child("upgrade_from_4_to_5.sql")),
+ ]
+ )
+
+ def test_determineUpgradeSequence(self):
+
+ upgrader = UpgradeDatabaseSchemaService(None, None)
+
+ upgrader.schemaLocation = getModule(__name__).filePath.sibling("fake_schema1")
+ files = upgrader.scanForUpgradeFiles()
+ upgrades = upgrader.determineUpgradeSequence(3, 4, files)
+ self.assertEqual(upgrades,
+ [upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_4.sql")],
+ )
+ self.assertRaises(RuntimeError, upgrader.determineUpgradeSequence, 3, 5, files)
+
+ upgrader.schemaLocation = getModule(__name__).filePath.sibling("fake_schema2")
+ files = upgrader.scanForUpgradeFiles()
+ upgrades = upgrader.determineUpgradeSequence(3, 5, files)
+ self.assertEqual(upgrades,
+ [upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_5.sql")]
+ )
+ upgrades = upgrader.determineUpgradeSequence(4, 5, files)
+ self.assertEqual(upgrades,
+ [upgrader.schemaLocation.child("upgrades").child("upgrade_from_4_to_5.sql")]
+ )
+
+ upgrader.schemaLocation = getModule(__name__).filePath.sibling("fake_schema3")
+ files = upgrader.scanForUpgradeFiles()
+ upgrades = upgrader.determineUpgradeSequence(3, 5, files)
+ self.assertEqual(upgrades,
+ [
+ upgrader.schemaLocation.child("upgrades").child("upgrade_from_3_to_4.sql"),
+ upgrader.schemaLocation.child("upgrades").child("upgrade_from_4_to_5.sql"),
+ ]
+ )
+
+ def test_upgradeAvailability(self):
+ """
+ Make sure that each old schema has a valid upgrade path to the current one.
+ """
+
+ upgrader = UpgradeDatabaseSchemaService(None, None)
+ files = upgrader.scanForUpgradeFiles()
+
+ def _getSchemaVersion(fp):
+ schema = fp.getContent()
+ found = re.search("insert into CALENDARSERVER values \('VERSION', '(\d)+'\);", schema)
+ if found is None:
+ self.fail("Could not determine schema version for: %s" % (fp,))
+ return int(found.group(1))
+
+
+ current_version = _getSchemaVersion(upgrader.schemaLocation.child("current.sql"))
+
+ for child in upgrader.schemaLocation.child("old").globChildren("*.sql"):
+ old_version = _getSchemaVersion(child)
+ upgrades = upgrader.determineUpgradeSequence(old_version, current_version, files)
+ self.assertNotEqual(len(upgrades), 0)
Modified: CalendarServer/trunk/txdav/common/datastore/test/util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/test/util.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/test/util.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -36,7 +36,7 @@
from twisted.python import log
from twisted.application.service import Service
-from txdav.common.datastore.sql import CommonDataStore, v1_schema
+from txdav.common.datastore.sql import CommonDataStore, current_sql_schema
from txdav.base.datastore.subpostgres import PostgresService
from txdav.base.datastore.dbapiclient import DiagnosticConnectionWrapper
from txdav.base.propertystore.base import PropertyName
@@ -89,7 +89,7 @@
).chainDeferred(ready)
return Service()
self.sharedService = PostgresService(
- dbRoot, getReady, v1_schema, resetSchema=True,
+ dbRoot, getReady, current_sql_schema, resetSchema=True,
databaseName="caldav",
testMode=True
)
Modified: CalendarServer/trunk/txdav/common/datastore/util.py
===================================================================
--- CalendarServer/trunk/txdav/common/datastore/util.py 2011-07-06 16:57:43 UTC (rev 7712)
+++ CalendarServer/trunk/txdav/common/datastore/util.py 2011-07-06 17:52:43 UTC (rev 7713)
@@ -15,14 +15,16 @@
# limitations under the License.
##
-import os
from twext.python.log import LoggingMixIn
from twisted.application.service import Service
-from txdav.common.datastore.file import CommonDataStore as FileStore, TOPPATHS
+from twisted.internet import reactor
+from twisted.internet.defer import inlineCallbacks
+from twisted.python.modules import getModule
from txdav.caldav.datastore.util import migrateHome as migrateCalendarHome
from txdav.carddav.datastore.util import migrateHome as migrateAddressbookHome
-from twisted.internet.defer import inlineCallbacks
-from twisted.internet import reactor
+from txdav.common.datastore.file import CommonDataStore as FileStore, TOPPATHS
+import os
+import re
class UpgradeToDatabaseService(Service, LoggingMixIn, object):
@@ -157,4 +159,183 @@
self.doMigration()
+class UpgradeDatabaseSchemaService(Service, LoggingMixIn, object):
+ """
+ Checks and upgrades the database schema. This assumes there are a bunch of
+ upgrade files in sql syntax that we can execute against the database to accomplish
+ the upgrade.
+ """
+ @classmethod
+ def wrapService(cls, service, store, uid=None, gid=None):
+ """
+ Create an L{UpgradeDatabaseSchemaService} when starting the database
+ so we can check the schema version and do any upgrades.
+
+ @param service: the service to wrap. This service should be started
+ when the upgrade is complete. (This is accomplished by returning
+ it directly when no upgrade needs to be done, and by adding it to
+ the service hierarchy when the upgrade completes; assuming that the
+ service parent of the resulting service will be set to a
+ L{MultiService} or similar.)
+
+ @param store: the SQL storage service.
+
+ @type service: L{IService}
+
+ @return: a service
+ @rtype: L{IService}
+ """
+ return cls(store, service, uid=uid, gid=gid,)
+
+
+ def __init__(self, sqlStore, service, uid=None, gid=None):
+ """
+ Initialize the service.
+ """
+ self.wrappedService = service
+ self.sqlStore = sqlStore
+ self.uid = uid
+ self.gid = gid
+ self.schemaLocation = getModule(__name__).filePath.sibling("sql_schema")
+
+ @inlineCallbacks
+ def doUpgrade(self):
+ """
+ Do the schema check and upgrade if needed. 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 database schema check.")
+
+ # Retrieve the version number from the schema file
+ current_schema = self.schemaLocation.child("current.sql").getContent()
+ found = re.search("insert into CALENDARSERVER values \('VERSION', '(\d)+'\);", current_schema)
+ if found is None:
+ msg = "Schema is missing required schema VERSION insert statement: %s" % (current_schema,)
+ self.log_error(msg)
+ raise RuntimeError(msg)
+ else:
+ required_version = int(found.group(1))
+ self.log_warn("Required schema version: %s." % (required_version,))
+
+ # Get the schema version in the current database
+ sqlTxn = self.sqlStore.newTransaction()
+ try:
+ actual_version = yield sqlTxn.schemaVersion()
+ yield sqlTxn.commit()
+ except RuntimeError:
+ self.log_error("Database schema version cannot be determined.")
+ yield sqlTxn.abort()
+ raise
+
+ self.log_warn("Actual schema version: %s." % (actual_version,))
+
+ if required_version == actual_version:
+ self.log_warn("Schema version check complete: no upgrade needed.")
+ elif required_version < actual_version:
+ msg = "Actual schema version %s is more recent than the expected version %s. The service cannot be started" % (actual_version, required_version,)
+ self.log_error(msg)
+ raise RuntimeError(msg)
+ else:
+ yield self.upgradeVersion(actual_version, required_version)
+
+ self.log_warn(
+ "Database schema check complete, launching database service."
+ )
+ # see http://twistedmatrix.com/trac/ticket/4649
+ reactor.callLater(0, self.wrappedService.setServiceParent, self.parent)
+
+ @inlineCallbacks
+
+ def upgradeVersion(self, fromVersion, toVersion):
+ """
+ Update the database from one version to another (the current one). Do this by
+ looking for upgrade_from_X_to_Y.sql files that cover the full range of upgrades.
+ """
+
+ self.log_warn("Starting schema upgrade from version %d to %d." % (fromVersion, toVersion,))
+
+ # Scan for all possible upgrade files - returned sorted
+ files = self.scanForUpgradeFiles()
+
+ # Determine upgrade sequence and run each upgrade
+ upgrades = self.determineUpgradeSequence(fromVersion, toVersion, files)
+
+ # Use one transaction for the entire set of upgrades
+ sqlTxn = self.sqlStore.newTransaction()
+ try:
+ for fp in upgrades:
+ yield self.applyUpgrade(sqlTxn, fp)
+ yield sqlTxn.commit()
+ except RuntimeError:
+ self.log_error("Database upgrade failed:" % (fp.basename(),))
+ yield sqlTxn.abort()
+ raise
+
+ self.log_warn("Schema upgraded from version %d to %d." % (fromVersion, toVersion,))
+
+ def scanForUpgradeFiles(self):
+ """
+ Scan the module path for upgrade files with the require name.
+ """
+
+ fp = self.schemaLocation.child("upgrades")
+ upgrades = []
+ regex = re.compile("upgrade_from_(\d)+_to_(\d)+.sql")
+ for child in fp.globChildren("upgrade_*.sql"):
+ matched = regex.match(child.basename())
+ if matched is not None:
+ fromV = int(matched.group(1))
+ toV = int(matched.group(2))
+ upgrades.append((fromV, toV, child))
+
+ upgrades.sort(key=lambda x:(x[0], x[1]))
+ return upgrades
+
+ def determineUpgradeSequence(self, fromVersion, toVersion, files):
+ """
+ Determine the upgrade_from_X_to_Y.sql files that cover the full range of upgrades.
+ Note that X and Y may not be consecutive, e.g., we might have an upgrade from 3 to 4,
+ 4 to 5, and 3 to 5 - the later because it is more efficient to jump over the intermediate
+ step. As a result we will always try and pick the upgrade file that gives the biggest
+ jump from one version to another at each step.
+ """
+
+ # Now find the path from the old version to the current one
+ filesByFromVersion = {}
+ for fromV, toV, fp in files:
+ if fromV not in filesByFromVersion or filesByFromVersion[fromV][1] < toV:
+ filesByFromVersion[fromV] = fromV, toV, fp
+
+ upgrades = []
+ nextVersion = fromVersion
+ while nextVersion != toVersion:
+ if nextVersion not in filesByFromVersion:
+ msg = "Missing upgrade file from version %d" % (nextVersion, )
+ self.log_error(msg)
+ raise RuntimeError(msg)
+ else:
+ upgrades.append(filesByFromVersion[nextVersion][2])
+ nextVersion = filesByFromVersion[nextVersion][1]
+
+ return upgrades
+
+ @inlineCallbacks
+ def applyUpgrade(self, sqlTxn, fp):
+ """
+ Apply the schema upgrade .sql file to the database.
+ """
+ self.log_warn("Applying schema upgrade: %s" % (fp.basename(),))
+ sql = fp.getContent()
+ yield sqlTxn.execSQL(sql)
+
+ def startService(self):
+ """
+ Start the service.
+ """
+ self.doUpgrade()
+
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110706/a7298530/attachment-0001.html>
More information about the calendarserver-changes
mailing list