Skip to content

Commit e73d3c3

Browse files
authored
Merge pull request #1865 from atishamte/migration
MigrationRunner issue with definition resolved
2 parents f577d49 + 36390ae commit e73d3c3

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

system/Database/MigrationRunner.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function __construct(BaseConfig $config, $db = null)
196196
* @param string|null $namespace
197197
* @param string|null $group
198198
*
199-
* @return mixed TRUE if no migrations are found, current version string on success, FALSE on failure
199+
* @return mixed Current version string on success, FALSE on failure or no migrations are found
200200
* @throws ConfigException
201201
*/
202202
public function version(string $targetVersion, string $namespace = null, string $group = null)
@@ -253,12 +253,14 @@ public function version(string $targetVersion, string $namespace = null, string
253253
$this->checkMigrations($migrations, $method, $targetVersion);
254254

255255
// loop migration for each namespace (module)
256+
257+
$migrationStatus = false;
256258
foreach ($migrations as $version => $migration)
257259
{
258260
// Only include migrations within the scoop
259-
if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion)
260-
)
261+
if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion))
261262
{
263+
$migrationStatus = false;
262264
include_once $migration->path;
263265
// Get namespaced class name
264266
$class = $this->namespace . '\Database\Migrations\Migration_' . ($migration->name);
@@ -288,10 +290,12 @@ public function version(string $targetVersion, string $namespace = null, string
288290
{
289291
$this->removeHistory($migration->version);
290292
}
293+
294+
$migrationStatus = true;
291295
}
292296
}
293297

294-
return true;
298+
return ($migrationStatus) ? $targetVersion : false;
295299
}
296300

297301
//--------------------------------------------------------------------

tests/system/Database/Migrations/MigrationRunnerTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ public function testVersionThrowsMigrationGapException()
231231

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

234-
$this->assertEquals($version, '002');
234+
$this->assertFalse($version);
235235
}
236236

237-
public function testVersionReturnsTrueWhenNothingToDo()
237+
public function testVersionReturnsFalseWhenNothingToDo()
238238
{
239239
$config = $this->config;
240240
$config->type = 'sequential';
@@ -246,7 +246,7 @@ public function testVersionReturnsTrueWhenNothingToDo()
246246

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

249-
$this->assertTrue($version);
249+
$this->assertFalse($version);
250250
}
251251

252252
/**
@@ -266,7 +266,7 @@ public function testVersionWithNoClassInFile()
266266

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

269-
$this->assertEquals('001', $version);
269+
$this->assertFalse($version);
270270
}
271271

272272
public function testVersionReturnsUpDownSuccess()
@@ -290,7 +290,7 @@ public function testVersionReturnsUpDownSuccess()
290290

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

293-
$this->assertTrue($version);
293+
$this->assertEquals('000', $version);
294294
$this->assertFalse(db_connect()->tableExists('foo'));
295295
}
296296

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

317+
public function testVersionReturnsDownSuccess()
318+
{
319+
$config = $this->config;
320+
$config->type = 'sequential';
321+
$runner = new MigrationRunner($config);
322+
$runner->setSilent(false);
323+
324+
$runner = $runner->setPath($this->start);
325+
326+
vfsStream::copyFromFileSystem(
327+
TESTPATH . '_support/Database/SupportMigrations',
328+
$this->root
329+
);
330+
331+
$version = $runner->version(0);
332+
333+
$this->assertEquals('000', $version);
334+
$this->assertFalse(db_connect()->tableExists('foo'));
335+
}
336+
317337
public function testCurrentSuccess()
318338
{
319339
$config = $this->config;

user_guide_src/source/dbmgmt/migration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ Class Reference
344344
:param mixed $namespace: application namespace, if null (App) namespace will be used.
345345
:param mixed $group: database group name, if null default database group will be used.
346346
:param mixed $target_version: Migration version to process
347-
:returns: TRUE if no migrations are found, current version string on success, FALSE on failure
347+
:returns: Current version string on success, FALSE on failure or no migrations are found
348348
:rtype: mixed
349349

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

0 commit comments

Comments
 (0)