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

MigrationRunner issue with definition resolved #1865

Merged
merged 7 commits into from
Mar 26, 2019
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
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 @@ -343,7 +343,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