Migrator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  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. *
  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\Connection;
  30. use Doctrine\DBAL\Exception;
  31. use Doctrine\DBAL\Platforms\MySQLPlatform;
  32. use Doctrine\DBAL\Schema\AbstractAsset;
  33. use Doctrine\DBAL\Schema\Schema;
  34. use Doctrine\DBAL\Schema\SchemaDiff;
  35. use Doctrine\DBAL\Types\StringType;
  36. use Doctrine\DBAL\Types\Type;
  37. use OCP\EventDispatcher\IEventDispatcher;
  38. use OCP\IConfig;
  39. use function preg_match;
  40. class Migrator {
  41. /** @var Connection */
  42. protected $connection;
  43. /** @var IConfig */
  44. protected $config;
  45. private ?IEventDispatcher $dispatcher;
  46. /** @var bool */
  47. private $noEmit = false;
  48. public function __construct(Connection $connection,
  49. IConfig $config,
  50. ?IEventDispatcher $dispatcher = null) {
  51. $this->connection = $connection;
  52. $this->config = $config;
  53. $this->dispatcher = $dispatcher;
  54. }
  55. /**
  56. * @throws Exception
  57. */
  58. public function migrate(Schema $targetSchema) {
  59. $this->noEmit = true;
  60. $this->applySchema($targetSchema);
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function generateChangeScript(Schema $targetSchema) {
  66. $schemaDiff = $this->getDiff($targetSchema, $this->connection);
  67. $script = '';
  68. $sqls = $this->connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
  69. foreach ($sqls as $sql) {
  70. $script .= $this->convertStatementToScript($sql);
  71. }
  72. return $script;
  73. }
  74. /**
  75. * @throws Exception
  76. */
  77. public function createSchema() {
  78. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  79. /** @var string|AbstractAsset $asset */
  80. $filterExpression = $this->getFilterExpression();
  81. if ($asset instanceof AbstractAsset) {
  82. return preg_match($filterExpression, $asset->getName()) === 1;
  83. }
  84. return preg_match($filterExpression, $asset) === 1;
  85. });
  86. return $this->connection->createSchemaManager()->introspectSchema();
  87. }
  88. /**
  89. * @return SchemaDiff
  90. */
  91. protected function getDiff(Schema $targetSchema, Connection $connection) {
  92. // Adjust STRING columns with a length higher than 4000 to TEXT (clob)
  93. // for consistency between the supported databases and
  94. // old vs. new installations.
  95. foreach ($targetSchema->getTables() as $table) {
  96. foreach ($table->getColumns() as $column) {
  97. if ($column->getType() instanceof StringType) {
  98. if ($column->getLength() > 4000) {
  99. $column->setType(Type::getType('text'));
  100. $column->setLength(null);
  101. }
  102. }
  103. }
  104. }
  105. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  106. /** @var string|AbstractAsset $asset */
  107. $filterExpression = $this->getFilterExpression();
  108. if ($asset instanceof AbstractAsset) {
  109. return preg_match($filterExpression, $asset->getName()) === 1;
  110. }
  111. return preg_match($filterExpression, $asset) === 1;
  112. });
  113. $sourceSchema = $connection->createSchemaManager()->introspectSchema();
  114. // remove tables we don't know about
  115. foreach ($sourceSchema->getTables() as $table) {
  116. if (!$targetSchema->hasTable($table->getName())) {
  117. $sourceSchema->dropTable($table->getName());
  118. }
  119. }
  120. // remove sequences we don't know about
  121. foreach ($sourceSchema->getSequences() as $table) {
  122. if (!$targetSchema->hasSequence($table->getName())) {
  123. $sourceSchema->dropSequence($table->getName());
  124. }
  125. }
  126. $comparator = $connection->createSchemaManager()->createComparator();
  127. return $comparator->compareSchemas($sourceSchema, $targetSchema);
  128. }
  129. /**
  130. * @throws Exception
  131. */
  132. protected function applySchema(Schema $targetSchema, ?Connection $connection = null) {
  133. if (is_null($connection)) {
  134. $connection = $this->connection;
  135. }
  136. $schemaDiff = $this->getDiff($targetSchema, $connection);
  137. if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
  138. $connection->beginTransaction();
  139. }
  140. $sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
  141. $step = 0;
  142. foreach ($sqls as $sql) {
  143. $this->emit($sql, $step++, count($sqls));
  144. $connection->executeStatement($sql);
  145. }
  146. if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
  147. $connection->commit();
  148. }
  149. }
  150. /**
  151. * @param $statement
  152. * @return string
  153. */
  154. protected function convertStatementToScript($statement) {
  155. $script = $statement . ';';
  156. $script .= PHP_EOL;
  157. $script .= PHP_EOL;
  158. return $script;
  159. }
  160. protected function getFilterExpression() {
  161. return '/^' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_'), '/') . '/';
  162. }
  163. protected function emit(string $sql, int $step, int $max): void {
  164. if ($this->noEmit) {
  165. return;
  166. }
  167. if (is_null($this->dispatcher)) {
  168. return;
  169. }
  170. $this->dispatcher->dispatchTyped(new MigratorExecuteSqlEvent($sql, $step, $max));
  171. }
  172. }