/** * 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. * @param callable|null $confirm 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 * * @throws MigrationException */ 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; }
/** * 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; }
/** * @Route("/{connectionName}/execute/{version}") * @Method("POST") */ public function executeAction($connectionName, $version) { $connection = $this->getDoctrine()->getConnection($connectionName); $outputWriter = new MemoryOutputWriter(); $configuration = new Configuration($connection); $configuration->setName($this->container->getParameter('doctrine_migrations.name')); $configuration->setOutputWriter($outputWriter); $configuration->setMigrationsTableName($this->container->getParameter('doctrine_migrations.table_name')); $configuration->setMigrationsDirectory($this->container->getParameter('doctrine_migrations.dir_name')); $configuration->setMigrationsNamespace($this->container->getParameter('doctrine_migrations.namespace')); $migration = new Migration($configuration); $currentVersion = $configuration->getCurrentVersion(); $dryRun = false; $migration->setNoMigrationException(true); $sql = $migration->migrate($version, $dryRun, true); return $this->render('MarkeiDoctrineMigrationWebBundle:Migrate:execute.html.twig', ['connectionName' => $connectionName, 'to' => $version, 'from' => $currentVersion, 'output' => $outputWriter->getMemory(), 'name' => $this->container->getParameter('doctrine_migrations.name')]); }
public function testMigrateDownSeveralSteps() { $this->config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateUp'); $this->config->registerMigration(2, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationSkipMigration'); $this->config->registerMigration(3, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateFurther'); $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); $migration->migrate(3); $this->assertEquals(3, $this->config->getCurrentVersion()); $migration->migrate(0); $schema = $this->config->getConnection()->getSchemaManager()->createSchema(); $this->assertFalse($schema->hasTable('foo')); $this->assertFalse($schema->hasTable('bar')); $this->assertEquals(0, $this->config->getCurrentVersion()); $migrations = $this->config->getMigrations(); $this->assertFalse($migrations[1]->isMigrated()); $this->assertFalse($migrations[2]->isMigrated()); $this->assertFalse($migrations[3]->isMigrated()); }
public function testInterweavedMigrationsAreExecuted() { $this->config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrateAddSqlTest'); $this->config->registerMigration(3, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateFurther'); $migration = new \Doctrine\DBAL\Migrations\Migration($this->config); $migration->migrate(); $migrations = $this->config->getMigrations(); $this->assertTrue($migrations[1]->isMigrated()); $this->assertTrue($migrations[3]->isMigrated()); $this->assertEquals(3, $this->config->getCurrentVersion()); $config = new Configuration($this->connection); $config->setMigrationsNamespace('Doctrine\\DBAL\\Migrations\\Tests\\Functional'); $config->setMigrationsDirectory('.'); $config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrateAddSqlTest'); $config->registerMigration(2, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateUp'); $config->registerMigration(3, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrationMigrateFurther'); $this->assertEquals(1, count($config->getMigrationsToExecute('up', 3))); $migrations = $config->getMigrationsToExecute('up', 3); $this->assertTrue(isset($migrations[2])); $this->assertEquals(2, $migrations[2]->getVersion()); $migration = new \Doctrine\DBAL\Migrations\Migration($config); $migration->migrate(); $migrations = $config->getMigrations(); $this->assertTrue($migrations[1]->isMigrated()); $this->assertTrue($migrations[2]->isMigrated()); $this->assertTrue($migrations[3]->isMigrated()); $this->assertEquals(3, $config->getCurrentVersion()); }
public function getCurrentVersion() { $this->doRegister(); return parent::getCurrentVersion(); }