From a9ec86faa3a3f7f592a633af43b6ef6c9f358239 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 27 Aug 2015 11:53:14 +0200 Subject: [PATCH] adding an option to not throw an exception if no migration is found This is mostly usefull for Continuous integration system --- lib/Doctrine/DBAL/Migrations/Migration.php | 16 +++++++++++++++- .../Tools/Console/Command/MigrateCommand.php | 2 ++ .../DBAL/Migrations/Tests/MigrationTest.php | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Migrations/Migration.php b/lib/Doctrine/DBAL/Migrations/Migration.php index ae9fcbf0c1..f68fae1e88 100644 --- a/lib/Doctrine/DBAL/Migrations/Migration.php +++ b/lib/Doctrine/DBAL/Migrations/Migration.php @@ -43,6 +43,11 @@ class Migration */ private $configuration; + /** + * @var boolean + */ + private $noMigrationException; + /** * Construct a Migration instance * @@ -52,6 +57,7 @@ public function __construct(Configuration $configuration) { $this->configuration = $configuration; $this->outputWriter = $configuration->getOutputWriter(); + $this->noMigrationException = false; } /** @@ -97,6 +103,14 @@ public function writeSqlFile($path, $to = null) return $sqlWriter->write($sql, $direction); } + /** + * @param boolean $noMigrationException Throw an exception or not if no migration is found. Mostly for Continuous Integration. + */ + public function setNoMigrationException($noMigrationException = false) + { + $this->noMigrationException = $noMigrationException; + } + /** * Run a migration to the current version or the given target version. * @@ -151,7 +165,7 @@ public function migrate($to = null, $dryRun = false, $timeAllQueries = false) /** * If there are no migrations to execute throw an exception. */ - if (empty($migrationsToExecute)) { + if (empty($migrationsToExecute) && !$this->noMigrationException) { throw MigrationException::noMigrationsToExecute(); } diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php index 056705bc64..44815a4829 100644 --- a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php +++ b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/MigrateCommand.php @@ -45,6 +45,7 @@ protected function configure() ->addOption('write-sql', null, InputOption::VALUE_NONE, 'The path to output the migration SQL file instead of executing it.') ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run.') ->addOption('query-time', null, InputOption::VALUE_NONE, 'Time all the queries individually.') + ->addOption('allow-no-migration', null, InputOption::VALUE_NONE, 'Don\'t throw an exception if no migration is available (CI).') ->setHelp(<<%command.name% command executes a migration to a specified version or the latest available version: @@ -138,6 +139,7 @@ public function execute(InputInterface $input, OutputInterface $output) } } + $migration->setNoMigrationException($input->getOption('allow-no-migration')); $sql = $migration->migrate($version, $dryRun, $timeAllqueries); if (empty($sql)) { diff --git a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php index 61076d41b5..593831ed5a 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php @@ -61,6 +61,14 @@ public function testMigrateWithNoMigrationsThrowsException() $migration->migrate(); } + public function testMigrateWithNoMigrationsDontThrowsExceptionIfContiniousIntegrationOption() + { + $migration = new Migration($this->config); + + $migration->setNoMigrationException(true); + $migration->migrate(); + } + /** * @param $to *