Doctrine\DBAL\Migrations\Migration::migrate PHP Method

migrate() public method

Run a migration to the current version or the given target version.
public migrate ( string $to = null, boolean $dryRun = false, boolean $timeAllQueries = false, callable $confirm = null ) : array
$to string The version to migrate to.
$dryRun boolean Whether or not to make this a dry run and not execute anything.
$timeAllQueries boolean Measuring or not the execution time of each SQL query.
$confirm callable A callback to confirm whether the migrations should be executed.
return array An array of migration sql statements. This will be empty if the the $confirm callback declines to execute the migration
    public function migrate($to = null, $dryRun = false, $timeAllQueries = false, callable $confirm = null)
    {
        /**
         * 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 ? Version::DIRECTION_DOWN : Version::DIRECTION_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 $this->noMigrations();
        }
        if (!$dryRun && false === $this->migrationsCanExecute($confirm)) {
            return [];
        }
        $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) && !$this->noMigrationException) {
            throw MigrationException::noMigrationsToExecute();
        } elseif (empty($migrationsToExecute)) {
            return $this->noMigrations();
        }
        $sql = [];
        $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 %ss", $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;
    }

Usage Example

 public function indexAction()
 {
     $container = $this->container;
     $conn = $this->get('doctrine')->getConnection();
     $dir = $container->getParameter('doctrine_migrations.dir_name');
     if (!file_exists($dir)) {
         mkdir($dir, 0777, true);
     }
     $configuration = new Configuration($conn);
     $configuration->setMigrationsNamespace($container->getParameter('doctrine_migrations.namespace'));
     $configuration->setMigrationsDirectory($dir);
     $configuration->registerMigrationsFromDirectory($dir);
     $configuration->setName($container->getParameter('doctrine_migrations.name'));
     $configuration->setMigrationsTableName($container->getParameter('doctrine_migrations.table_name'));
     $versions = $configuration->getMigrations();
     foreach ($versions as $version) {
         $migration = $version->getMigration();
         if ($migration instanceof ContainerAwareInterface) {
             $migration->setContainer($container);
         }
     }
     $migration = new Migration($configuration);
     $migrated = $migration->migrate();
     // ...
 }
All Usage Examples Of Doctrine\DBAL\Migrations\Migration::migrate