Migrator.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Exception;
  30. use Doctrine\DBAL\Platforms\MySQLPlatform;
  31. use Doctrine\DBAL\Schema\AbstractAsset;
  32. use Doctrine\DBAL\Schema\Comparator;
  33. use Doctrine\DBAL\Schema\Schema;
  34. use Doctrine\DBAL\Types\StringType;
  35. use Doctrine\DBAL\Types\Type;
  36. use OCP\IConfig;
  37. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  38. use Symfony\Component\EventDispatcher\GenericEvent;
  39. use function preg_match;
  40. class Migrator {
  41. /** @var \Doctrine\DBAL\Connection */
  42. protected $connection;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var EventDispatcherInterface */
  46. private $dispatcher;
  47. /** @var bool */
  48. private $noEmit = false;
  49. /**
  50. * @param \Doctrine\DBAL\Connection $connection
  51. * @param IConfig $config
  52. * @param EventDispatcherInterface $dispatcher
  53. */
  54. public function __construct(\Doctrine\DBAL\Connection $connection,
  55. IConfig $config,
  56. EventDispatcherInterface $dispatcher = null) {
  57. $this->connection = $connection;
  58. $this->config = $config;
  59. $this->dispatcher = $dispatcher;
  60. }
  61. /**
  62. * @param \Doctrine\DBAL\Schema\Schema $targetSchema
  63. *
  64. * @throws Exception
  65. */
  66. public function migrate(Schema $targetSchema) {
  67. $this->noEmit = true;
  68. $this->applySchema($targetSchema);
  69. }
  70. /**
  71. * @param \Doctrine\DBAL\Schema\Schema $targetSchema
  72. * @return string
  73. */
  74. public function generateChangeScript(Schema $targetSchema) {
  75. $schemaDiff = $this->getDiff($targetSchema, $this->connection);
  76. $script = '';
  77. $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform());
  78. foreach ($sqls as $sql) {
  79. $script .= $this->convertStatementToScript($sql);
  80. }
  81. return $script;
  82. }
  83. /**
  84. * @throws Exception
  85. */
  86. public function createSchema() {
  87. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  88. /** @var string|AbstractAsset $asset */
  89. $filterExpression = $this->getFilterExpression();
  90. if ($asset instanceof AbstractAsset) {
  91. return preg_match($filterExpression, $asset->getName()) === 1;
  92. }
  93. return preg_match($filterExpression, $asset) === 1;
  94. });
  95. return $this->connection->getSchemaManager()->createSchema();
  96. }
  97. /**
  98. * @param Schema $targetSchema
  99. * @param \Doctrine\DBAL\Connection $connection
  100. * @return \Doctrine\DBAL\Schema\SchemaDiff
  101. */
  102. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
  103. // adjust varchar columns with a length higher then getVarcharMaxLength to clob
  104. foreach ($targetSchema->getTables() as $table) {
  105. foreach ($table->getColumns() as $column) {
  106. if ($column->getType() instanceof StringType) {
  107. if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) {
  108. $column->setType(Type::getType('text'));
  109. $column->setLength(null);
  110. }
  111. }
  112. }
  113. }
  114. $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
  115. /** @var string|AbstractAsset $asset */
  116. $filterExpression = $this->getFilterExpression();
  117. if ($asset instanceof AbstractAsset) {
  118. return preg_match($filterExpression, $asset->getName()) === 1;
  119. }
  120. return preg_match($filterExpression, $asset) === 1;
  121. });
  122. $sourceSchema = $connection->getSchemaManager()->createSchema();
  123. // remove tables we don't know about
  124. foreach ($sourceSchema->getTables() as $table) {
  125. if (!$targetSchema->hasTable($table->getName())) {
  126. $sourceSchema->dropTable($table->getName());
  127. }
  128. }
  129. // remove sequences we don't know about
  130. foreach ($sourceSchema->getSequences() as $table) {
  131. if (!$targetSchema->hasSequence($table->getName())) {
  132. $sourceSchema->dropSequence($table->getName());
  133. }
  134. }
  135. $comparator = new Comparator();
  136. return $comparator->compare($sourceSchema, $targetSchema);
  137. }
  138. /**
  139. * @param \Doctrine\DBAL\Schema\Schema $targetSchema
  140. * @param \Doctrine\DBAL\Connection $connection
  141. *
  142. * @throws Exception
  143. */
  144. protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) {
  145. if (is_null($connection)) {
  146. $connection = $this->connection;
  147. }
  148. $schemaDiff = $this->getDiff($targetSchema, $connection);
  149. if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
  150. $connection->beginTransaction();
  151. }
  152. $sqls = $schemaDiff->toSql($connection->getDatabasePlatform());
  153. $step = 0;
  154. foreach ($sqls as $sql) {
  155. $this->emit($sql, $step++, count($sqls));
  156. $connection->query($sql);
  157. }
  158. if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
  159. $connection->commit();
  160. }
  161. }
  162. /**
  163. * @param $statement
  164. * @return string
  165. */
  166. protected function convertStatementToScript($statement) {
  167. $script = $statement . ';';
  168. $script .= PHP_EOL;
  169. $script .= PHP_EOL;
  170. return $script;
  171. }
  172. protected function getFilterExpression() {
  173. return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
  174. }
  175. protected function emit($sql, $step, $max) {
  176. if ($this->noEmit) {
  177. return;
  178. }
  179. if (is_null($this->dispatcher)) {
  180. return;
  181. }
  182. $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max]));
  183. }
  184. }