From e0c6f161c1d6581dabc7a491dde42a9fa4445332 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 24 Mar 2019 22:33:48 +0530 Subject: [PATCH 1/7] Migration check --- system/Database/MigrationRunner.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index b0de8c69fe66..faebc61e2da9 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -253,12 +253,15 @@ 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) ) { + $migrationStatus = false; include_once $migration->path; // Get namespaced class name $class = $this->namespace . '\Database\Migrations\Migration_' . ($migration->name); @@ -288,10 +291,12 @@ public function version(string $targetVersion, string $namespace = null, string { $this->removeHistory($migration->version); } + + $migrationStatus = true; } } - return true; + return ($migrationStatus) ? $targetVersion : false; } //-------------------------------------------------------------------- From c1b3b5db1279a28508c11b9d56ddbc340e820f0d Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 24 Mar 2019 23:14:56 +0530 Subject: [PATCH 2/7] Migration check --- system/Database/MigrationRunner.php | 6 ++++-- tests/system/Database/Migrations/MigrationRunnerTest.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index faebc61e2da9..35da4d3bed28 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -254,7 +254,8 @@ public function version(string $targetVersion, string $namespace = null, string // loop migration for each namespace (module) - $migrationStatus = false; + $migrationStatus = true; + $executedVersion = true; foreach ($migrations as $version => $migration) { // Only include migrations within the scoop @@ -262,6 +263,7 @@ public function version(string $targetVersion, string $namespace = null, string ) { $migrationStatus = false; + $executedVersion = $version; include_once $migration->path; // Get namespaced class name $class = $this->namespace . '\Database\Migrations\Migration_' . ($migration->name); @@ -296,7 +298,7 @@ public function version(string $targetVersion, string $namespace = null, string } } - return ($migrationStatus) ? $targetVersion : false; + return ($migrationStatus) ? $executedVersion : false; } //-------------------------------------------------------------------- diff --git a/tests/system/Database/Migrations/MigrationRunnerTest.php b/tests/system/Database/Migrations/MigrationRunnerTest.php index b494936821aa..bb8097db2e07 100644 --- a/tests/system/Database/Migrations/MigrationRunnerTest.php +++ b/tests/system/Database/Migrations/MigrationRunnerTest.php @@ -290,7 +290,7 @@ public function testVersionReturnsUpDownSuccess() $version = $runner->version(0); - $this->assertTrue($version); + $this->assertEquals('000', $version); $this->assertFalse(db_connect()->tableExists('foo')); } From 9ddcd80a50c43770005237d01fa452d907b67233 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 24 Mar 2019 23:36:15 +0530 Subject: [PATCH 3/7] Migration check --- system/Database/MigrationRunner.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 35da4d3bed28..20fb440c05d4 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -259,8 +259,7 @@ public function version(string $targetVersion, string $namespace = null, string 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; $executedVersion = $version; @@ -298,7 +297,7 @@ public function version(string $targetVersion, string $namespace = null, string } } - return ($migrationStatus) ? $executedVersion : false; + return ($migrationStatus) ? (($targetVersion == '0') ? '0' : $executedVersion) : false; } //-------------------------------------------------------------------- From bfff40f5f31f9abb8f482b3e08f692ca08a10c68 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 25 Mar 2019 22:14:53 +0530 Subject: [PATCH 4/7] Migration check --- system/Database/MigrationRunner.php | 6 ++---- .../system/Database/Migrations/MigrationRunnerTest.php | 10 +++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 20fb440c05d4..9bfd2808fcb6 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -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) @@ -255,14 +255,12 @@ public function version(string $targetVersion, string $namespace = null, string // loop migration for each namespace (module) $migrationStatus = true; - $executedVersion = true; foreach ($migrations as $version => $migration) { // Only include migrations within the scoop if (($method === 'up' && $version > $currentVersion && $version <= $targetVersion) || ( $method === 'down' && $version <= $currentVersion && $version > $targetVersion)) { $migrationStatus = false; - $executedVersion = $version; include_once $migration->path; // Get namespaced class name $class = $this->namespace . '\Database\Migrations\Migration_' . ($migration->name); @@ -297,7 +295,7 @@ public function version(string $targetVersion, string $namespace = null, string } } - return ($migrationStatus) ? (($targetVersion == '0') ? '0' : $executedVersion) : false; + return ($migrationStatus) ? $targetVersion : false; } //-------------------------------------------------------------------- diff --git a/tests/system/Database/Migrations/MigrationRunnerTest.php b/tests/system/Database/Migrations/MigrationRunnerTest.php index bb8097db2e07..0f1c5cb1e8fd 100644 --- a/tests/system/Database/Migrations/MigrationRunnerTest.php +++ b/tests/system/Database/Migrations/MigrationRunnerTest.php @@ -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'; @@ -246,7 +246,7 @@ public function testVersionReturnsTrueWhenNothingToDo() $version = $runner->version(0); - $this->assertTrue($version); + $this->assertFalse($version); } /** @@ -266,7 +266,7 @@ public function testVersionWithNoClassInFile() $version = $runner->version(1); - $this->assertEquals('001', $version); + $this->assertFalse($version); } public function testVersionReturnsUpDownSuccess() @@ -290,7 +290,7 @@ public function testVersionReturnsUpDownSuccess() $version = $runner->version(0); - $this->assertEquals('000', $version); + $this->assertFalse($version); $this->assertFalse(db_connect()->tableExists('foo')); } From ea580d2fba523dd9a6f0c6c85bb025b4dde33518 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 25 Mar 2019 22:34:01 +0530 Subject: [PATCH 5/7] Migration check --- system/Database/MigrationRunner.php | 2 +- tests/system/Database/Migrations/MigrationRunnerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 9bfd2808fcb6..f51dfe531030 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -254,7 +254,7 @@ public function version(string $targetVersion, string $namespace = null, string // loop migration for each namespace (module) - $migrationStatus = true; + $migrationStatus = false; foreach ($migrations as $version => $migration) { // Only include migrations within the scoop diff --git a/tests/system/Database/Migrations/MigrationRunnerTest.php b/tests/system/Database/Migrations/MigrationRunnerTest.php index 0f1c5cb1e8fd..af07bff655b0 100644 --- a/tests/system/Database/Migrations/MigrationRunnerTest.php +++ b/tests/system/Database/Migrations/MigrationRunnerTest.php @@ -290,7 +290,7 @@ public function testVersionReturnsUpDownSuccess() $version = $runner->version(0); - $this->assertFalse($version); + $this->assertEquals('000', $version); $this->assertFalse(db_connect()->tableExists('foo')); } From 06e5b4264a7b396a895a61abbd2c6d406f03eee2 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 25 Mar 2019 22:53:04 +0530 Subject: [PATCH 6/7] Migration check --- .../Migrations/MigrationRunnerTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/system/Database/Migrations/MigrationRunnerTest.php b/tests/system/Database/Migrations/MigrationRunnerTest.php index af07bff655b0..8e6efce5b522 100644 --- a/tests/system/Database/Migrations/MigrationRunnerTest.php +++ b/tests/system/Database/Migrations/MigrationRunnerTest.php @@ -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; From 36390aeb060a9d5884c1d058e9be7b1ef31069b0 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Mon, 25 Mar 2019 23:41:24 +0530 Subject: [PATCH 7/7] Updated migration user guide documentation --- user_guide_src/source/dbmgmt/migration.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/dbmgmt/migration.rst b/user_guide_src/source/dbmgmt/migration.rst index ebb329250dc3..7dea1c7e0d35 100644 --- a/user_guide_src/source/dbmgmt/migration.rst +++ b/user_guide_src/source/dbmgmt/migration.rst @@ -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