Migration.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * Construct a Migration instance
  43. *
  44. * @param Configuration $configuration A migration Configuration instance
  45. */
  46. public function __construct(Configuration $configuration)
  47. {
  48. $this->configuration = $configuration;
  49. $this->outputWriter = $configuration->getOutputWriter();
  50. }
  51. /**
  52. * Get the array of versions and SQL queries that would be executed for
  53. * each version but do not execute anything.
  54. *
  55. * @param string $to The version to migrate to.
  56. *
  57. * @return array $sql The array of SQL queries.
  58. */
  59. public function getSql($to = null)
  60. {
  61. return $this->migrate($to, true);
  62. }
  63. /**
  64. * Write a migration SQL file to the given path
  65. *
  66. * @param string $path The path to write the migration SQL file.
  67. * @param string $to The version to migrate to.
  68. *
  69. * @return boolean $written
  70. */
  71. public function writeSqlFile($path, $to = null)
  72. {
  73. $sql = $this->getSql($to);
  74. $from = $this->configuration->getCurrentVersion();
  75. if ($to === null) {
  76. $to = $this->configuration->getLatestVersion();
  77. }
  78. $string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
  79. $string .= sprintf("# Migrating from %s to %s\n", $from, $to);
  80. foreach ($sql as $version => $queries) {
  81. $string .= "\n# Version " . $version . "\n";
  82. foreach ($queries as $query) {
  83. $string .= $query . ";\n";
  84. }
  85. }
  86. if (is_dir($path)) {
  87. $path = realpath($path);
  88. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  89. }
  90. $this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
  91. return file_put_contents($path, $string);
  92. }
  93. /**
  94. * Run a migration to the current version or the given target version.
  95. *
  96. * @param string $to The version to migrate to.
  97. * @param boolean $dryRun Whether or not to make this a dry run and not execute anything.
  98. *
  99. * @return array $sql The array of migration sql statements
  100. *
  101. * @throws MigrationException
  102. */
  103. public function migrate($to = null, $dryRun = false)
  104. {
  105. if ($to === null) {
  106. $to = $this->configuration->getLatestVersion();
  107. }
  108. $from = $this->configuration->getCurrentVersion();
  109. $from = (string) $from;
  110. $to = (string) $to;
  111. $migrations = $this->configuration->getMigrations();
  112. if ( ! isset($migrations[$to]) && $to > 0) {
  113. throw MigrationException::unknownMigrationVersion($to);
  114. }
  115. $direction = $from > $to ? 'down' : 'up';
  116. $migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to);
  117. if ($from === $to && empty($migrationsToExecute) && $migrations) {
  118. return array();
  119. }
  120. if ($dryRun === false) {
  121. $this->outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
  122. } else {
  123. $this->outputWriter->write(sprintf('Executing dry run of migration <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
  124. }
  125. if (empty($migrationsToExecute)) {
  126. throw MigrationException::noMigrationsToExecute();
  127. }
  128. $sql = array();
  129. $time = 0;
  130. foreach ($migrationsToExecute as $version) {
  131. $versionSql = $version->execute($direction, $dryRun);
  132. $sql[$version->getVersion()] = $versionSql;
  133. $time += $version->getTime();
  134. }
  135. $this->outputWriter->write("\n <comment>------------------------</comment>\n");
  136. $this->outputWriter->write(sprintf(" <info>++</info> finished in %s", $time));
  137. $this->outputWriter->write(sprintf(" <info>++</info> %s migrations executed", count($migrationsToExecute)));
  138. $this->outputWriter->write(sprintf(" <info>++</info> %s sql queries", count($sql, true) - count($sql)));
  139. return $sql;
  140. }
  141. }