Exemplo n.º 1
0
 protected function _getMigrationConfiguration(InputInterface $input, OutputInterface $output)
 {
     $outputWriter = new OutputWriter(function ($message) use($output) {
         return $output->writeln($message);
     });
     $em = $this->getHelper('em')->getEntityManager();
     if ($input->getOption('configuration')) {
         $info = pathinfo($input->getOption('configuration'));
         $class = $info['extension'] === 'xml' ? 'DoctrineExtensions\\Migrations\\Configuration\\XmlConfiguration' : 'DoctrineExtensions\\Migrations\\Configuration\\YamlConfiguration';
         $configuration = new $class($em->getConnection(), $outputWriter);
         $configuration->load($input->getOption('configuration'));
     } else {
         if (file_exists('migrations.xml')) {
             $configuration = new XmlConfiguration($em->getConnection(), $outputWriter);
             $configuration->load('migrations.xml');
         } else {
             if (file_exists('migrations.yml')) {
                 $configuration = new YamlConfiguration($em->getConnection(), $outputWriter);
                 $configuration->load('migrations.yml');
             } else {
                 throw MigrationException::couldNotFindConfiguration();
             }
         }
     }
     return $configuration;
 }
Exemplo n.º 2
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $configuration = $this->_getMigrationConfiguration($input, $output);
     $migration = new Migration($configuration);
     if ($input->getOption('add') === false && $input->getOption('delete') === false) {
         throw new \InvalidArgumentException('You must specify whether you want to --add or --delete the specified version.');
     }
     $version = $input->getArgument('version');
     $migrated = $input->getOption('add') ? true : false;
     if (!$configuration->hasVersion($version)) {
         throw MigrationException::unknownMigrationVersion($version);
     }
     $version = $configuration->getVersion($version);
     if ($migrated && $configuration->hasVersionMigrated($version)) {
         throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
     }
     if (!$migrated && !$configuration->hasVersionMigrated($version)) {
         throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
     }
     $version->isMigrated($migrated);
 }
Exemplo n.º 3
0
 /**
  * Returns the Version instance for a given version in the format YYYYMMDDHHMMSS.
  *
  * @param string $version   The version string in the format YYYYMMDDHHMMSS.
  * @return Version $version
  * @throws MigrationException $exception Throws exception if migration version does not exist.
  */
 public function getVersion($version)
 {
     if (!isset($this->_migrations[$version])) {
         MigrationException::unknownMigrationVersion($version);
     }
     return $this->_migrations[$version];
 }
Exemplo n.º 4
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;
 }