/** * Run a migration to the current version or the given target version. * * @param string $to The version to migrate to. * * @throws AntiMattr\MongoDB\Migrations\Exception\UnknownVersionException * @throws AntiMattr\MongoDB\Migrations\Exception\NoMigrationsToExecuteException */ public function migrate($to = null) { 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 new UnknownVersionException($to); } $direction = $from > $to ? 'down' : 'up'; $migrationsToExecute = $this->configuration->getMigrationsToExecute($direction, $to); if ($from === $to && empty($migrationsToExecute) && $migrations) { return; } $this->outputWriter->write(sprintf('Migrating <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>', $direction, $to, $from)); if (empty($migrationsToExecute)) { throw new NoMigrationsToExecuteException('Could not find any migrations to execute.'); } $time = 0; foreach ($migrationsToExecute as $version) { $versionSql = $version->execute($direction); $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))); }
public static function configureMigrations(ContainerInterface $container, Configuration $configuration) { $dir = $container->getParameter('mongo_db_migrations.dir_name'); if (!file_exists($dir)) { mkdir($dir, 0777, true); } $configuration->setMigrationsCollectionName($container->getParameter('mongo_db_migrations.collection_name')); $configuration->setMigrationsDatabaseName($container->getParameter('mongo_db_migrations.database_name')); $configuration->setMigrationsDirectory($dir); $configuration->setMigrationsNamespace($container->getParameter('mongo_db_migrations.namespace')); $configuration->setName($container->getParameter('mongo_db_migrations.name')); $configuration->registerMigrationsFromDirectory($dir); self::injectContainerToMigrations($container, $configuration->getMigrations()); }