Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MDB2SchemaManager.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <vincent@nextcloud.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\PostgreSQL94Platform;
  35. use Doctrine\DBAL\Platforms\SqlitePlatform;
  36. use Doctrine\DBAL\Schema\Schema;
  37. class MDB2SchemaManager {
  38. /** @var Connection $conn */
  39. protected $conn;
  40. /**
  41. * @param Connection $conn
  42. */
  43. public function __construct($conn) {
  44. $this->conn = $conn;
  45. }
  46. /**
  47. * Creates tables from XML file
  48. * @param string $file file to read structure from
  49. * @return bool
  50. *
  51. * TODO: write more documentation
  52. */
  53. public function createDbFromStructure($file) {
  54. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform());
  55. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  56. $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema);
  57. return $this->executeSchemaChange($toSchema);
  58. }
  59. /**
  60. * @return \OC\DB\Migrator
  61. */
  62. public function getMigrator() {
  63. $random = \OC::$server->getSecureRandom();
  64. $platform = $this->conn->getDatabasePlatform();
  65. $config = \OC::$server->getConfig();
  66. $dispatcher = \OC::$server->getEventDispatcher();
  67. if ($platform instanceof SqlitePlatform) {
  68. return new SQLiteMigrator($this->conn, $random, $config, $dispatcher);
  69. } elseif ($platform instanceof OraclePlatform) {
  70. return new OracleMigrator($this->conn, $random, $config, $dispatcher);
  71. } elseif ($platform instanceof MySQLPlatform) {
  72. return new MySQLMigrator($this->conn, $random, $config, $dispatcher);
  73. } elseif ($platform instanceof PostgreSQL94Platform) {
  74. return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher);
  75. } else {
  76. return new Migrator($this->conn, $random, $config, $dispatcher);
  77. }
  78. }
  79. /**
  80. * Reads database schema from file
  81. *
  82. * @param string $file file to read from
  83. * @return \Doctrine\DBAL\Schema\Schema
  84. */
  85. private function readSchemaFromFile($file) {
  86. $platform = $this->conn->getDatabasePlatform();
  87. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform);
  88. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  89. return $schemaReader->loadSchemaFromFile($file, $toSchema);
  90. }
  91. /**
  92. * update the database scheme
  93. * @param string $file file to read structure from
  94. * @param bool $generateSql only return the sql needed for the upgrade
  95. * @return string|boolean
  96. */
  97. public function updateDbFromStructure($file, $generateSql = false) {
  98. $toSchema = $this->readSchemaFromFile($file);
  99. $migrator = $this->getMigrator();
  100. if ($generateSql) {
  101. return $migrator->generateChangeScript($toSchema);
  102. } else {
  103. $migrator->migrate($toSchema);
  104. return true;
  105. }
  106. }
  107. /**
  108. * @param \Doctrine\DBAL\Schema\Schema $schema
  109. * @return string
  110. */
  111. public function generateChangeScript($schema) {
  112. $migrator = $this->getMigrator();
  113. return $migrator->generateChangeScript($schema);
  114. }
  115. /**
  116. * remove all tables defined in a database structure xml file
  117. *
  118. * @param string $file the xml file describing the tables
  119. */
  120. public function removeDBStructure($file) {
  121. $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform());
  122. $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig());
  123. $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema);
  124. $toSchema = clone $fromSchema;
  125. foreach ($toSchema->getTables() as $table) {
  126. $toSchema->dropTable($table->getName());
  127. }
  128. $comparator = new \Doctrine\DBAL\Schema\Comparator();
  129. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  130. $this->executeSchemaChange($schemaDiff);
  131. }
  132. /**
  133. * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema
  134. * @return bool
  135. */
  136. private function executeSchemaChange($schema) {
  137. $this->conn->beginTransaction();
  138. foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) {
  139. $this->conn->query($sql);
  140. }
  141. $this->conn->commit();
  142. if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) {
  143. $this->conn->close();
  144. $this->conn->connect();
  145. }
  146. return true;
  147. }
  148. }