ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
migrateto32.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 
4 echo "SabreDAV migrate script for version 3.2\n";
5 
6 if ($argc < 2) {
7 
8  echo <<<HELLO
9 
10 This script help you migrate from a 3.1 database to 3.2 and later
11 
12 Changes:
13 * Created a new calendarinstances table to support calendar sharing.
14 * Remove a lot of columns from calendars.
15 
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.
18 
19 Make a back-up first. This script has been tested, but the amount of
20 potential variants are extremely high, so it's impossible to deal with every
21 possible situation.
22 
23 In the worst case, you will lose all your data. This is not an overstatement.
24 
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.
27 
28 Usage:
29 
30 php {$argv[0]} [pdo-dsn] [username] [password]
31 
32 For example:
33 
34 php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password
35 php {$argv[0]} sqlite:data/sabredav.db
36 
37 HELLO;
38 
39  exit();
40 
41 }
42 
43 // There's a bunch of places where the autoloader could be, so we'll try all of
44 // them.
45 $paths = [
46  __DIR__ . '/../vendor/autoload.php',
47  __DIR__ . '/../../../autoload.php',
48 ];
49 
50 foreach ($paths as $path) {
51  if (file_exists($path)) {
52  include $path;
53  break;
54  }
55 }
56 
57 $dsn = $argv[1];
58 $user = isset($argv[2]) ? $argv[2] : null;
59 $pass = isset($argv[3]) ? $argv[3] : null;
60 
61 $backupPostfix = time();
62 
63 echo "Connecting to database: " . $dsn . "\n";
64 
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);
68 
69 $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
70 
71 switch ($driver) {
72 
73  case 'mysql' :
74  echo "Detected MySQL.\n";
75  break;
76  case 'sqlite' :
77  echo "Detected SQLite.\n";
78  break;
79  default :
80  echo "Error: unsupported driver: " . $driver . "\n";
81  die(-1);
82 }
83 
84 echo "Creating 'calendarinstances'\n";
85 $addValueType = false;
86 try {
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";
92 
93  switch ($driver) {
94  case 'mysql' :
95  $pdo->exec(<<<SQL
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),
102  uri VARBINARY(200),
103  description TEXT,
104  calendarorder INT(11) UNSIGNED NOT NULL DEFAULT '0',
105  calendarcolor VARBINARY(10),
106  timezone TEXT,
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;
115 SQL
116  );
117  $pdo->exec("
118 INSERT INTO calendarinstances
119  (
120  calendarid,
121  principaluri,
122  access,
123  displayname,
124  uri,
125  description,
126  calendarorder,
127  calendarcolor,
128  transparent
129  )
130 SELECT
131  id,
132  principaluri,
133  1,
134  displayname,
135  uri,
136  description,
137  calendarorder,
138  calendarcolor,
139  transparent
140 FROM calendars
141 ");
142  break;
143  case 'sqlite' :
144  $pdo->exec(<<<SQL
145 CREATE TABLE calendarinstances (
146  id integer primary key asc NOT NULL,
147  calendarid integer,
148  principaluri text,
149  access integer COMMENT '1 = owner, 2 = read, 3 = readwrite' NOT NULL DEFAULT '1',
150  displayname text,
151  uri text NOT NULL,
152  description text,
153  calendarorder integer,
154  calendarcolor text,
155  timezone text,
156  transparent bool,
157  share_href text,
158  share_displayname text,
159  share_invitestatus integer DEFAULT '2',
160  UNIQUE (principaluri, uri),
161  UNIQUE (calendarid, principaluri),
162  UNIQUE (calendarid, share_href)
163 );
164 SQL
165  );
166  $pdo->exec("
167 INSERT INTO calendarinstances
168  (
169  calendarid,
170  principaluri,
171  access,
172  displayname,
173  uri,
174  description,
175  calendarorder,
176  calendarcolor,
177  transparent
178  )
179 SELECT
180  id,
181  principaluri,
182  1,
183  displayname,
184  uri,
185  description,
186  calendarorder,
187  calendarcolor,
188  transparent
189 FROM calendars
190 ");
191  break;
192  }
193 
194 }
195 try {
196  $result = $pdo->query('SELECT * FROM calendars LIMIT 1');
197  $row = $result->fetch(\PDO::FETCH_ASSOC);
198 
199  if (!$row) {
200  echo "Source table is empty.\n";
201  $migrateCalendars = true;
202  }
203 
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;
208  } else {
209  echo "The calendars table has " . $columnCount . " columns.\n";
210  $migrateCalendars = true;
211  }
212 
213 } catch (Exception $e) {
214  echo "calendars table does not exist. This is a major problem. Exiting.\n";
215  exit(-1);
216 }
217 
218 if ($migrateCalendars) {
219 
220  $calendarBackup = 'calendars_3_1_' . $backupPostfix;
221  echo "Backing up 'calendars' to '", $calendarBackup, "'\n";
222 
223  switch ($driver) {
224  case 'mysql' :
225  $pdo->exec('RENAME TABLE calendars TO ' . $calendarBackup);
226  break;
227  case 'sqlite' :
228  $pdo->exec('ALTER TABLE calendars RENAME TO ' . $calendarBackup);
229  break;
230 
231  }
232 
233  echo "Creating new calendars table.\n";
234  switch ($driver) {
235  case 'mysql' :
236  $pdo->exec(<<<SQL
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;
242 SQL
243 );
244  break;
245  case 'sqlite' :
246  $pdo->exec(<<<SQL
247 CREATE TABLE calendars (
248  id integer primary key asc NOT NULL,
249  synctoken integer DEFAULT 1 NOT NULL,
250  components text NOT NULL
251 );
252 SQL
253  );
254  break;
255 
256  }
257 
258  echo "Migrating data from old to new table\n";
259 
260  $pdo->exec(<<<SQL
261 INSERT INTO calendars (id, synctoken, components) SELECT id, synctoken, COALESCE(components,"VEVENT,VTODO,VJOURNAL") as components FROM $calendarBackup
262 SQL
263  );
264 
265 }
266 
267 
268 echo "Upgrade to 3.2 schema completed.\n";
up()
Definition: up.php:2
back()
Definition: back.php:2