sqlitemigrator.php 2.5 KB

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