You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\DB;
  27. use Doctrine\DBAL\DBALException;
  28. use Doctrine\DBAL\Schema\Schema;
  29. use Doctrine\DBAL\Types\BigIntType;
  30. use Doctrine\DBAL\Types\Type;
  31. class SQLiteMigrator extends Migrator {
  32. /**
  33. * @param \Doctrine\DBAL\Schema\Schema $targetSchema
  34. * @throws \OC\DB\MigrationException
  35. *
  36. * For sqlite we simple make a copy of the entire database, and test the migration on that
  37. */
  38. public function checkMigrate(\Doctrine\DBAL\Schema\Schema $targetSchema) {
  39. $dbFile = $this->connection->getDatabase();
  40. $tmpFile = $this->buildTempDatabase();
  41. copy($dbFile, $tmpFile);
  42. $connectionParams = [
  43. 'path' => $tmpFile,
  44. 'driver' => 'pdo_sqlite',
  45. ];
  46. $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
  47. try {
  48. $this->applySchema($targetSchema, $conn);
  49. $conn->close();
  50. unlink($tmpFile);
  51. } catch (DBALException $e) {
  52. $conn->close();
  53. unlink($tmpFile);
  54. throw new MigrationException('', $e->getMessage());
  55. }
  56. }
  57. /**
  58. * @return string
  59. */
  60. private function buildTempDatabase() {
  61. $dataDir = $this->config->getSystemValue("datadirectory", \OC::$SERVERROOT . '/data');
  62. $tmpFile = uniqid("oc_");
  63. return "$dataDir/$tmpFile.db";
  64. }
  65. /**
  66. * @param Schema $targetSchema
  67. * @param \Doctrine\DBAL\Connection $connection
  68. * @return \Doctrine\DBAL\Schema\SchemaDiff
  69. */
  70. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
  71. $platform = $connection->getDatabasePlatform();
  72. $platform->registerDoctrineTypeMapping('tinyint unsigned', 'integer');
  73. $platform->registerDoctrineTypeMapping('smallint unsigned', 'integer');
  74. $platform->registerDoctrineTypeMapping('varchar ', 'string');
  75. // with sqlite autoincrement columns is of type integer
  76. foreach ($targetSchema->getTables() as $table) {
  77. foreach ($table->getColumns() as $column) {
  78. if ($column->getType() instanceof BigIntType && $column->getAutoincrement()) {
  79. $column->setType(Type::getType('integer'));
  80. }
  81. }
  82. }
  83. return parent::getDiff($targetSchema, $connection);
  84. }
  85. }