vendor/doctrine-migrations/lib/Doctrine/DBAL/Migrations/Migration.php
changeset 39 03b14b0fe101
equal deleted inserted replaced
38:bbdc7f9aa25e 39:03b14b0fe101
       
     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 
       
    20 namespace Doctrine\DBAL\Migrations;
       
    21 
       
    22 use Doctrine\DBAL\Migrations\Configuration\Configuration,
       
    23     Doctrine\DBAL\Schema\Schema;
       
    24 
       
    25 /**
       
    26  * Class for running migrations to the current version or a manually specified version.
       
    27  *
       
    28  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
       
    29  * @link        www.doctrine-project.org
       
    30  * @since       2.0
       
    31  * @author      Jonathan H. Wage <jonwage@gmail.com>
       
    32  */
       
    33 class Migration
       
    34 {
       
    35     /**
       
    36      * The OutputWriter object instance used for outputting information
       
    37      *
       
    38      * @var OutputWriter
       
    39      */
       
    40     private $outputWriter;
       
    41 
       
    42     /**
       
    43      * @var Configuration
       
    44      */
       
    45     private $configuration;
       
    46 
       
    47     /**
       
    48      * Construct a Migration instance
       
    49      *
       
    50      * @param Configuration $configuration  A migration Configuration instance
       
    51      */
       
    52     public function __construct(Configuration $configuration)
       
    53     {
       
    54         $this->configuration = $configuration;
       
    55         $this->outputWriter = $configuration->getOutputWriter();
       
    56     }
       
    57 
       
    58     /**
       
    59      * Get the array of versions and SQL queries that would be executed for 
       
    60      * each version but do not execute anything.
       
    61      *
       
    62      * @param string $to   The version to migrate to.
       
    63      * @return array $sql  The array of SQL queries.
       
    64      */
       
    65     public function getSql($to = null)
       
    66     {
       
    67         return $this->migrate($to, true);
       
    68     }
       
    69 
       
    70     /**
       
    71      * Write a migration SQL file to the given path
       
    72      *
       
    73      * @param string $path   The path to write the migration SQL file.
       
    74      * @param string $to     The version to migrate to.
       
    75      * @return bool $written
       
    76      */
       
    77     public function writeSqlFile($path, $to = null)
       
    78     {
       
    79         $sql = $this->getSql($to);
       
    80 
       
    81         $from = $this->configuration->getCurrentVersion();
       
    82         if ($to === null) {
       
    83             $to = $this->configuration->getLatestVersion();
       
    84         }
       
    85 
       
    86         $string  = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
       
    87         $string .= sprintf("# Migrating from %s to %s\n", $from, $to);
       
    88 
       
    89         foreach ($sql as $version => $queries) {
       
    90             $string .= "\n# Version " . $version . "\n";
       
    91             foreach ($queries as $query) {
       
    92                 $string .= $query . ";\n";
       
    93             }
       
    94         }
       
    95         if (is_dir($path)) {
       
    96             $path = realpath($path);
       
    97             $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
       
    98         }
       
    99 
       
   100         $this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
       
   101 
       
   102         return file_put_contents($path, $string);
       
   103     }
       
   104 
       
   105     /**
       
   106      * Run a migration to the current version or the given target version.
       
   107      *
       
   108      * @param string $to      The version to migrate to.
       
   109      * @param string $dryRun  Whether or not to make this a dry run and not execute anything.
       
   110      * @return array $sql     The array of migration sql statements
       
   111      * @throws MigrationException
       
   112      */
       
   113     public function migrate($to = null, $dryRun = false)
       
   114     {
       
   115         if ($to === null) {
       
   116             $to = $this->configuration->getLatestVersion();
       
   117         }
       
   118 
       
   119         $from = $this->configuration->getCurrentVersion();
       
   120         $from = (string) $from;
       
   121         $to = (string) $to;
       
   122 
       
   123         $migrations = $this->configuration->getMigrations();
       
   124         if ( ! isset($migrations[$to]) && $to > 0) {
       
   125             throw MigrationException::unknownMigrationVersion($to);
       
   126         }
       
   127 
       
   128         if ($from === $to) {
       
   129             return array();
       
   130         }
       
   131 
       
   132         $direction = $from > $to ? 'down' : 'up';
       
   133         $migrations = $this->configuration->getMigrationsToExecute($direction, $to);
       
   134 
       
   135         if ($dryRun === false) {
       
   136             $this->outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
       
   137         } else {
       
   138             $this->outputWriter->write(sprintf('Executing dry run of migration <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
       
   139         }
       
   140 
       
   141         if (empty($migrations)) {
       
   142             throw MigrationException::noMigrationsToExecute();
       
   143         }
       
   144 
       
   145         $sql = array();
       
   146         $time = 0;
       
   147         foreach ($migrations as $version) {
       
   148             $versionSql = $version->execute($direction, $dryRun);
       
   149             $sql[$version->getVersion()] = $versionSql;
       
   150             $time += $version->getTime();
       
   151         }
       
   152 
       
   153         $this->outputWriter->write("\n  <comment>------------------------</comment>\n");
       
   154         $this->outputWriter->write(sprintf("  <info>++</info> finished in %s", $time));
       
   155         $this->outputWriter->write(sprintf("  <info>++</info> %s migrations executed", count($migrations)));
       
   156         $this->outputWriter->write(sprintf("  <info>++</info> %s sql queries", count($sql, true) - count($sql)));
       
   157 
       
   158         return $sql;
       
   159     }
       
   160 }