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.

173 lines
5.5 KiB

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