/**
  * @inheritdoc
  */
 protected function doLoad($file)
 {
     libxml_use_internal_errors(true);
     $xml = new \DOMDocument();
     $xml->load($file);
     if (!$xml->schemaValidate(__DIR__ . DIRECTORY_SEPARATOR . "XML" . DIRECTORY_SEPARATOR . "configuration.xsd")) {
         libxml_clear_errors();
         throw MigrationException::configurationNotValid('XML configuration did not pass the validation test.');
     }
     $xml = simplexml_load_file($file, "SimpleXMLElement", LIBXML_NOCDATA);
     $config = [];
     if (isset($xml->name)) {
         $config['name'] = (string) $xml->name;
     }
     if (isset($xml->table['name'])) {
         $config['table_name'] = (string) $xml->table['name'];
     }
     if (isset($xml->table['column'])) {
         $config['column_name'] = (string) $xml->table['column'];
     }
     if (isset($xml->{'migrations-namespace'})) {
         $config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
     }
     if (isset($xml->{'organize-migrations'})) {
         $config['organize_migrations'] = $xml->{'organize-migrations'};
     }
     if (isset($xml->{'migrations-directory'})) {
         $config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
     }
     if (isset($xml->migrations->migration)) {
         $config['migrations'] = $xml->migrations->migration;
     }
     $this->setConfiguration($config);
 }
Esempio 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');
     $markMigrated = $input->getOption('add') ? true : false;
     if (!$configuration->hasVersion($version)) {
         throw MigrationException::unknownMigrationVersion($version);
     }
     $version = $configuration->getVersion($version);
     if ($markMigrated && $configuration->hasVersionMigrated($version)) {
         throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
     }
     if (!$markMigrated && !$configuration->hasVersionMigrated($version)) {
         throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
     }
     if ($markMigrated) {
         $version->markMigrated();
     } else {
         $version->markNotMigrated();
     }
 }
 /**
  * Load the information from the passed configuration file
  *
  * @param string $file The path to the configuration file
  *
  * @throws MigrationException Throws exception if configuration file was already loaded
  */
 public function load($file)
 {
     if ($this->loaded) {
         throw MigrationException::configurationFileAlreadyLoaded();
     }
     if (file_exists($path = getcwd() . '/' . $file)) {
         $file = $path;
     }
     $this->file = $file;
     $this->doLoad($file);
     $this->loaded = true;
 }
 /**
  * Load the information from the passed configuration file
  *
  * @param string $file The path to the configuration file
  *
  * @throws MigrationException Throws exception if configuration file was already loaded
  */
 public function load($file)
 {
     if ($this->loaded) {
         throw MigrationException::configurationFileAlreadyLoaded();
     }
     if (file_exists($path = getcwd() . '/' . $file)) {
         $file = $path;
     }
     $this->file = $file;
     if (!file_exists($file)) {
         throw new \InvalidArgumentException('Given config file does not exist');
     }
     $this->doLoad($file);
     $this->loaded = true;
 }
 /**
  * Add a migration version to the migrations table or remove it.
  *
  * This does not execute any migration code but simply records a version
  * as migrated or not.
  *
  * @param string $version The version to add or remove
  * @param boolean $markAsMigrated
  * @return void
  * @throws \Doctrine\DBAL\Migrations\MigrationException
  * @throws \LogicException
  */
 public function markAsMigrated($version, $markAsMigrated)
 {
     $configuration = $this->getMigrationConfiguration();
     if ($version === 'all') {
         foreach ($configuration->getMigrations() as $version) {
             if ($markAsMigrated === true && $configuration->hasVersionMigrated($version) === false) {
                 $version->markMigrated();
             } elseif ($markAsMigrated === false && $configuration->hasVersionMigrated($version) === true) {
                 $version->markNotMigrated();
             }
         }
     } else {
         if ($configuration->hasVersion($version) === false) {
             throw \Doctrine\DBAL\Migrations\MigrationException::unknownMigrationVersion($version);
         }
         $version = $configuration->getVersion($version);
         if ($markAsMigrated === true) {
             if ($configuration->hasVersionMigrated($version) === true) {
                 throw new \Doctrine\DBAL\Migrations\MigrationException(sprintf('The version "%s" already exists in the version table.', $version));
             }
             $version->markMigrated();
         } else {
             if ($configuration->hasVersionMigrated($version) === false) {
                 throw new \Doctrine\DBAL\Migrations\MigrationException(sprintf('The version "%s" does not exist in the version table.', $version));
             }
             $version->markNotMigrated();
         }
     }
 }
