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 boolean $dryRun         Whether or not to make this a dry run and not execute anything.
  * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query.
  *
  * @return array $sql     The array of migration sql statements
  *
  * @throws MigrationException
  */
 public function migrate($to = null, $dryRun = false, $timeAllQueries = false)
 {
     /**
      * If no version to migrate to is given we default to the last available one.
      */
     if ($to === null) {
         $to = $this->configuration->getLatestVersion();
     }
     $from = (string) $this->configuration->getCurrentVersion();
     $to = (string) $to;
     /**
      * Throw an error if we can't find the migration to migrate to in the registered
      * migrations.
      */
     $migrations = $this->configuration->getMigrations();
     if (!isset($migrations[$to]) && $to > 0) {
         throw MigrationException::unknownMigrationVersion($to);
     }
     $direction = $from > $to ? 'down' : 'up';
     $migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to);
     /**
      * If
      *  there are no migrations to execute
      *  and there are migrations,
      *  and the migration from and to are the same
      * means we are already at the destination return an empty array()
      * to signify that there is nothing left to do.
      */
     if ($from === $to && empty($migrationsToExecute) && !empty($migrations)) {
         return array();
     }
     $output = $dryRun ? 'Executing dry run of migration' : 'Migrating';
     $output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>';
     $this->outputWriter->write(sprintf($output, $direction, $to, $from));
     /**
      * If there are no migrations to execute throw an exception.
      */
     if (empty($migrationsToExecute)) {
         throw MigrationException::noMigrationsToExecute();
     }
     $sql = array();
     $time = 0;
     foreach ($migrationsToExecute as $version) {
         $versionSql = $version->execute($direction, $dryRun, $timeAllQueries);
         $sql[$version->getVersion()] = $versionSql;
         $time += $version->getTime();
     }
     $this->outputWriter->write("\n  <comment>------------------------</comment>\n");
     $this->outputWriter->write(sprintf("  <info>++</info> finished in %s", $time));
     $this->outputWriter->write(sprintf("  <info>++</info> %s migrations executed", count($migrationsToExecute)));
     $this->outputWriter->write(sprintf("  <info>++</info> %s sql queries", count($sql, true) - count($sql)));
     return $sql;
 }
Exemplo n.º 2
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();
     $time = 0;
     foreach ($migrations as $version) {
         $versionSql = $version->execute($direction, $dryRun);
         $sql[$version->getVersion()] = $versionSql;
         $time += $version->getTime();
     }
     $this->_outputWriter->write("\n  <comment>------------------------</comment>\n");
     $this->_outputWriter->write(sprintf("  <info>++</info> finished in %s", $time));
     $this->_outputWriter->write(sprintf("  <info>++</info> %s migrations executed", count($migrations)));
     $this->_outputWriter->write(sprintf("  <info>++</info> %s sql queries", count($sql, true) - count($sql)));
     return $sql;
 }