]> git.mxchange.org Git - friendica-addons.git/blobdiff - dav/database-init.inc.php
More refactoring, mainly of the addressbook/VCARD-part
[friendica-addons.git] / dav / database-init.inc.php
index 6372bf442668c432d581c6a7a6a35efff0268e57..7c0d23a3a98396621817db629bbaf14c06b560e5 100644 (file)
@@ -9,103 +9,86 @@ function dav_get_update_statements($from_version)
 {
        $stms = array();
 
-       if ($from_version <= 0) {
-               $stms[] = "ALTER TABLE `dav_calendars` ADD `uri` VARCHAR( 50 ) NULL DEFAULT NULL AFTER `description` , ADD `has_vevent` TINYINT NOT NULL DEFAULT '1' AFTER `uri` , ADD `has_vtodo` TINYINT NOT NULL DEFAULT '1' AFTER `has_vevent`";
-
-               $stms[] = "UPDATE `dav_calendars` SET `uri` = 'private' WHERE `namespace` = 1";
-               $stms[] = "UPDATE `dav_calendars` SET `uri` = 'friendica-mine' WHERE `namespace` = 2 AND `namespace_id` = 1";
-               $stms[] = "UPDATE `dav_calendars` SET `uri` = 'friendica-contacts' WHERE `namespace` = 2 AND `namespace_id` = 2";
-               $stms[] = "ALTER TABLE `dav_calendars` DROP PRIMARY KEY ";
-               $stms[] = "ALTER TABLE `dav_calendars` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ";
-
-               $stms[] = "ALTER TABLE `dav_calendarobjects` ADD `calendar_id` INT NOT NULL AFTER `id` ";
-               $stms[] = "UPDATE `dav_calendarobjects` a JOIN `dav_calendars` b ON a.`namespace` = b.`namespace` AND a.`namespace_id` = b.`namespace_id` SET a.`calendar_id` = b.`id`";
-               $stms[] = "ALTER TABLE `dav_calendarobjects` DROP `namespace` , DROP `namespace_id` ;";
-               $stms[] = "ALTER TABLE `dav_calendarobjects` ADD INDEX ( `calendar_id` ) ";
-               $stms[] = "ALTER TABLE `dav_calendarobjects` ADD `componentType` ENUM( 'VEVENT', 'VTODO' ) NOT NULL DEFAULT 'VEVENT' AFTER `calendardata` ,
-                       ADD `firstOccurence` TIMESTAMP NOT NULL AFTER `lastmodified` ,
-                       ADD `lastOccurence` TIMESTAMP NOT NULL AFTER `firstOccurence`";
-
-               $stms[] = "DROP TABLE `dav_jqcalendar`";
-               $stms[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` (
-                       `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-                       `ical_recurr_uri` varchar(100) DEFAULT NULL,
-                       `calendar_id` int(10) unsigned NOT NULL,
-                       `calendarobject_id` int(10) unsigned NOT NULL,
-                       `Subject` varchar(1000) NOT NULL,
-                       `StartTime` timestamp NULL DEFAULT NULL,
-                       `EndTime` timestamp NULL DEFAULT NULL,
-                       `IsEditable` tinyint(3) unsigned NOT NULL,
-                       `IsAllDayEvent` tinyint(4) NOT NULL,
-                       `IsRecurring` tinyint(4) NOT NULL,
-                       `Color` CHAR(6) DEFAULT NULL,
-                       PRIMARY KEY (`id`),
-                       KEY `calendarByStart` (`calendar_id`,`StartTime`),
-                       KEY `calendarobject_id` (`calendarobject_id`,`ical_recurr_uri`)
-                       ) DEFAULT CHARSET=utf8 ";
-
-
-               $stms[] = "ALTER TABLE `dav_notifications` ADD `calendar_id` INT NOT NULL AFTER `ical_recurr_uri` ";
-               $stms[] = "ALTER TABLE `dav_notifications` DROP INDEX `ical_uri` , ADD INDEX `ical_uri` ( `calendar_id` , `ical_uri` , `ical_recurr_uri` ) ";
-               $stms[] = "TRUNCATE TABLE `dav_notifications`";
-               $stms[] = "ALTER TABLE `dav_notifications` DROP `namespace` , DROP `namespace_id`";
-
-               $stms[] = "TRUNCATE TABLE `dav_cal_virtual_object_cache`";
-               $stme[] = "ALTER TABLE `dav_cal_virtual_object_cache` ADD `calendar_id` INT UNSIGNED NOT NULL AFTER `id` ";
-               $stms[] = "ALTER TABLE `dav_cal_virtual_object_cache` DROP INDEX `ref_type` , ADD INDEX `ref_type` ( `calendar_id` , `data_end` )  ";
-               $stms[] = "ALTER TABLE `dav_cal_virtual_object_cache` DROP `uid`, DROP `namespace` , DROP `namespace_id` ";
-
-               $stms[] = "CREATE TABLE `friendica`.`dav_cal_virtual_object_sync` (
-                       `calendar_id` INT UNSIGNED NOT NULL ,
-                       `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
-                       PRIMARY KEY ( `calendar_id` )
-                       ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
-
-               $stms[] = "DROP TABLE `dav_cache_synchronized` ";
-
-               $stms[] = "UPDATE `dav_calendars` SET `namespace` = 1, `namespace_id` = `uid`"; // last
-               $stms[] = "ALTER TABLE `dav_calendars` DROP `uid`";
+       if ($from_version == 1) {
+               $stms[] = "ALTER TABLE `dav_calendarobjects`
+                       ADD `calendar_id` INT NOT NULL AFTER `namespace_id` ,
+                       ADD `user_temp` INT NOT NULL AFTER `calendar_id` ";
+               $stms[] = "ALTER TABLE `dav_calendarobjects`
+                       ADD `componentType` ENUM( 'VEVENT', 'VTODO' ) NOT NULL DEFAULT 'VEVENT' AFTER `lastmodified` ,
+                       ADD `firstOccurence` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `componentType` ,
+                       ADD `lastOccurence` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `firstOccurence`";
+               $stms[] = "UPDATE dav_calendarobjects a JOIN dav_calendars b ON a.namespace = b.namespace AND a.namespace_id = b.namespace_id SET a.user_temp = b.uid";
+               $stms[] = "DROP TABLE IF EXISTS
+                       `dav_addressbooks_community` ,
+                       `dav_addressbooks_phone` ,
+                       `dav_cache_synchronized` ,
+                       `dav_caldav_log` ,
+                       `dav_calendars` ,
+                       `dav_cal_virtual_object_cache` ,
+                       `dav_cards` ,
+                       `dav_jqcalendar` ,
+                       `dav_locks` ,
+                       `dav_notifications` ;";
+
+               $stms = array_merge($stms, dav_get_create_statements(array("dav_calendarobjects")));
+
+               $user_ids = q("SELECT DISTINCT `uid` FROM %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
+               foreach ($user_ids as $user) $stms = array_merge($stms, wdcal_create_std_calendars_get_statements($user["uid"], false));
+
+               $stms[] = "UPDATE dav_calendarobjects a JOIN dav_calendars b
+                       ON b.`namespace` = " . CALDAV_NAMESPACE_PRIVATE . " AND a.`user_temp` = b.`namespace_id` AND b.`uri` = 'private'
+                       SET a.`calendar_id` = b.`id`";
+
+               $stms[] = "ALTER TABLE `dav_calendarobjects` DROP `namespace`, DROP `namespace_id`, DROP `user_temp`";
+
+       }
+
+       if (in_array($from_version, array(1, 2))) {
+               $stms[] = "DROP TABLE `dav_addressbooks_phone`";
+               $stms[] = "DROP TABLE `dav_addressbooks_community`";
+               $stms[] = "DROP TABLE `dav_cards`";
+
+               $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `namespace` mediumint(9) NOT NULL,
+  `namespace_id` int(11) unsigned NOT NULL,
+  `displayname` varchar(200) NOT NULL,
+  `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
+  `uri` varchar(50) NOT NULL,
+  `ctag` int(11) unsigned NOT NULL DEFAULT '1',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
+
+               $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `addressbook_id` int(11) unsigned NOT NULL,
+  `contact` int(11) DEFAULT NULL,
+  `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
+  `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
+  `lastmodified` timestamp NULL DEFAULT NULL,
+  `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
+  `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
+  `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+  `size` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `namespace` (`addressbook_id`,`contact`),
+  KEY `contact` (`contact`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
        }
 
        return $stms;
 }
 
 /**
+ * @param array $except
  * @return array
  */
-function dav_get_create_statements()
+function dav_get_create_statements($except = array())
 {
        $arr = array();
 
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_community` (
-               `uid` int(11) NOT NULL,
-  `ctag` int(11) unsigned NOT NULL DEFAULT '1',
-  PRIMARY KEY (`uid`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_phone` (
-               `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `uid` int(11) NOT NULL,
-  `principaluri` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
-  `displayname` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
-  `uri` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
-  `description` text COLLATE utf8_unicode_ci,
-  `ctag` int(11) unsigned NOT NULL DEFAULT '1',
-  PRIMARY KEY (`id`),
-  UNIQUE KEY `principaluri` (`principaluri`,`uri`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cache_synchronized` (
-               `uid` mediumint(8) unsigned NOT NULL,
-  `namespace` smallint(6) NOT NULL,
-  `namespace_id` int(11) NOT NULL,
-  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-  PRIMARY KEY (`uid`,`namespace`,`namespace_id`),
-  KEY `namespace` (`namespace`,`namespace_id`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8";
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_caldav_log` (
-               `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+       if (!in_array("dav_caldav_log", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_caldav_log` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `uid` mediumint(9) NOT NULL,
   `ip` varchar(15) NOT NULL,
   `user_agent` varchar(100) NOT NULL,
@@ -114,129 +97,123 @@ function dav_get_create_statements()
   `url` varchar(100) NOT NULL,
   PRIMARY KEY (`id`),
   KEY `mitglied` (`uid`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8";
+) ENGINE=InnoDB DEFAULT CHARSET=utf8";
 
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendarobjects` (
-               `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-  `namespace` mediumint(9) NOT NULL,
-  `namespace_id` int(10) unsigned NOT NULL,
+       if (!in_array("dav_calendarobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendarobjects` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `calendar_id` int(11) NOT NULL,
   `calendardata` text,
   `uri` varchar(200) NOT NULL,
   `lastmodified` timestamp NULL DEFAULT NULL,
+  `componentType` enum('VEVENT','VTODO') NOT NULL DEFAULT 'VEVENT',
+  `firstOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+  `lastOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   `etag` varchar(15) NOT NULL,
   `size` int(10) unsigned NOT NULL,
   PRIMARY KEY (`id`),
-  UNIQUE KEY `uri` (`uri`,`namespace`,`namespace_id`),
-  KEY `namespace` (`namespace`,`namespace_id`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
+  UNIQUE KEY `uri` (`uri`),
+  KEY `calendar_id` (`calendar_id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
 
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendars` (
-               `namespace` mediumint(9) NOT NULL,
+       if (!in_array("dav_calendars", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendars` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `namespace` mediumint(9) NOT NULL,
   `namespace_id` int(10) unsigned NOT NULL,
-  `uid` mediumint(8) unsigned NOT NULL,
   `calendarorder` int(11) NOT NULL DEFAULT '1',
-  `calendarcolor` varchar(20) NOT NULL DEFAULT '#5858FF',
+  `calendarcolor` char(6) NOT NULL DEFAULT '5858FF',
   `displayname` varchar(200) NOT NULL,
   `timezone` text NOT NULL,
   `description` varchar(500) NOT NULL,
-  `uri` varchar(50) DEFAULT NULL,
+  `uri` varchar(50) NOT NULL DEFAULT '',
   `has_vevent` tinyint(4) NOT NULL DEFAULT '1',
   `has_vtodo` tinyint(4) NOT NULL DEFAULT '1',
   `ctag` int(10) unsigned NOT NULL,
-  PRIMARY KEY (`namespace`,`namespace_id`),
-  KEY `uid` (`uid`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8";
+  PRIMARY KEY (`id`),
+  UNIQUE KEY (`namespace` , `namespace_id` , `uri`),
+  KEY `uri` (`uri`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
 
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_cache` (
-               `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-  `uid` mediumint(8) unsigned NOT NULL,
-  `namespace` mediumint(9) NOT NULL,
-  `namespace_id` int(11) NOT NULL DEFAULT '0',
+       if (!in_array("dav_cal_virtual_object_cache", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_cache` (
+  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
+  `calendar_id` int(10) unsigned NOT NULL,
   `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `data_uri` char(80) NOT NULL,
-  `data_subject` varchar(1000) NOT NULL,
+  `data_summary` varchar(1000) NOT NULL,
   `data_location` varchar(1000) NOT NULL,
-  `data_description` text NOT NULL,
   `data_start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   `data_end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
   `data_allday` tinyint(4) NOT NULL,
   `data_type` varchar(20) NOT NULL,
-  `ical` text NOT NULL,
-  `ical_size` int(11) NOT NULL,
-  `ical_etag` varchar(15) NOT NULL,
-  PRIMARY KEY (`id`),
-  KEY `ref_type` (`namespace`,`namespace_id`),
-  KEY `mitglied` (`uid`,`data_end`),
-  KEY `data_uri` (`data_uri`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8";
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cards` (
-               `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-  `namespace` tinyint(3) unsigned NOT NULL,
-  `namespace_id` int(11) unsigned NOT NULL,
-  `contact` int(11) DEFAULT NULL,
-  `carddata` mediumtext COLLATE utf8_unicode_ci,
-  `uri` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
-  `lastmodified` int(11) unsigned DEFAULT NULL,
-  `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
-  `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
-  `etag` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
-  `size` int(10) unsigned NOT NULL,
+  `calendardata` text NOT NULL,
+  `size` int(11) NOT NULL,
+  `etag` varchar(15) NOT NULL,
   PRIMARY KEY (`id`),
-  UNIQUE KEY `namespace` (`namespace`,`namespace_id`,`contact`),
-  KEY `contact` (`contact`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
+  KEY `data_uri` (`data_uri`),
+  KEY `ref_type` (`calendar_id`,`data_end`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
 
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` (
-               `id` int(11) NOT NULL AUTO_INCREMENT,
-  `uid` int(10) unsigned NOT NULL,
-  `ical_uri` varchar(200) NOT NULL,
-  `ical_recurr_uri` varchar(100) NOT NULL,
-  `namespace` mediumint(9) NOT NULL,
-  `namespace_id` int(11) NOT NULL,
-  `permission_edit` tinyint(4) NOT NULL DEFAULT '1',
-  `Subject` varchar(1000) DEFAULT NULL,
-  `Location` varchar(1000) DEFAULT NULL,
-  `Description` text,
+       if (!in_array("dav_cal_virtual_object_sync", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_sync` (
+  `calendar_id` int(10) unsigned NOT NULL,
+  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  PRIMARY KEY (`calendar_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8";
+
+       if (!in_array("dav_jqcalendar", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `ical_recurr_uri` varchar(100) DEFAULT NULL,
+  `calendar_id` int(10) unsigned NOT NULL,
+  `calendarobject_id` int(10) unsigned NOT NULL,
+  `Summary` varchar(100) NOT NULL,
   `StartTime` timestamp NULL DEFAULT NULL,
   `EndTime` timestamp NULL DEFAULT NULL,
-  `IsAllDayEvent` smallint(6) NOT NULL,
-  `Color` varchar(20) DEFAULT NULL,
-  `RecurringRule` varchar(500) DEFAULT NULL,
+  `IsEditable` tinyint(3) unsigned NOT NULL,
+  `IsAllDayEvent` tinyint(4) NOT NULL,
+  `IsRecurring` tinyint(4) NOT NULL,
+  `Color` char(6) DEFAULT NULL,
   PRIMARY KEY (`id`),
-  KEY `user` (`uid`,`StartTime`),
-  KEY `zuord_typ` (`namespace`,`namespace_id`),
-  KEY `ical_uri` (`ical_uri`,`ical_recurr_uri`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_locks` (
-               `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-  `owner` varchar(100) DEFAULT NULL,
-  `timeout` int(10) unsigned DEFAULT NULL,
-  `created` int(11) DEFAULT NULL,
-  `token` varchar(100) DEFAULT NULL,
-  `scope` tinyint(4) DEFAULT NULL,
-  `depth` tinyint(4) DEFAULT NULL,
-  `uri` text,
-  PRIMARY KEY (`id`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8";
-
-
-       $arr[] = "CREATE TABLE IF NOT EXISTS `dav_notifications` (
-               `id` int(11) NOT NULL AUTO_INCREMENT,
-  `uid` int(10) unsigned NOT NULL,
-  `ical_uri` varchar(200) NOT NULL,
-  `ical_recurr_uri` varchar(100) NOT NULL,
-  `namespace` mediumint(8) unsigned NOT NULL,
-  `namespace_id` int(10) unsigned NOT NULL,
+  KEY `calendarByStart` (`calendar_id`,`StartTime`),
+  KEY `calendarobject_id` (`calendarobject_id`,`ical_recurr_uri`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
+
+       if (!in_array("dav_notifications", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_notifications` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `ical_recurr_uri` varchar(100) DEFAULT NULL,
+  `calendar_id` int(11) NOT NULL,
+  `calendarobject_id` int(10) unsigned NOT NULL,
+  `action` enum('email','display') NOT NULL DEFAULT 'email',
   `alert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-  `rel_type` enum('second','minute','hour','day','week','month','year') NOT NULL,
-  `rel_value` mediumint(9) NOT NULL,
   `notified` tinyint(4) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`),
   KEY `notified` (`notified`,`alert_date`),
-  KEY `ical_uri` (`uid`,`ical_uri`,`ical_recurr_uri`)
-) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
+  KEY `calendar_id` (`calendar_id`,`calendarobject_id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
+
+       if (!in_array("dav_addressbooks", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `namespace` mediumint(9) NOT NULL,
+  `namespace_id` int(11) unsigned NOT NULL,
+  `displayname` varchar(200) NOT NULL,
+  `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
+  `uri` varchar(50) NOT NULL,
+  `ctag` int(11) unsigned NOT NULL DEFAULT '1',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
+
+       if (!in_array("dav_addressbookobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `addressbook_id` int(11) unsigned NOT NULL,
+  `contact` int(11) DEFAULT NULL,
+  `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
+  `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
+  `lastmodified` timestamp NULL DEFAULT NULL,
+  `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
+  `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
+  `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
+  `size` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `namespace` (`addressbook_id`,`contact`),
+  KEY `contact` (`contact`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
 
        return $arr;
 }
@@ -246,10 +223,12 @@ function dav_get_create_statements()
  */
 function dav_check_tables()
 {
-       $dbv = get_config("dav", "db_version");
-       if ($dbv == CALDAV_DB_VERSION) return 0; // Correct
-       if (is_numeric($dbv) || $dbv == "CALDAV_DB_VERSION") return 1; // Older version (update needed)
-       return -1; // Not installed
+       $x = q("DESCRIBE %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
+       if (!$x) return -1;
+       if (count($x) == 9) return 1; // Version 0.1
+       // @TODO Detect Version 0.2
+       if (count($x) == 12) return 0; // Correct
+       return -2; // Unknown version
 }
 
 
@@ -267,8 +246,6 @@ function dav_create_tables()
                if ($db->error) $errors[] = $db->error;
        }
 
-       if (count($errors) == 0) set_config("dav", "db_version", CALDAV_DB_VERSION);
-
        return $errors;
 }
 
@@ -277,9 +254,9 @@ function dav_create_tables()
  */
 function dav_upgrade_tables()
 {
-       $dbv = get_config("dav", "db_version");
-       if ($dbv == "CALDAV_DB_VERSION") $ver = 0;
-       else $ver = IntVal($dbv);
+       $ver = dav_check_tables();
+       if (!in_array($ver, array(1))) return array("Unknown error");
+
        $stms   = dav_get_update_statements($ver);
        $errors = array();
 
@@ -289,7 +266,5 @@ function dav_upgrade_tables()
                if ($db->error) $errors[] = $db->error;
        }
 
-       if (count($errors) == 0) set_config("dav", "db_version", CALDAV_DB_VERSION);
-
        return $errors;
 }
\ No newline at end of file