Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add the possibility to synchronize all migration versions available in the version table. #123

Merged
merged 1 commit into from

7 participants

@ErikSaunier

Add the possibility to synchronize all migration versions available in the version table.
Very helpful in some case.
./app/console doctrine:migrations:version --add --all
./app/console doctrine:migrations:version --delete --all

...L/Migrations/Tools/Console/Command/VersionCommand.php
((20 lines not shown))
->addOption('add', null, InputOption::VALUE_NONE, 'Add the specified version.')
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the specified version.')
+ ->addOption('sync', null, InputOption::VALUE_NONE, 'Apply to all the versions.')
@stof Collaborator
stof added a note

I would name it --all instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ErikSaunier

@stof : Did you think this can be merged ?

@ErikSaunier

Is there something missing for this patch to be merge ?

...L/Migrations/Tools/Console/Command/VersionCommand.php
((16 lines not shown))
- if ( ! $configuration->hasVersion($version)) {
+ if ($input->getOption('all') === true) {
+ $availableVersions = $this->conf->getAvailableVersions();
+ foreach ($availableVersions as $version) {
+ $this->mark($version, true);
+ }
+ } else {
+ $this->mark($version);
+ }
+ }
+
+ protected function mark($version, $all = null)
@stof Collaborator
stof added a note

please make it private and change the default value to false instead of null for the boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ErikSaunier

Thank you @stof for your swift answer !

@ErikSaunier

@stof : Good to merge ?

@garak

:+1: please merge, it's very useful!

@krymen

:+1: to merge

@adri

Any updates on this?

@ErikSaunier

No updates on this, waiting for merge.

@adri

@ErikSaunier maybe someone who can merge it is looking for some form of tests?

@adri

A workaround for this if you are on UNIX is:
ls app/DoctrineMigrations | sed 's/[^0-9]*//g' | xargs -L 1 app/console doctrine:migrations:version --add

@ErikSaunier

Hi @stof, what did you think about this ?

@ErikSaunier

I squashed my commits. Feel free to comment!

...L/Migrations/Tools/Console/Command/VersionCommand.php
((26 lines not shown))
<info>%command.full_name% YYYYMMDDHHMMSS --add</info>
If you want to delete a version you can use the <comment>--delete</comment> option:
<info>%command.full_name% YYYYMMDDHHMMSS --delete</info>
+
+If you want to synchronize by addind or deleting all migration versions available in the version table you can use the <comment>--all</comment> option:
@jwage Owner
jwage added a note

