mdb2schemamanager.php 5.4 KB

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