From 916bee27bae598fbaf90bc21117fb6b2f76bba1c Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 14 Jan 2016 22:19:52 +0100 Subject: [PATCH] Fix issue with some method call order when the migrations were not loaded 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. --- .../Configuration/Configuration.php | 31 +++++++++++++++++++ .../DBAL/Migrations/Tests/MigrationTest.php | 2 +- .../Migrations/Tests/MigrationTestCase.php | 2 +- .../Stub/migration-empty-folder/.gitkeep | 0 .../Console/Command/MigrationStatusTest.php | 20 ++++++++++-- 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/DBAL/Migrations/Tests/Stub/migration-empty-folder/.gitkeep diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php index a8287ea4f1..eee5a53fe2 100644 --- a/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Configuration.php @@ -382,6 +382,8 @@ public function setMigrationsFinder(MigrationFinderInterface $finder) */ public function registerMigrationsFromDirectory($path) { + $this->validate(); + return $this->registerMigrations($this->findMigrations($path)); } @@ -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]); } @@ -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(); } @@ -532,6 +543,10 @@ public function getCurrentVersion() { $this->createMigrationTable(); + if (empty($this->migrations)) { + $this->registerMigrationsFromDirectory($this->getMigrationsDirectory()); + } + $where = null; if (!empty($this->migrations)) { $migratedVersions = []; @@ -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); @@ -652,6 +671,10 @@ public function getNumberOfExecutedMigrations() */ public function getNumberOfAvailableMigrations() { + if (empty($this->migrations)) { + $this->registerMigrationsFromDirectory($this->getMigrationsDirectory()); + } + return count($this->migrations); } @@ -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); @@ -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)); diff --git a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php index 593831ed5a..f7d3371a59 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTest.php @@ -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\\'); } diff --git a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php index b0860bc9d3..613783f074 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/MigrationTestCase.php @@ -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; diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Stub/migration-empty-folder/.gitkeep b/tests/Doctrine/DBAL/Migrations/Tests/Stub/migration-empty-folder/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/Doctrine/DBAL/Migrations/Tests/Tools/Console/Command/MigrationStatusTest.php b/tests/Doctrine/DBAL/Migrations/Tests/Tools/Console/Command/MigrationStatusTest.php index dfaf596a14..726ae40455 100644 --- a/tests/Doctrine/DBAL/Migrations/Tests/Tools/Console/Command/MigrationStatusTest.php +++ b/tests/Doctrine/DBAL/Migrations/Tests/Tools/Console/Command/MigrationStatusTest.php @@ -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. */ @@ -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 @@ -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()) @@ -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())