Skip to content

Commit

Permalink
Merge pull request #1865 from atishamte/migration
Browse files Browse the repository at this point in the history
MigrationRunner issue with definition resolved
  • Loading branch information
lonnieezell authored Mar 26, 2019
2 parents f577d49 + 36390ae commit e73d3c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
12 changes: 8 additions & 4 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function __construct(BaseConfig $config, $db = null)
* @param string|null $namespace
* @param string|null $group
*
* @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure
* @return mixed Current version string on success, FALSE on failure or no migrations are found
* @throws ConfigException
*/
public function version(string $targetVersion, string $namespace = null, string $group = null)
Expand Down Expand Up @@ -253,12 +253,14 @@ public function version(string $targetVersion, string $namespace = null, string
$this->checkMigrations($migrations, $method, $targetVersion);

// loop migration for each namespace (module)

$migrationStatus = false;
foreach ($migrations as $version => $migration)
{
// Only include migrations within the scoop
if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion)
)
if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion))
{
$migrationStatus = false;
include_once $migration->path;
// Get namespaced class name
$class = $this->namespace . '\Database\Migrations\Migration_' . ($migration->name);
Expand Down Expand Up @@ -288,10 +290,12 @@ public function version(string $targetVersion, string $namespace = null, string
{
$this->removeHistory($migration->version);
}

$migrationStatus = true;
}
}

return true;
return ($migrationStatus) ? $targetVersion : false;
}

//--------------------------------------------------------------------
Expand Down
30 changes: 25 additions & 5 deletions tests/system/Database/Migrations/MigrationRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public function testVersionThrowsMigrationGapException()

$version = $runner->version(0);

$this->assertEquals($version, '002');
$this->assertFalse($version);
}

public function testVersionReturnsTrueWhenNothingToDo()
public function testVersionReturnsFalseWhenNothingToDo()
{
$config = $this->config;
$config->type = 'sequential';
Expand All @@ -246,7 +246,7 @@ public function testVersionReturnsTrueWhenNothingToDo()

$version = $runner->version(0);

$this->assertTrue($version);
$this->assertFalse($version);
}

/**
Expand All @@ -266,7 +266,7 @@ public function testVersionWithNoClassInFile()

$version = $runner->version(1);

$this->assertEquals('001', $version);
$this->assertFalse($version);
}

public function testVersionReturnsUpDownSuccess()
Expand All @@ -290,7 +290,7 @@ public function testVersionReturnsUpDownSuccess()

$version = $runner->version(0);

$this->assertTrue($version);
$this->assertEquals('000', $version);
$this->assertFalse(db_connect()->tableExists('foo'));
}

Expand All @@ -314,6 +314,26 @@ public function testLatestSuccess()
$this->assertTrue(db_connect()->tableExists('foo'));
}

public function testVersionReturnsDownSuccess()
{
$config = $this->config;
$config->type = 'sequential';
$runner = new MigrationRunner($config);
$runner->setSilent(false);

$runner = $runner->setPath($this->start);

vfsStream::copyFromFileSystem(
TESTPATH . '_support/Database/SupportMigrations',
$this->root
);

$version = $runner->version(0);

$this->assertEquals('000', $version);
$this->assertFalse(db_connect()->tableExists('foo'));
}

public function testCurrentSuccess()
{
$config = $this->config;
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/dbmgmt/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Class Reference
:param mixed $namespace: application namespace, if null (App) namespace will be used.
:param mixed $group: database group name, if null default database group will be used.
:param mixed $target_version: Migration version to process
:returns: TRUE if no migrations are found, current version string on success, FALSE on failure
:returns: Current version string on success, FALSE on failure or no migrations are found
:rtype: mixed

Version can be used to roll back changes or step forwards programmatically to
Expand Down

0 comments on commit e73d3c3

Please sign in to comment.