MDB2SchemaManager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\DB;
  30. use Doctrine\DBAL\Platforms\MySqlPlatform;
  31. use Doctrine\DBAL\Platforms\OraclePlatform;
  32. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  33. use Doctrine\DBAL\Platforms\SqlitePlatform;
  34. use Doctrine\DBAL\Schema\Schema;
  35. use OCP\IDBConnection;
  36. class MDB2SchemaManager {
  37. /** @var \OC\DB\Connection $conn */
  38. protected $conn;
  39. /**
  40. * @param IDBConnection $conn
  41. */
  42. public function __construct($conn) {
  43. $this->conn = $conn;
  44. }
  45. /**
  46. * saves database scheme to xml file
  47. * @param string $file name of file
  48. * @return bool
  49. *
  50. * TODO: write more documentation
  51. */
  52. public function getDbStructure($file) {
  53. return \OC\DB\MDB2SchemaWriter::saveSchemaToFile($file, $this->conn);
  54. }
  55. /**
  56. * Creates tables from XML file
  57. * @param string $file file to read structure from
  58. * @return bool
  59. *
  60. * TODO: write more documentation
  61. */
  62. public function createDbFromStructure($file) {
  63. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform());
  64. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  65. $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema);
  66. return $this->executeSchemaChange($toSchema);
  67. }
  68. /**
  69. * @return \OC\DB\Migrator
  70. */
  71. public function getMigrator() {
  72. $random = \OC::$server->getSecureRandom();
  73. $platform = $this->conn->getDatabasePlatform();
  74. $config = \OC::$server->getConfig();
  75. $dispatcher = \OC::$server->getEventDispatcher();
  76. if ($platform instanceof SqlitePlatform) {
  77. return new SQLiteMigrator($this->conn, $random, $config, $dispatcher);
  78. } else if ($platform instanceof OraclePlatform) {
  79. return new OracleMigrator($this->conn, $random, $config, $dispatcher);
  80. } else if ($platform instanceof MySqlPlatform) {
  81. return new MySQLMigrator($this->conn, $random, $config, $dispatcher);
  82. } else if ($platform instanceof PostgreSqlPlatform) {
  83. return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher);
  84. } else {
  85. return new NoCheckMigrator($this->conn, $random, $config, $dispatcher);
  86. }
  87. }
  88. /**
  89. * Reads database schema from file
  90. *
  91. * @param string $file file to read from
  92. * @return \Doctrine\DBAL\Schema\Schema
  93. */
  94. private function readSchemaFromFile($file) {
  95. $platform = $this->conn->getDatabasePlatform();
  96. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform);
  97. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  98. return $schemaReader->loadSchemaFromFile($file, $toSchema);
  99. }
  100. /**
  101. * update the database scheme
  102. * @param string $file file to read structure from
  103. * @param bool $generateSql only return the sql needed for the upgrade
  104. * @return string|boolean
  105. */
  106. public function updateDbFromStructure($file, $generateSql = false) {
  107. $toSchema = $this->readSchemaFromFile($file);
  108. $migrator = $this->getMigrator();
  109. if ($generateSql) {
  110. return $migrator->generateChangeScript($toSchema);
  111. } else {
  112. $migrator->migrate($toSchema);
  113. return true;
  114. }
  115. }
  116. /**
  117. * @param \Doctrine\DBAL\Schema\Schema $schema
  118. * @return string
  119. */
  120. public function generateChangeScript($schema) {
  121. $migrator = $this->getMigrator();
  122. return $migrator->generateChangeScript($schema);
  123. }
  124. /**
  125. * remove all tables defined in a database structure xml file
  126. *
  127. * @param string $file the xml file describing the tables
  128. */
  129. public function removeDBStructure($file) {
  130. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform());
  131. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  132. $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema);
  133. $toSchema = clone $fromSchema;
  134. /** @var $table \Doctrine\DBAL\Schema\Table */
  135. foreach ($toSchema->getTables() as $table) {
  136. $toSchema->dropTable($table->getName());
  137. }
  138. $comparator = new \Doctrine\DBAL\Schema\Comparator();
  139. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  140. $this->executeSchemaChange($schemaDiff);
  141. }
  142. /**
  143. * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema
  144. * @return bool
  145. */
  146. private function executeSchemaChange($schema) {
  147. $this->conn->beginTransaction();
  148. foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) {
  149. $this->conn->query($sql);
  150. }
  151. $this->conn->commit();
  152. if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) {
  153. $this->conn->close();
  154. $this->conn->connect();
  155. }
  156. return true;
  157. }
  158. }