From 84665bea3a9ab04d6664ab34cba532dd72fbcfc1 Mon Sep 17 00:00:00 2001 From: michalsn Date: Sat, 5 Oct 2019 18:20:12 +0200 Subject: [PATCH 1/2] Use DBGroup from migration class if defined --- system/Database/Migration.php | 4 +-- system/Database/MigrationRunner.php | 41 ++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/system/Database/Migration.php b/system/Database/Migration.php index bfa31a96d25e..01ae82f0a204 100644 --- a/system/Database/Migration.php +++ b/system/Database/Migration.php @@ -74,7 +74,7 @@ abstract class Migration */ public function __construct(Forge $forge = null) { - $this->forge = ! is_null($forge) ? $forge : \Config\Database::forge($this->DBGroup); + $this->forge = ! is_null($forge) ? $forge : \Config\Database::forge($this->DBGroup ?? config('Database')->defaultGroup); $this->db = $this->forge->getConnection(); } @@ -86,7 +86,7 @@ public function __construct(Forge $forge = null) * * @return string */ - public function getDBGroup(): string + public function getDBGroup(): ?string { return $this->DBGroup; } diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 25fe423948d3..b61749df635a 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -128,6 +128,20 @@ class MigrationRunner */ protected $path; + /** + * The database Group filter. + * + * @var string + */ + protected $groupFilter; + + /** + * Used to skip current migration. + * + * @var boolean + */ + protected $groupSkip = false; + //-------------------------------------------------------------------- /** @@ -180,6 +194,7 @@ public function latest(string $group = null) // Set database group if not null if (! is_null($group)) { + $this->groupFilter = $group; $this->setGroup($group); } @@ -206,6 +221,12 @@ public function latest(string $group = null) { if ($this->migrate('up', $migration)) { + if ($this->groupSkip === true) + { + $this->groupSkip = false; + continue; + } + $this->addHistory($migration, $batch); } // If a migration failed then try to back out what was done @@ -370,6 +391,7 @@ public function force(string $path, string $namespace, string $group = null) // Set database group if not null if (! is_null($group)) { + $this->groupFilter = $group; $this->setGroup($group); } @@ -406,11 +428,13 @@ public function force(string $path, string $namespace, string $group = null) // Start a new batch $batch = $this->getLastBatch() + 1; - if ($this->migrate('up', $migration)) + if ($this->migrate('up', $migration) && $this->groupSkip === false) { $this->addHistory($migration, $batch); return true; } + + $this->groupSkip = false; } // down @@ -991,8 +1015,19 @@ protected function migrate($direction, $migration): bool throw new \RuntimeException($message); } - // Forcing migration to selected database group - $instance = new $class(\Config\Database::forge($this->group)); + // Initialize migration + $instance = new $class(); + // Determine DBGroup to use + $group = $instance->getDBGroup() ?? config('Database')->defaultGroup; + + // Skip if migration if group filteing was set + if ($direction === 'up' && ! is_null($this->groupFilter) && $this->groupFilter !== $group) + { + $this->groupSkip = true; + return true; + } + + $this->setGroup($group); if (! is_callable([$instance, $direction])) { From 4908d4399e2eb717e7c8b8ac3deead02b18c667c Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 7 Oct 2019 19:32:46 +0200 Subject: [PATCH 2/2] [ci skip] comment update --- system/Database/MigrationRunner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index b61749df635a..1dbae84336c6 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -1020,7 +1020,7 @@ protected function migrate($direction, $migration): bool // Determine DBGroup to use $group = $instance->getDBGroup() ?? config('Database')->defaultGroup; - // Skip if migration if group filteing was set + // Skip migration if group filtering was set if ($direction === 'up' && ! is_null($this->groupFilter) && $this->groupFilter !== $group) { $this->groupSkip = true;