#!/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 'propertystorage'\n"; $addValueType = false; try { $result = $pdo->query('SELECT * FROM propertystorage LIMIT 1'); $row = $result->fetch(\PDO::FETCH_ASSOC); if (!$row) { echo "No data in table. Going to re-create the table.\n"; $random = mt_rand(1000,9999); echo "Renaming propertystorage -> propertystorage_old$random and creating new table.\n"; switch($driver) { case 'mysql' : $pdo->exec('RENAME TABLE propertystorage TO propertystorage_old' . $random); $pdo->exec(' CREATE TABLE propertystorage ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, path VARBINARY(1024) NOT NULL, name VARBINARY(100) NOT NULL, valuetype INT UNSIGNED, value MEDIUMBLOB ); '); $pdo->exec('CREATE UNIQUE INDEX path_property_' . $random . ' ON propertystorage (path(600), name(100));'); break; case 'sqlite' : $pdo->exec('ALTER TABLE propertystorage RENAME TO propertystorage_old' . $random); $pdo->exec(' CREATE TABLE propertystorage ( id integer primary key asc, path text, name text, valuetype integer, value blob );'); $pdo->exec('CREATE UNIQUE INDEX path_property_' . $random . ' ON propertystorage (path, name);'); break; } } elseif (array_key_exists('valuetype', $row)) { echo "valuetype field exists. Assuming that this part of the migration has\n"; echo "Already been completed.\n"; } else { echo "2.1 schema detected. Going to perform upgrade.\n"; $addValueType = true; } } catch (Exception $e) { echo "Could not find a propertystorage table. Skipping this part of the\n"; echo "upgrade.\n"; echo $e->getMessage(), "\n"; } if ($addValueType) { switch($driver) { case 'mysql' : $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT UNSIGNED'); break; case 'sqlite' : $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT'); break; } $pdo->exec('UPDATE propertystorage SET valuetype = 1 WHERE valuetype IS NULL '); } echo "Migrating vcardurl\n"; $result = $pdo->query('SELECT id, uri, vcardurl FROM principals WHERE vcardurl IS NOT NULL'); $stmt1 = $pdo->prepare('INSERT INTO propertystorage (path, name, valuetype, value) VALUES (?, ?, 3, ?)'); while($row = $result->fetch(\PDO::FETCH_ASSOC)) { // Inserting the new record $stmt1->execute([ 'addressbooks/' . basename($row['uri']), '{http://calendarserver.org/ns/}me-card', serialize(new Sabre\DAV\Xml\Property\Href($row['vcardurl'])) ]); echo serialize(new Sabre\DAV\Xml\Property\Href($row['vcardurl'])); } echo "Done.\n"; echo "Upgrade to 3.0 schema completed.\n";