aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/dav/bin/migrateto21.php
blob: 9096435838008b6223fe7a09426b13ba7f3b6741 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env php
<?php

echo "SabreDAV migrate script for version 2.1\n";

if ($argc < 2) {
    echo <<<HELLO

This script help you migrate from a pre-2.1 database to 2.1.

Changes:
  The 'calendarobjects' table will be upgraded.
  'schedulingobjects' will be created.

If you don't use the default PDO CalDAV or CardDAV backend, it's pointless to
run this script.

Keep in mind that ALTER TABLE commands will be executed. If you have a large
dataset this may mean that this process takes a while.

Lastly: Make a back-up first. This script has been tested, but the amount of
potential variants are extremely high, so it's impossible to deal with every
possible situation.

In the worst case, you will lose all your data. This is not an overstatement.

Usage:

php {$argv[0]} [pdo-dsn] [username] [password]

For example:

php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password
php {$argv[0]} sqlite:data/sabredav.db

HELLO;

    exit();
}

// There's a bunch of places where the autoloader could be, so we'll try all of
// them.
$paths = [
    __DIR__.'/../vendor/autoload.php',
    __DIR__.'/../../../autoload.php',
];

foreach ($paths as $path) {
    if (file_exists($path)) {
        include $path;
        break;
    }
}

$dsn = $argv[1];
$user = isset($argv[2]) ? $argv[2] : null;
$pass = isset($argv[3]) ? $argv[3] : null;

echo 'Connecting to database: '.$dsn."\n";

$pdo = new PDO($dsn, $user, $pass);
$pdo->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;
}

echo "Done.\n";

echo "Upgrade to 2.1 schema completed.\n";