1
0

MDB2SchemaManager.php 5.5 KB

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