Skip to content

Commit

Permalink
Merge pull request #2303 from michalsn/migration_groups
Browse files Browse the repository at this point in the history
Use DBGroup variable from migration class if defined
  • Loading branch information
lonnieezell authored Oct 8, 2019
2 parents 10755eb + 4908d43 commit 5295d37
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions system/Database/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -86,7 +86,7 @@ public function __construct(Forge $forge = null)
*
* @return string
*/
public function getDBGroup(): string
public function getDBGroup(): ?string
{
return $this->DBGroup;
}
Expand Down
41 changes: 38 additions & 3 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 migration if group filtering 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]))
{
Expand Down

0 comments on commit 5295d37

Please sign in to comment.