Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with some method when the migrations were not loaded #421

Merged
merged 1 commit into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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