Skip to content

Commit

Permalink
Fix issue with some method call order when the migrations were not lo…
Browse files Browse the repository at this point in the history
…aded

Previously you needed to make sure manually that registerMigrations was
called before to make any call to a method that was using the migrations
loaded.

This might cause performance issue in some edge case but it's unlikely.
Unfortunately in the current desgin I don't see any other solution.
  • Loading branch information
mikeSimonson committed Jan 14, 2016
1 parent 228133d commit 916bee2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
31 changes: 31 additions & 0 deletions lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public function setMigrationsFinder(MigrationFinderInterface $finder)
*/
public function registerMigrationsFromDirectory($path)
{
$this->validate();

return $this->registerMigrations($this->findMigrations($path));
}

Expand Down Expand Up @@ -468,6 +470,10 @@ public function getVersion($version)
*/
public function hasVersion($version)
{
if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

return isset($this->migrations[$version]);
}

Expand Down Expand Up @@ -516,6 +522,11 @@ public function getMigratedVersions()
public function getAvailableVersions()
{
$availableVersions = [];

if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

foreach ($this->migrations as $migration) {
$availableVersions[] = $migration->getVersion();
}
Expand All @@ -532,6 +543,10 @@ public function getCurrentVersion()
{
$this->createMigrationTable();

if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

$where = null;
if (!empty($this->migrations)) {
$migratedVersions = [];
Expand Down Expand Up @@ -582,6 +597,10 @@ public function getNextVersion()
*/
public function getRelativeVersion($version, $delta)
{
if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

$versions = array_keys($this->migrations);
array_unshift($versions, 0);
$offset = array_search($version, $versions);
Expand Down Expand Up @@ -652,6 +671,10 @@ public function getNumberOfExecutedMigrations()
*/
public function getNumberOfAvailableMigrations()
{
if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

return count($this->migrations);
}

Expand All @@ -662,6 +685,10 @@ public function getNumberOfAvailableMigrations()
*/
public function getLatestVersion()
{
if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

$versions = array_keys($this->migrations);
$latest = end($versions);

Expand Down Expand Up @@ -710,6 +737,10 @@ public function createMigrationTable()
*/
public function getMigrationsToExecute($direction, $to)
{
if (empty($this->migrations)) {
$this->registerMigrationsFromDirectory($this->getMigrationsDirectory());
}

if ($direction === Version::DIRECTION_DOWN) {
if (count($this->migrations)) {
$allVersions = array_reverse(array_keys($this->migrations));
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MigrationTest extends MigrationTestCase
public function setUp()
{
$this->config = new Configuration($this->getSqliteConnection());
$this->config->setMigrationsDirectory(\sys_get_temp_dir());
$this->config->setMigrationsDirectory(__DIR__ . DIRECTORY_SEPARATOR . 'Stub/migration-empty-folder');
$this->config->setMigrationsNamespace('DoctrineMigrations\\');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getSqliteConnection()
public function getSqliteConfiguration()
{
$config = new Configuration($this->getSqliteConnection());
$config->setMigrationsDirectory(\sys_get_temp_dir());
$config->setMigrationsDirectory(__DIR__ . '/Stub/migration-empty-folder');
$config->setMigrationsNamespace('DoctrineMigrations');

return $config;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

class MigrationStatusTest extends MigrationTestCase
{

private $migrationDirectory;

public function __construct()
{
parent::__construct(null, [], null);
$this->migrationDirectory = __DIR__ . '/../../../Stub/migration-empty-folder';
}

/**
* Tests the display of the previous/current/next/latest versions.
*/
Expand Down Expand Up @@ -46,7 +55,7 @@ protected function assertVersion($alias, $version, $label, $output)

$configuration = $this->getMockBuilder('Doctrine\DBAL\Migrations\Configuration\Configuration')
->setConstructorArgs([$this->getSqliteConnection()])
->setMethods(['resolveVersionAlias', 'getDateTime'])
->setMethods(['resolveVersionAlias', 'getDateTime', 'getAvailableVersions'])
->getMock();

$configuration
Expand All @@ -61,8 +70,13 @@ protected function assertVersion($alias, $version, $label, $output)
->method('getDateTime')
->will($this->returnValue('FORMATTED'));

$configuration
->expects($this->any())
->method('getAvailableVersions')
->will($this->returnValue([]));

$configuration->setMigrationsNamespace('DoctrineMigrations');
$configuration->setMigrationsDirectory(sys_get_temp_dir());
$configuration->setMigrationsDirectory($this->migrationDirectory);

$command
->expects($this->once())
Expand Down Expand Up @@ -118,7 +132,7 @@ public function testIfAmountNewMigrationsIsCorrectWithUnavailableMigrations()
->will($this->returnValue(1239));

$configuration->setMigrationsNamespace('DoctrineMigrations');
$configuration->setMigrationsDirectory(sys_get_temp_dir());
$configuration->setMigrationsDirectory($this->migrationDirectory);

$command
->expects($this->once())
Expand Down

0 comments on commit 916bee2

Please sign in to comment.