Exemplo n.º 1
0
 /**
  * Run a migration to the current version or the given target version.
  *
  * @param string $to      The version to migrate to.
  * @param string $dryRun  Whether or not to make this a dry run and not execute anything.
  * @return array $sql     The array of migration sql statements
  * @throws MigrationException
  */
 public function migrate($to = null, $dryRun = false)
 {
     if ($to === null) {
         $to = $this->_configuration->getLatestVersion();
     }
     $from = $this->_configuration->getCurrentVersion();
     $from = (string) $from;
     $to = (string) $to;
     $migrations = $this->_configuration->getMigrations();
     if (!isset($migrations[$to]) && $to > 0) {
         throw MigrationException::unknownMigrationVersion($to);
     }
     if ($from === $to) {
         throw MigrationException::alreadyAtVersion($to);
     }
     $direction = $from > $to ? 'down' : 'up';
     $migrations = $this->_configuration->getMigrationsToExecute($direction, $to);
     if ($dryRun === false) {
         $this->_outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
     } else {
         $this->_outputWriter->write(sprintf('Executing dry run of migration <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from));
     }
     if (empty($migrations)) {
         throw MigrationException::noMigrationsToExecute();
     }
     $sql = array();
     foreach ($migrations as $version) {
         $versionSql = $version->execute($direction, $dryRun);
         $sql[$version->getVersion()] = $versionSql;
     }
     return $sql;
 }