Esempio n. 6
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
  *
  * @throws MigrationException Throws exception if migration version does not exist.
  */
 public function getVersion($version)
 {
     if (!isset($this->migrations[$version])) {
         throw MigrationException::unknownMigrationVersion($version);
     }
     return $this->migrations[$version];
 }
 /**
  * @param string $class
  */
 private function ensureMigrationClassExists($class)
 {
     if (!class_exists($class)) {
         throw MigrationException::migrationClassNotFound($class, $this->getMigrationsNamespace());
     }
 }
Esempio n. 8
0
 /**
  * @throws MigrationException
  */
 private function ensureOrganizeMigrationsIsCompatibleWithFinder()
 {
     if (!$this->migrationFinder instanceof MigrationDeepFinderInterface) {
         throw MigrationException::configurationIncompatibleWithFinder('organize-migrations', $this->migrationFinder);
     }
 }
Esempio n. 9
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;
 }
 private function mark($version, $all = false)
 {
     if (!$this->configuration->hasVersion($version)) {
         throw MigrationException::unknownMigrationVersion($version);
     }
     $version = $this->configuration->getVersion($version);
     if ($this->markMigrated && $this->configuration->hasVersionMigrated($version)) {
         $marked = true;
         if (!$all) {
             throw new \InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
         }
     }
     if (!$this->markMigrated && !$this->configuration->hasVersionMigrated($version)) {
         $marked = false;
         if (!$all) {
             throw new \InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
         }
     }
     if (!isset($marked)) {
         if ($this->markMigrated) {
             $version->markMigrated();
         } else {
             $version->markNotMigrated();
         }
     }
 }
 /**
  * Add a migration version to the migrations table or remove it.
  *
  * This does not execute any migration code but simply records a version
  * as migrated or not.
  *
  * @param string $version The version to add or remove
  * @param boolean $markAsMigrated
  * @return void
  * @throws MigrationException
  * @throws \LogicException
  */
 public function markAsMigrated($version, $markAsMigrated)
 {
     $configuration = $this->getMigrationConfiguration();
     if ($version === 'all') {
         foreach ($configuration->getMigrations() as $version) {
             if ($markAsMigrated === true && $configuration->hasVersionMigrated($version) === false) {
                 $version->markMigrated();
             } elseif ($markAsMigrated === false && $configuration->hasVersionMigrated($version) === true) {
                 $version->markNotMigrated();
             }
         }
     } else {
         if ($configuration->hasVersion($version) === false) {
             throw MigrationException::unknownMigrationVersion($version);
         }
         $version = $configuration->getVersion($version);
         if ($markAsMigrated === true) {
             if ($configuration->hasVersionMigrated($version) === true) {
                 throw new MigrationException(sprintf('The version "%s" is already marked as executed.', $version));
             }
             $version->markMigrated();
         } else {
             if ($configuration->hasVersionMigrated($version) === false) {
                 throw new MigrationException(sprintf('The version "%s" is already marked as not executed.', $version));
             }
             $version->markNotMigrated();
         }
     }
 }
 /**
  * @param            $versionName
  * @param bool|false $all
  *
  * @throws MigrationException
  */
 protected function mark($versionName, $all = false)
 {
     if (!$this->configuration->hasVersion($versionName)) {
         throw MigrationException::unknownMigrationVersion($versionName);
     }
     $version = $this->configuration->getVersion($versionName);
     if ($this->markMigrated && $this->configuration->hasVersionMigrated($version)) {
         $marked = true;
         if (!$all) {
             throw new InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
         }
     }
     if (!$this->markMigrated && !$this->configuration->hasVersionMigrated($version)) {
         $marked = false;
         if (!$all) {
             throw new InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
         }
     }
     if (!isset($marked)) {
         $filename = $this->configuration->getNamingStrategy()->getFilename($versionName);
         if ($this->markMigrated) {
             $version->markMigrated();
             $this->info('<info>Added version to table:</info> ' . $filename);
         } else {
             $version->markNotMigrated();
             $this->info('<info>Removed version from table:</info> ' . $filename);
         }
     }
 }
Esempio n. 13
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;
 }
Esempio n. 14
0
 /**
  * Write a migration SQL file to the given path
  *
  * @param string $path      The path to write the migration SQL file.
  * @param string $direction The direction to execute.
  *
  * @return boolean $written
  */
 public function writeSqlFile($path, $direction = self::DIRECTION_UP)
 {
     $queries = $this->execute($direction, true);
     if (!empty($this->params)) {
         throw MigrationException::migrationNotConvertibleToSql($this->class);
     }
     $this->outputWriter->write("\n-- Version " . $this->version . "\n");
     $sqlQueries = [$this->version => $queries];
     $sqlWriter = new SqlFileWriter($this->configuration->getMigrationsColumnName(), $this->configuration->getMigrationsTableName(), $path, $this->outputWriter);
     return $sqlWriter->write($sqlQueries, $direction);
 }