Typo here addind should be adding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...L/Migrations/Tools/Console/Command/VersionCommand.php
@@ -36,22 +36,37 @@
*/
class VersionCommand extends AbstractCommand
{
+ /**
+ * The Migrations Configuration instance
+ *
+ * @var \Doctrine\DBAL\Migrations\Configuration\Configuration
+ */
+ private $conf;
+
+ private $markMigrated;
@jwage Owner
jwage added a note

Missing doc block here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...L/Migrations/Tools/Console/Command/VersionCommand.php
@@ -36,22 +36,37 @@
*/
class VersionCommand extends AbstractCommand
{
+ /**
+ * The Migrations Configuration instance
+ *
+ * @var \Doctrine\DBAL\Migrations\Configuration\Configuration
+ */
+ private $conf;
@jwage Owner
jwage added a note

Use $configuration instead of $conf here to be consistent with the rest of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...L/Migrations/Tools/Console/Command/VersionCommand.php
@@ -60,32 +75,53 @@ protected function configure()
public function execute(InputInterface $input, OutputInterface $output)
{
- $configuration = $this->getMigrationConfiguration($input, $output);
+ $this->conf = $this->getMigrationConfiguration($input, $output);
@jwage Owner
jwage added a note

Same thing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...L/Migrations/Tools/Console/Command/VersionCommand.php
((15 lines not shown))
- if ( ! $configuration->hasVersion($version)) {
+ if ($input->getOption('all') === true) {
+ $availableVersions = $this->conf->getAvailableVersions();
@jwage Owner
jwage added a note

Move this to a private method as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ErikSaunier

Thanks @jwage for the code review.

@jwage
Owner

No problem. The only other thing I can think of is this command is missing is a confirmation step. Since this command has the potential to destroy data maybe we should add one?

@ErikSaunier

That's a good idea @jwage ! Could you have a look on my confirmation step?

@ErikSaunier

:birthday: Happy Birthday PR #123

@jwage
Owner

:+1:

@jwage jwage merged commit 77d9ffe into doctrine:master

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed
@TomasVotruba

Issue #46 can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 9, 2014
  1. @ErikSaunier
This page is out of date. Refresh to see the latest.
Showing with 76 additions and 16 deletions.
  1. +76 −16 lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php
View
92 lib/Doctrine/DBAL/Migrations/Tools/Console/Command/VersionCommand.php
@@ -19,7 +19,6 @@
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
-use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\MigrationException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -36,56 +35,117 @@
*/
class VersionCommand extends AbstractCommand
{
+ /**
+ * The Migrations Configuration instance
+ *
+ * @var \Doctrine\DBAL\Migrations\Configuration\Configuration
+ */
+ private $configuration;
+
+ /**
+ * Whether or not the versions have to be marked as migrated or not
+ *
+ * @var boolean
+ */
+ private $markMigrated;
+
protected function configure()
{
$this
->setName('migrations:version')
->setDescription('Manually add and delete migration versions from the version table.')
- ->addArgument('version', InputArgument::REQUIRED, 'The version to add or delete.', null)
+ ->addArgument('version', InputArgument::OPTIONAL, 'The version to add or delete.', null)
->addOption('add', null, InputOption::VALUE_NONE, 'Add the specified version.')
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the specified version.')
+ ->addOption('all', null, InputOption::VALUE_NONE, 'Apply to all the versions.')
->setHelp(<<<EOT
-The <info>%command.name%</info> command allows you to manually add and delete migration versions from the version table:
+The <info>%command.name%</info> command allows you to manually add, delete or synchronize migration versions from the version table:
<info>%command.full_name% YYYYMMDDHHMMSS --add</info>
If you want to delete a version you can use the <comment>--delete</comment> option:
<info>%command.full_name% YYYYMMDDHHMMSS --delete</info>
+
+If you want to synchronize by adding or deleting all migration versions available in the version table you can use the <comment>--all</comment> option:
+
+ <info>%command.full_name% --add --all</info>
+ <info>%command.full_name% --delete --all</info>
+
+You can also execute this command without a warning message which you need to interact with:
+
+ <info>%command.full_name% --no-interaction</info>
EOT
- );
+ );
parent::configure();
}
public function execute(InputInterface $input, OutputInterface $output)
{
- $configuration = $this->getMigrationConfiguration($input, $output);
+ $this->configuration = $this->getMigrationConfiguration($input, $output);
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.');
}
+ $this->markMigrated = $input->getOption('add') ? true : false;
+
+ $noInteraction = $input->getOption('no-interaction') ? true : false;
+ if ($noInteraction === true) {
+ $this->markAllAvailableVersions($input);
+ } else {
+ $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>WARNING! You are about to add, delete or synchronize migration versions from the version table that could result in data lost. Are you sure you wish to continue? (y/n)</question>', false);
+ if ($confirmation === true) {
+ $this->markAllAvailableVersions($input);
+ } else {
+ $output->writeln('<error>Migration cancelled!</error>');
+ }
+ }
+
+ }
+
+ private function markAllAvailableVersions(InputInterface $input)
+ {
$version = $input->getArgument('version');
- $markMigrated = $input->getOption('add') ? true : false;
- if ( ! $configuration->hasVersion($version)) {
+ if ($input->getOption('all') === true) {
+ $availableVersions = $this->configuration->getAvailableVersions();
+ foreach ($availableVersions as $version) {
+ $this->mark($version, true);
+ }
+ } else {
+ $this->mark($version);
+ }
+ }
+
+ private function mark($version, $all = false)
+ {
+ if ( ! $this->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));
+ $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 ( ! $markMigrated && ! $configuration->hasVersionMigrated($version)) {
- throw new \InvalidArgumentException(sprintf('The version "%s" does not exist 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 ($markMigrated) {
- $version->markMigrated();
- } else {
- $version->markNotMigrated();
+ if ( ! isset($marked)) {
+ if ($this->markMigrated) {
+ $version->markMigrated();
+ } else {
+ $version->markNotMigrated();
+ }
}
}
}
Something went wrong with that request. Please try again.