migrateto30.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env php
  2. <?php
  3. echo "SabreDAV migrate script for version 3.0\n";
  4. if ($argc < 2) {
  5. echo <<<HELLO
  6. This script help you migrate from a pre-3.0 database to 3.0 and later
  7. Changes:
  8. * The propertystorage table has changed to allow storage of complex
  9. properties.
  10. * the vcardurl field in the principals table is no more. This was moved to
  11. the propertystorage table.
  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 'propertystorage'\n";
  58. $addValueType = false;
  59. try {
  60. $result = $pdo->query('SELECT * FROM propertystorage LIMIT 1');
  61. $row = $result->fetch(\PDO::FETCH_ASSOC);
  62. if (!$row) {
  63. echo "No data in table. Going to re-create the table.\n";
  64. $random = mt_rand(1000, 9999);
  65. echo "Renaming propertystorage -> propertystorage_old$random and creating new table.\n";
  66. switch ($driver) {
  67. case 'mysql' :
  68. $pdo->exec('RENAME TABLE propertystorage TO propertystorage_old' . $random);
  69. $pdo->exec('
  70. CREATE TABLE propertystorage (
  71. id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
  72. path VARBINARY(1024) NOT NULL,
  73. name VARBINARY(100) NOT NULL,
  74. valuetype INT UNSIGNED,
  75. value MEDIUMBLOB
  76. );
  77. ');
  78. $pdo->exec('CREATE UNIQUE INDEX path_property_' . $random . ' ON propertystorage (path(600), name(100));');
  79. break;
  80. case 'sqlite' :
  81. $pdo->exec('ALTER TABLE propertystorage RENAME TO propertystorage_old' . $random);
  82. $pdo->exec('
  83. CREATE TABLE propertystorage (
  84. id integer primary key asc,
  85. path text,
  86. name text,
  87. valuetype integer,
  88. value blob
  89. );');
  90. $pdo->exec('CREATE UNIQUE INDEX path_property_' . $random . ' ON propertystorage (path, name);');
  91. break;
  92. }
  93. } elseif (array_key_exists('valuetype', $row)) {
  94. echo "valuetype field exists. Assuming that this part of the migration has\n";
  95. echo "Already been completed.\n";
  96. } else {
  97. echo "2.1 schema detected. Going to perform upgrade.\n";
  98. $addValueType = true;
  99. }
  100. } catch (Exception $e) {
  101. echo "Could not find a propertystorage table. Skipping this part of the\n";
  102. echo "upgrade.\n";
  103. echo $e->getMessage(), "\n";
  104. }
  105. if ($addValueType) {
  106. switch ($driver) {
  107. case 'mysql' :
  108. $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT UNSIGNED');
  109. break;
  110. case 'sqlite' :
  111. $pdo->exec('ALTER TABLE propertystorage ADD valuetype INT');
  112. break;
  113. }
  114. $pdo->exec('UPDATE propertystorage SET valuetype = 1 WHERE valuetype IS NULL ');
  115. }
  116. echo "Migrating vcardurl\n";
  117. $result = $pdo->query('SELECT id, uri, vcardurl FROM principals WHERE vcardurl IS NOT NULL');
  118. $stmt1 = $pdo->prepare('INSERT INTO propertystorage (path, name, valuetype, value) VALUES (?, ?, 3, ?)');
  119. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  120. // Inserting the new record
  121. $stmt1->execute([
  122. 'addressbooks/' . basename($row['uri']),
  123. '{http://calendarserver.org/ns/}me-card',
  124. serialize(new Sabre\DAV\Xml\Property\Href($row['vcardurl']))
  125. ]);
  126. echo serialize(new Sabre\DAV\Xml\Property\Href($row['vcardurl']));
  127. }
  128. echo "Done.\n";
  129. echo "Upgrade to 3.0 schema completed.\n";