Skip to content

Commit

Permalink
adding an option to not throw an exception if no migration is found
Browse files Browse the repository at this point in the history
This is mostly usefull for Continuous integration system
  • Loading branch information
mikeSimonson committed Aug 27, 2015
1 parent 27edc38 commit a9ec86f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/Doctrine/DBAL/Migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Migration
*/
private $configuration;

/**
* @var boolean
*/
private $noMigrationException;

/**
* Construct a Migration instance
*
Expand All @@ -52,6 +57,7 @@ public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
$this->outputWriter = $configuration->getOutputWriter();
$this->noMigrationException = false;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<<<EOT
The <info>%command.name%</info> command executes a migration to a specified version or the latest available version:
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function testMigrateWithNoMigrationsThrowsException()
$migration->migrate();
}

public function testMigrateWithNoMigrationsDontThrowsExceptionIfContiniousIntegrationOption()
{
$migration = new Migration($this->config);

$migration->setNoMigrationException(true);
$migration->migrate();
}

/**
* @param $to
*
Expand Down

0 comments on commit a9ec86f

Please sign in to comment.