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(); } }
/** * 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(); } } }
/** * 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]; }
/** * 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); } } }
/** * 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; }
/** * 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 (empty($this->migrations)) { $this->registerMigrationsFromDirectory($this->getMigrationsDirectory()); } if (!isset($this->migrations[$version])) { throw MigrationException::unknownMigrationVersion($version); } return $this->migrations[$version]; }