SQLiteMigrator.php 2.9 KB

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