#!/usr/bin/env php setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); switch($driver) { case 'mysql' : echo "Detected MySQL.\n"; break; case 'sqlite' : echo "Detected SQLite.\n"; break; default : echo "Error: unsupported driver: " . $driver . "\n"; die(-1); } echo "Upgrading 'calendarobjects'\n"; $addUid = false; try { $result = $pdo->query('SELECT * FROM calendarobjects LIMIT 1'); $row = $result->fetch(\PDO::FETCH_ASSOC); if (!$row) { echo "No data in table. Going to try to add the uid field anyway.\n"; $addUid = true; } elseif (array_key_exists('uid', $row)) { echo "uid field exists. Assuming that this part of the migration has\n"; echo "Already been completed.\n"; } else { echo "2.0 schema detected.\n"; $addUid = true; } } catch (Exception $e) { echo "Could not find a calendarobjects table. Skipping this part of the\n"; echo "upgrade.\n"; } if ($addUid) { switch($driver) { case 'mysql' : $pdo->exec('ALTER TABLE calendarobjects ADD uid VARCHAR(200)'); break; case 'sqlite' : $pdo->exec('ALTER TABLE calendarobjects ADD uid TEXT'); break; } $result = $pdo->query('SELECT id, calendardata FROM calendarobjects'); $stmt = $pdo->prepare('UPDATE calendarobjects SET uid = ? WHERE id = ?'); $counter = 0; while($row = $result->fetch(\PDO::FETCH_ASSOC)) { try { $vobj = \Sabre\VObject\Reader::read($row['calendardata']); } catch (\Exception $e) { echo "Warning! Item with id $row[id] could not be parsed!\n"; continue; } $uid = null; $item = $vobj->getBaseComponent(); if (!isset($item->UID)) { echo "Warning! Item with id $item[id] does NOT have a UID property and this is required.\n"; continue; } $uid = (string)$item->UID; $stmt->execute([$uid, $row['id']]); $counter++; } } echo "Creating 'schedulingobjects'\n"; switch($driver) { case 'mysql' : $pdo->exec('CREATE TABLE IF NOT EXISTS schedulingobjects ( id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, principaluri VARCHAR(255), calendardata MEDIUMBLOB, uri VARCHAR(200), lastmodified INT(11) UNSIGNED, etag VARCHAR(32), size INT(11) UNSIGNED NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; '); break; case 'sqlite' : $pdo->exec('CREATE TABLE IF NOT EXISTS schedulingobjects ( id integer primary key asc, principaluri text, calendardata blob, uri text, lastmodified integer, etag text, size integer ) '); break; $pdo->exec(' CREATE INDEX principaluri_uri ON calendarsubscriptions (principaluri, uri); '); break; } echo "Done.\n"; echo "Upgrade to 2.1 schema completed.\n";