4 echo "SabreDAV migrate script for version 3.2\n";
10 This
script help you migrate from
a 3.1 database to 3.2
and later
13 * Created
a new calendarinstances table to support calendar sharing.
14 * Remove
a lot
of columns from calendars.
16 Keep
in mind
that ALTER TABLE commands will be executed. If you have
a large
17 dataset
this may mean
that this process takes
a while.
20 potential variants
are extremely high, so it
's impossible to deal with every 23 In the worst case, you will lose all your data. This is not an overstatement. 25 Lastly, if you are upgrading from an older version than 3.1, make sure you run 26 the earlier migration script first. Migration scripts must be ran in order. 30 php {$argv[0]} [pdo-dsn] [username] [password] 34 php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password 35 php {$argv[0]} sqlite:data/sabredav.db 43 // There's a bunch
of places where
the autoloader could be, so we
'll try all of 46 __DIR__ . '/../vendor/autoload.php
', 47 __DIR__ . '/../../../autoload.php
', 50 foreach ($paths as $path) { 51 if (file_exists($path)) { 58 $user = isset($argv[2]) ? $argv[2] : null; 59 $pass = isset($argv[3]) ? $argv[3] : null; 61 $backupPostfix = time(); 63 echo "Connecting to database: " . $dsn . "\n"; 65 $pdo = new PDO($dsn, $user, $pass); 66 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 67 $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 69 $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); 74 echo "Detected MySQL.\n"; 77 echo "Detected SQLite.\n"; 80 echo "Error: unsupported driver: " . $driver . "\n"; 84 echo "Creating 'calendarinstances
'\n"; 85 $addValueType = false; 87 $result = $pdo->query('SELECT * FROM calendarinstances LIMIT 1
'); 88 $result->fetch(\PDO::FETCH_ASSOC); 89 echo "calendarinstances exists. Assuming this part of the migration has already been done.\n"; 90 } catch (Exception $e) { 91 echo "calendarinstances does not yet exist. Creating table and migrating data.\n"; 96 CREATE TABLE calendarinstances ( 97 id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, 98 calendarid INTEGER UNSIGNED NOT NULL, 99 principaluri VARBINARY(100), 100 access TINYINT(1) NOT NULL DEFAULT '1
' COMMENT '1 = owner, 2 = read, 3 = readwrite
', 101 displayname VARCHAR(100), 104 calendarorder INT(11) UNSIGNED NOT NULL DEFAULT '0
', 105 calendarcolor VARBINARY(10), 107 transparent TINYINT(1) NOT NULL DEFAULT '0
', 108 share_href VARBINARY(100), 109 share_displayname VARCHAR(100), 110 share_invitestatus TINYINT(1) NOT NULL DEFAULT '2
' COMMENT '1 = noresponse, 2 = accepted, 3 = declined, 4 = invalid
', 111 UNIQUE(principaluri, uri), 112 UNIQUE(calendarid, principaluri), 113 UNIQUE(calendarid, share_href) 114 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 118 INSERT INTO calendarinstances 145 CREATE TABLE calendarinstances ( 146 id integer primary key asc NOT NULL, 149 access integer COMMENT '1 = owner, 2 = read, 3 = readwrite
' NOT NULL DEFAULT '1
', 153 calendarorder integer, 158 share_displayname text, 159 share_invitestatus integer DEFAULT '2
', 160 UNIQUE (principaluri, uri), 161 UNIQUE (calendarid, principaluri), 162 UNIQUE (calendarid, share_href) 167 INSERT INTO calendarinstances 196 $result = $pdo->query('SELECT * FROM calendars LIMIT 1
'); 197 $row = $result->fetch(\PDO::FETCH_ASSOC); 200 echo "Source table is empty.\n"; 201 $migrateCalendars = true; 204 $columnCount = count($row); 205 if ($columnCount === 3) { 206 echo "The calendars table has 3 columns already. Assuming this part of the migration was already done.\n"; 207 $migrateCalendars = false; 209 echo "The calendars table has " . $columnCount . " columns.\n"; 210 $migrateCalendars = true; 213 } catch (Exception $e) { 214 echo "calendars table does not exist. This is a major problem. Exiting.\n"; 218 if ($migrateCalendars) { 220 $calendarBackup = 'calendars_3_1_
' . $backupPostfix; 221 echo "Backing up 'calendars
' to '", $calendarBackup, "'\n"; 225 $pdo->exec('RENAME TABLE calendars TO
' . $calendarBackup); 228 $pdo->exec('ALTER TABLE calendars RENAME TO
' . $calendarBackup); 233 echo "Creating new calendars table.\n"; 237 CREATE TABLE calendars ( 238 id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, 239 synctoken INTEGER UNSIGNED NOT NULL DEFAULT '1
', 240 components VARBINARY(21) 241 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 247 CREATE TABLE calendars ( 248 id integer primary key asc NOT NULL, 249 synctoken integer DEFAULT 1 NOT NULL, 250 components text NOT NULL 258 echo "Migrating data from old to new table\n"; 261 INSERT INTO calendars (id, synctoken, components) SELECT id, synctoken, COALESCE(components,"VEVENT,VTODO,VJOURNAL") as components FROM $calendarBackup 268 echo "Upgrade to 3.2 schema completed.\n";