migrateto20.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #!/usr/bin/env php
  2. <?php
  3. echo "SabreDAV migrate script for version 2.0\n";
  4. if ($argc < 2) {
  5. echo <<<HELLO
  6. This script help you migrate from a pre-2.0 database to 2.0 and later
  7. The 'calendars', 'addressbooks' and 'cards' tables will be upgraded, and new
  8. tables (calendarchanges, addressbookchanges, propertystorage) will be added.
  9. If you don't use the default PDO CalDAV or CardDAV backend, it's pointless to
  10. run this script.
  11. Keep in mind that ALTER TABLE commands will be executed. If you have a large
  12. dataset this may mean that this process takes a while.
  13. Lastly: Make a back-up first. This script has been tested, but the amount of
  14. potential variants are extremely high, so it's impossible to deal with every
  15. possible situation.
  16. In the worst case, you will lose all your data. This is not an overstatement.
  17. Usage:
  18. php {$argv[0]} [pdo-dsn] [username] [password]
  19. For example:
  20. php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password
  21. php {$argv[0]} sqlite:data/sabredav.db
  22. HELLO;
  23. exit();
  24. }
  25. // There's a bunch of places where the autoloader could be, so we'll try all of
  26. // them.
  27. $paths = [
  28. __DIR__ . '/../vendor/autoload.php',
  29. __DIR__ . '/../../../autoload.php',
  30. ];
  31. foreach ($paths as $path) {
  32. if (file_exists($path)) {
  33. include $path;
  34. break;
  35. }
  36. }
  37. $dsn = $argv[1];
  38. $user = isset($argv[2]) ? $argv[2] : null;
  39. $pass = isset($argv[3]) ? $argv[3] : null;
  40. echo "Connecting to database: " . $dsn . "\n";
  41. $pdo = new PDO($dsn, $user, $pass);
  42. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  43. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  44. $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  45. switch ($driver) {
  46. case 'mysql' :
  47. echo "Detected MySQL.\n";
  48. break;
  49. case 'sqlite' :
  50. echo "Detected SQLite.\n";
  51. break;
  52. default :
  53. echo "Error: unsupported driver: " . $driver . "\n";
  54. die(-1);
  55. }
  56. foreach (['calendar', 'addressbook'] as $itemType) {
  57. $tableName = $itemType . 's';
  58. $tableNameOld = $tableName . '_old';
  59. $changesTable = $itemType . 'changes';
  60. echo "Upgrading '$tableName'\n";
  61. // The only cross-db way to do this, is to just fetch a single record.
  62. $row = $pdo->query("SELECT * FROM $tableName LIMIT 1")->fetch();
  63. if (!$row) {
  64. echo "No records were found in the '$tableName' table.\n";
  65. echo "\n";
  66. echo "We're going to rename the old table to $tableNameOld (just in case).\n";
  67. echo "and re-create the new table.\n";
  68. switch ($driver) {
  69. case 'mysql' :
  70. $pdo->exec("RENAME TABLE $tableName TO $tableNameOld");
  71. switch ($itemType) {
  72. case 'calendar' :
  73. $pdo->exec("
  74. CREATE TABLE calendars (
  75. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  76. principaluri VARCHAR(100),
  77. displayname VARCHAR(100),
  78. uri VARCHAR(200),
  79. synctoken INT(11) UNSIGNED NOT NULL DEFAULT '1',
  80. description TEXT,
  81. calendarorder INT(11) UNSIGNED NOT NULL DEFAULT '0',
  82. calendarcolor VARCHAR(10),
  83. timezone TEXT,
  84. components VARCHAR(20),
  85. transparent TINYINT(1) NOT NULL DEFAULT '0',
  86. UNIQUE(principaluri, uri)
  87. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  88. ");
  89. break;
  90. case 'addressbook' :
  91. $pdo->exec("
  92. CREATE TABLE addressbooks (
  93. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  94. principaluri VARCHAR(255),
  95. displayname VARCHAR(255),
  96. uri VARCHAR(200),
  97. description TEXT,
  98. synctoken INT(11) UNSIGNED NOT NULL DEFAULT '1',
  99. UNIQUE(principaluri, uri)
  100. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  101. ");
  102. break;
  103. }
  104. break;
  105. case 'sqlite' :
  106. $pdo->exec("ALTER TABLE $tableName RENAME TO $tableNameOld");
  107. switch ($itemType) {
  108. case 'calendar' :
  109. $pdo->exec("
  110. CREATE TABLE calendars (
  111. id integer primary key asc,
  112. principaluri text,
  113. displayname text,
  114. uri text,
  115. synctoken integer,
  116. description text,
  117. calendarorder integer,
  118. calendarcolor text,
  119. timezone text,
  120. components text,
  121. transparent bool
  122. );
  123. ");
  124. break;
  125. case 'addressbook' :
  126. $pdo->exec("
  127. CREATE TABLE addressbooks (
  128. id integer primary key asc,
  129. principaluri text,
  130. displayname text,
  131. uri text,
  132. description text,
  133. synctoken integer
  134. );
  135. ");
  136. break;
  137. }
  138. break;
  139. }
  140. echo "Creation of 2.0 $tableName table is complete\n";
  141. } else {
  142. // Checking if there's a synctoken field already.
  143. if (array_key_exists('synctoken', $row)) {
  144. echo "The 'synctoken' field already exists in the $tableName table.\n";
  145. echo "It's likely you already upgraded, so we're simply leaving\n";
  146. echo "the $tableName table alone\n";
  147. } else {
  148. echo "1.8 table schema detected\n";
  149. switch ($driver) {
  150. case 'mysql' :
  151. $pdo->exec("ALTER TABLE $tableName ADD synctoken INT(11) UNSIGNED NOT NULL DEFAULT '1'");
  152. $pdo->exec("ALTER TABLE $tableName DROP ctag");
  153. $pdo->exec("UPDATE $tableName SET synctoken = '1'");
  154. break;
  155. case 'sqlite' :
  156. $pdo->exec("ALTER TABLE $tableName ADD synctoken integer");
  157. $pdo->exec("UPDATE $tableName SET synctoken = '1'");
  158. echo "Note: there's no easy way to remove fields in sqlite.\n";
  159. echo "The ctag field is no longer used, but it's kept in place\n";
  160. break;
  161. }
  162. echo "Upgraded '$tableName' to 2.0 schema.\n";
  163. }
  164. }
  165. try {
  166. $pdo->query("SELECT * FROM $changesTable LIMIT 1");
  167. echo "'$changesTable' already exists. Assuming that this part of the\n";
  168. echo "upgrade was already completed.\n";
  169. } catch (Exception $e) {
  170. echo "Creating '$changesTable' table.\n";
  171. switch ($driver) {
  172. case 'mysql' :
  173. $pdo->exec("
  174. CREATE TABLE $changesTable (
  175. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  176. uri VARCHAR(200) NOT NULL,
  177. synctoken INT(11) UNSIGNED NOT NULL,
  178. {$itemType}id INT(11) UNSIGNED NOT NULL,
  179. operation TINYINT(1) NOT NULL,
  180. INDEX {$itemType}id_synctoken ({$itemType}id, synctoken)
  181. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  182. ");
  183. break;
  184. case 'sqlite' :
  185. $pdo->exec("
  186. CREATE TABLE $changesTable (
  187. id integer primary key asc,
  188. uri text,
  189. synctoken integer,
  190. {$itemType}id integer,
  191. operation bool
  192. );
  193. ");
  194. $pdo->exec("CREATE INDEX {$itemType}id_synctoken ON $changesTable ({$itemType}id, synctoken);");
  195. break;
  196. }
  197. }
  198. }
  199. try {
  200. $pdo->query("SELECT * FROM calendarsubscriptions LIMIT 1");
  201. echo "'calendarsubscriptions' already exists. Assuming that this part of the\n";
  202. echo "upgrade was already completed.\n";
  203. } catch (Exception $e) {
  204. echo "Creating calendarsubscriptions table.\n";
  205. switch ($driver) {
  206. case 'mysql' :
  207. $pdo->exec("
  208. CREATE TABLE calendarsubscriptions (
  209. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  210. uri VARCHAR(200) NOT NULL,
  211. principaluri VARCHAR(100) NOT NULL,
  212. source TEXT,
  213. displayname VARCHAR(100),
  214. refreshrate VARCHAR(10),
  215. calendarorder INT(11) UNSIGNED NOT NULL DEFAULT '0',
  216. calendarcolor VARCHAR(10),
  217. striptodos TINYINT(1) NULL,
  218. stripalarms TINYINT(1) NULL,
  219. stripattachments TINYINT(1) NULL,
  220. lastmodified INT(11) UNSIGNED,
  221. UNIQUE(principaluri, uri)
  222. );
  223. ");
  224. break;
  225. case 'sqlite' :
  226. $pdo->exec("
  227. CREATE TABLE calendarsubscriptions (
  228. id integer primary key asc,
  229. uri text,
  230. principaluri text,
  231. source text,
  232. displayname text,
  233. refreshrate text,
  234. calendarorder integer,
  235. calendarcolor text,
  236. striptodos bool,
  237. stripalarms bool,
  238. stripattachments bool,
  239. lastmodified int
  240. );
  241. ");
  242. $pdo->exec("CREATE INDEX principaluri_uri ON calendarsubscriptions (principaluri, uri);");
  243. break;
  244. }
  245. }
  246. try {
  247. $pdo->query("SELECT * FROM propertystorage LIMIT 1");
  248. echo "'propertystorage' already exists. Assuming that this part of the\n";
  249. echo "upgrade was already completed.\n";
  250. } catch (Exception $e) {
  251. echo "Creating propertystorage table.\n";
  252. switch ($driver) {
  253. case 'mysql' :
  254. $pdo->exec("
  255. CREATE TABLE propertystorage (
  256. id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  257. path VARBINARY(1024) NOT NULL,
  258. name VARBINARY(100) NOT NULL,
  259. value MEDIUMBLOB
  260. );
  261. ");
  262. $pdo->exec("
  263. CREATE UNIQUE INDEX path_property ON propertystorage (path(600), name(100));
  264. ");
  265. break;
  266. case 'sqlite' :
  267. $pdo->exec("
  268. CREATE TABLE propertystorage (
  269. id integer primary key asc,
  270. path TEXT,
  271. name TEXT,
  272. value TEXT
  273. );
  274. ");
  275. $pdo->exec("
  276. CREATE UNIQUE INDEX path_property ON propertystorage (path, name);
  277. ");
  278. break;
  279. }
  280. }
  281. echo "Upgrading cards table to 2.0 schema\n";
  282. try {
  283. $create = false;
  284. $row = $pdo->query("SELECT * FROM cards LIMIT 1")->fetch();
  285. if (!$row) {
  286. $random = mt_rand(1000, 9999);
  287. echo "There was no data in the cards table, so we're re-creating it\n";
  288. echo "The old table will be renamed to cards_old$random, just in case.\n";
  289. $create = true;
  290. switch ($driver) {
  291. case 'mysql' :
  292. $pdo->exec("RENAME TABLE cards TO cards_old$random");
  293. break;
  294. case 'sqlite' :
  295. $pdo->exec("ALTER TABLE cards RENAME TO cards_old$random");
  296. break;
  297. }
  298. }
  299. } catch (Exception $e) {
  300. echo "Exception while checking cards table. Assuming that the table does not yet exist.\n";
  301. echo "Debug: ", $e->getMessage(), "\n";
  302. $create = true;
  303. }
  304. if ($create) {
  305. switch ($driver) {
  306. case 'mysql' :
  307. $pdo->exec("
  308. CREATE TABLE cards (
  309. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  310. addressbookid INT(11) UNSIGNED NOT NULL,
  311. carddata MEDIUMBLOB,
  312. uri VARCHAR(200),
  313. lastmodified INT(11) UNSIGNED,
  314. etag VARBINARY(32),
  315. size INT(11) UNSIGNED NOT NULL
  316. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  317. ");
  318. break;
  319. case 'sqlite' :
  320. $pdo->exec("
  321. CREATE TABLE cards (
  322. id integer primary key asc,
  323. addressbookid integer,
  324. carddata blob,
  325. uri text,
  326. lastmodified integer,
  327. etag text,
  328. size integer
  329. );
  330. ");
  331. break;
  332. }
  333. } else {
  334. switch ($driver) {
  335. case 'mysql' :
  336. $pdo->exec("
  337. ALTER TABLE cards
  338. ADD etag VARBINARY(32),
  339. ADD size INT(11) UNSIGNED NOT NULL;
  340. ");
  341. break;
  342. case 'sqlite' :
  343. $pdo->exec("
  344. ALTER TABLE cards ADD etag text;
  345. ALTER TABLE cards ADD size integer;
  346. ");
  347. break;
  348. }
  349. echo "Reading all old vcards and populating etag and size fields.\n";
  350. $result = $pdo->query('SELECT id, carddata FROM cards');
  351. $stmt = $pdo->prepare('UPDATE cards SET etag = ?, size = ? WHERE id = ?');
  352. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  353. $stmt->execute([
  354. md5($row['carddata']),
  355. strlen($row['carddata']),
  356. $row['id']
  357. ]);
  358. }
  359. }
  360. echo "Upgrade to 2.0 schema completed.\n";