migrateto21.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env php
  2. <?php
  3. echo "SabreDAV migrate script for version 2.1\n";
  4. if ($argc < 2) {
  5. echo <<<HELLO
  6. This script help you migrate from a pre-2.1 database to 2.1.
  7. Changes:
  8. The 'calendarobjects' table will be upgraded.
  9. 'schedulingobjects' will be created.
  10. If you don't use the default PDO CalDAV or CardDAV backend, it's pointless to
  11. run this script.
  12. Keep in mind that ALTER TABLE commands will be executed. If you have a large
  13. dataset this may mean that this process takes a while.
  14. Lastly: Make a back-up first. This script has been tested, but the amount of
  15. potential variants are extremely high, so it's impossible to deal with every
  16. possible situation.
  17. In the worst case, you will lose all your data. This is not an overstatement.
  18. Usage:
  19. php {$argv[0]} [pdo-dsn] [username] [password]
  20. For example:
  21. php {$argv[0]} "mysql:host=localhost;dbname=sabredav" root password
  22. php {$argv[0]} sqlite:data/sabredav.db
  23. HELLO;
  24. exit();
  25. }
  26. // There's a bunch of places where the autoloader could be, so we'll try all of
  27. // them.
  28. $paths = [
  29. __DIR__ . '/../vendor/autoload.php',
  30. __DIR__ . '/../../../autoload.php',
  31. ];
  32. foreach ($paths as $path) {
  33. if (file_exists($path)) {
  34. include $path;
  35. break;
  36. }
  37. }
  38. $dsn = $argv[1];
  39. $user = isset($argv[2]) ? $argv[2] : null;
  40. $pass = isset($argv[3]) ? $argv[3] : null;
  41. echo "Connecting to database: " . $dsn . "\n";
  42. $pdo = new PDO($dsn, $user, $pass);
  43. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  44. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  45. $driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  46. switch ($driver) {
  47. case 'mysql' :
  48. echo "Detected MySQL.\n";
  49. break;
  50. case 'sqlite' :
  51. echo "Detected SQLite.\n";
  52. break;
  53. default :
  54. echo "Error: unsupported driver: " . $driver . "\n";
  55. die(-1);
  56. }
  57. echo "Upgrading 'calendarobjects'\n";
  58. $addUid = false;
  59. try {
  60. $result = $pdo->query('SELECT * FROM calendarobjects LIMIT 1');
  61. $row = $result->fetch(\PDO::FETCH_ASSOC);
  62. if (!$row) {
  63. echo "No data in table. Going to try to add the uid field anyway.\n";
  64. $addUid = true;
  65. } elseif (array_key_exists('uid', $row)) {
  66. echo "uid field exists. Assuming that this part of the migration has\n";
  67. echo "Already been completed.\n";
  68. } else {
  69. echo "2.0 schema detected.\n";
  70. $addUid = true;
  71. }
  72. } catch (Exception $e) {
  73. echo "Could not find a calendarobjects table. Skipping this part of the\n";
  74. echo "upgrade.\n";
  75. }
  76. if ($addUid) {
  77. switch ($driver) {
  78. case 'mysql' :
  79. $pdo->exec('ALTER TABLE calendarobjects ADD uid VARCHAR(200)');
  80. break;
  81. case 'sqlite' :
  82. $pdo->exec('ALTER TABLE calendarobjects ADD uid TEXT');
  83. break;
  84. }
  85. $result = $pdo->query('SELECT id, calendardata FROM calendarobjects');
  86. $stmt = $pdo->prepare('UPDATE calendarobjects SET uid = ? WHERE id = ?');
  87. $counter = 0;
  88. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  89. try {
  90. $vobj = \Sabre\VObject\Reader::read($row['calendardata']);
  91. } catch (\Exception $e) {
  92. echo "Warning! Item with id $row[id] could not be parsed!\n";
  93. continue;
  94. }
  95. $uid = null;
  96. $item = $vobj->getBaseComponent();
  97. if (!isset($item->UID)) {
  98. echo "Warning! Item with id $item[id] does NOT have a UID property and this is required.\n";
  99. continue;
  100. }
  101. $uid = (string)$item->UID;
  102. $stmt->execute([$uid, $row['id']]);
  103. $counter++;
  104. }
  105. }
  106. echo "Creating 'schedulingobjects'\n";
  107. switch ($driver) {
  108. case 'mysql' :
  109. $pdo->exec('CREATE TABLE IF NOT EXISTS schedulingobjects
  110. (
  111. id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  112. principaluri VARCHAR(255),
  113. calendardata MEDIUMBLOB,
  114. uri VARCHAR(200),
  115. lastmodified INT(11) UNSIGNED,
  116. etag VARCHAR(32),
  117. size INT(11) UNSIGNED NOT NULL
  118. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  119. ');
  120. break;
  121. case 'sqlite' :
  122. $pdo->exec('CREATE TABLE IF NOT EXISTS schedulingobjects (
  123. id integer primary key asc,
  124. principaluri text,
  125. calendardata blob,
  126. uri text,
  127. lastmodified integer,
  128. etag text,
  129. size integer
  130. )
  131. ');
  132. break;
  133. $pdo->exec('
  134. CREATE INDEX principaluri_uri ON calendarsubscriptions (principaluri, uri);
  135. ');
  136. break;
  137. }
  138. echo "Done.\n";
  139. echo "Upgrade to 2.1 schema completed.\n";