Migrator.php 5.5 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\Comparator;
  34. use Doctrine\DBAL\Schema\Schema;
  35. use Doctrine\DBAL\Schema\SchemaDiff;
  36. use Doctrine\DBAL\Types\StringType;
  37. use Doctrine\DBAL\Types\Type;
  38. use OCP\IConfig;
  39. use function preg_match;
  40. use OCP\EventDispatcher\IEventDispatcher;
  41. class Migrator {
  42. /** @var Connection */
  43. protected $connection;
  44. /** @var IConfig */
  45. protected $config;
  46. private ?IEventDispatcher $dispatcher;
  47. /** @var bool */
  48. private $noEmit = false;
  49. public function __construct(Connection $connection,
  50. IConfig $config,
  51. ?IEventDispatcher $dispatcher = null) {
  52. $this->connection = $connection;
  53. $this->config = $config;
  54. $this->dispatcher = $dispatcher;
  55. }
  56. /**
  57. * @throws Exception
  58. */
  59. public function migrate(Schema $targetSchema) {
  60. $this->noEmit = true;
  61. $this->applySchema($targetSchema);
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function generateChangeScript(Schema $targetSchema) {
  67. $schemaDiff = $this->getDiff($targetSchema, $this->connection);
  68. $script = '';
  69. $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform());
  70. foreach ($sqls as $sql) {
  71. $script .= $this->convertStatementToScript($sql);
  72. }
  73. return $script;
  74. }
  75. /**
  76. * @throws Exception
  77. */
  78. public function createSchema() {
  79. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  80. /** @var string|AbstractAsset $asset */
  81. $filterExpression = $this->getFilterExpression();
  82. if ($asset instanceof AbstractAsset) {
  83. return preg_match($filterExpression, $asset->getName()) === 1;
  84. }
  85. return preg_match($filterExpression, $asset) === 1;
  86. });
  87. return $this->connection->getSchemaManager()->createSchema();
  88. }
  89. /**
  90. * @return SchemaDiff
  91. */
  92. protected function getDiff(Schema $targetSchema, Connection $connection) {
  93. // adjust varchar columns with a length higher then getVarcharMaxLength to clob
  94. foreach ($targetSchema->getTables() as $table) {
  95. foreach ($table->getColumns() as $column) {
  96. if ($column->getType() instanceof StringType) {
  97. if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) {
  98. $column->setType(Type::getType('text'));
  99. $column->setLength(null);
  100. }
  101. }
  102. }
  103. }
  104. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  105. /** @var string|AbstractAsset $asset */
  106. $filterExpression = $this->getFilterExpression();
  107. if ($asset instanceof AbstractAsset) {
  108. return preg_match($filterExpression, $asset->getName()) === 1;
  109. }
  110. return preg_match($filterExpression, $asset) === 1;
  111. });
  112. $sourceSchema = $connection->getSchemaManager()->createSchema();
  113. // remove tables we don't know about
  114. foreach ($sourceSchema->getTables() as $table) {
  115. if (!$targetSchema->hasTable($table->getName())) {
  116. $sourceSchema->dropTable($table->getName());
  117. }
  118. }
  119. // remove sequences we don't know about
  120. foreach ($sourceSchema->getSequences() as $table) {
  121. if (!$targetSchema->hasSequence($table->getName())) {
  122. $sourceSchema->dropSequence($table->getName());
  123. }
  124. }
  125. /** @psalm-suppress InternalMethod */
  126. $comparator = new Comparator();
  127. return $comparator->compare($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 = $schemaDiff->toSql($connection->getDatabasePlatform());
  141. $step = 0;
  142. foreach ($sqls as $sql) {
  143. $this->emit($sql, $step++, count($sqls));
  144. $connection->query($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. }