Migration.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Migrations;
  20. use Doctrine\DBAL\Migrations\Configuration\Configuration;
  21. /**
  22. * Class for running migrations to the current version or a manually specified version.
  23. *
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.doctrine-project.org
  26. * @since 2.0
  27. * @author Jonathan H. Wage <jonwage@gmail.com>
  28. */
  29. class Migration
  30. {
  31. /**
  32. * The OutputWriter object instance used for outputting information
  33. *
  34. * @var OutputWriter
  35. */
  36. private $outputWriter;
  37. /**
  38. * @var Configuration
  39. */
  40. private $configuration;
  41. /**
  42. * @var boolean
  43. */
  44. private $noMigrationException;
  45. /**
  46. * Construct a Migration instance
  47. *
  48. * @param Configuration $configuration A migration Configuration instance
  49. */
  50. public function __construct(Configuration $configuration)
  51. {
  52. $this->configuration = $configuration;
  53. $this->outputWriter = $configuration->getOutputWriter();
  54. $this->noMigrationException = false;
  55. }
  56. /**
  57. * Get the array of versions and SQL queries that would be executed for
  58. * each version but do not execute anything.
  59. *
  60. * @param string $to The version to migrate to.
  61. *
  62. * @return array $sql The array of SQL queries.
  63. */
  64. public function getSql($to = null)
  65. {
  66. return $this->migrate($to, true);
  67. }
  68. /**
  69. * Write a migration SQL file to the given path
  70. *
  71. * @param string $path The path to write the migration SQL file.
  72. * @param string $to The version to migrate to.
  73. *
  74. * @return boolean $written
  75. */
  76. public function writeSqlFile($path, $to = null)
  77. {
  78. $sql = $this->getSql($to);
  79. $from = $this->configuration->getCurrentVersion();
  80. if ($to === null) {
  81. $to = $this->configuration->getLatestVersion();
  82. }
  83. $direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP;
  84. $this->outputWriter->write(sprintf("-- Migrating from %s to %s\n", $from, $to));
  85. $sqlWriter = new SqlFileWriter(
  86. $this->configuration->getMigrationsColumnName(),
  87. $this->configuration->getMigrationsTableName(),
  88. $path,
  89. $this->outputWriter
  90. );
  91. return $sqlWriter->write($sql, $direction);
  92. }
  93. /**
  94. * @param boolean $noMigrationException Throw an exception or not if no migration is found. Mostly for Continuous Integration.
  95. */
  96. public function setNoMigrationException($noMigrationException = false)
  97. {
  98. $this->noMigrationException = $noMigrationException;
  99. }
  100. /**
  101. * Run a migration to the current version or the given target version.
  102. *
  103. * @param string $to The version to migrate to.
  104. * @param boolean $dryRun Whether or not to make this a dry run and not execute anything.
  105. * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query.
  106. * @param callable|null $confirm A callback to confirm whether the migrations should be executed.
  107. *
  108. * @return array An array of migration sql statements. This will be empty if the the $confirm callback declines to execute the migration
  109. *
  110. * @throws MigrationException
  111. */
  112. public function migrate($to = null, $dryRun = false, $timeAllQueries = false, callable $confirm = null)
  113. {
  114. /**
  115. * If no version to migrate to is given we default to the last available one.
  116. */
  117. if ($to === null) {
  118. $to = $this->configuration->getLatestVersion();
  119. }
  120. $from = (string) $this->configuration->getCurrentVersion();
  121. $to = (string) $to;
  122. /**
  123. * Throw an error if we can't find the migration to migrate to in the registered
  124. * migrations.
  125. */
  126. $migrations = $this->configuration->getMigrations();
  127. if (!isset($migrations[$to]) && $to > 0) {
  128. throw MigrationException::unknownMigrationVersion($to);
  129. }
  130. $direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP;
  131. $migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to);
  132. /**
  133. * If
  134. * there are no migrations to execute
  135. * and there are migrations,
  136. * and the migration from and to are the same
  137. * means we are already at the destination return an empty array()
  138. * to signify that there is nothing left to do.
  139. */
  140. if ($from === $to && empty($migrationsToExecute) && !empty($migrations)) {
  141. return $this->noMigrations();
  142. }
  143. if (!$dryRun && false === $this->migrationsCanExecute($confirm)) {
  144. return [];
  145. }
  146. $output = $dryRun ? 'Executing dry run of migration' : 'Migrating';
  147. $output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>';
  148. $this->outputWriter->write(sprintf($output, $direction, $to, $from));
  149. /**
  150. * If there are no migrations to execute throw an exception.
  151. */
  152. if (empty($migrationsToExecute) && !$this->noMigrationException) {
  153. throw MigrationException::noMigrationsToExecute();
  154. } elseif (empty($migrationsToExecute)) {
  155. return $this->noMigrations();
  156. }
  157. $sql = [];
  158. $time = 0;
  159. foreach ($migrationsToExecute as $version) {
  160. $versionSql = $version->execute($direction, $dryRun, $timeAllQueries);
  161. $sql[$version->getVersion()] = $versionSql;
  162. $time += $version->getTime();
  163. }
  164. $this->outputWriter->write("\n <comment>------------------------</comment>\n");
  165. $this->outputWriter->write(sprintf(" <info>++</info> finished in %ss", $time));
  166. $this->outputWriter->write(sprintf(" <info>++</info> %s migrations executed", count($migrationsToExecute)));
  167. $this->outputWriter->write(sprintf(" <info>++</info> %s sql queries", count($sql, true) - count($sql)));
  168. return $sql;
  169. }
  170. private function noMigrations()
  171. {
  172. $this->outputWriter->write('<comment>No migrations to execute.</comment>');
  173. return [];
  174. }
  175. private function migrationsCanExecute(callable $confirm=null)
  176. {
  177. return null === $confirm ? true : $confirm();
  178. }
  179. }