Skip to content

Commit

Permalink
Feature: Allow to disable switching of default connection and restore…
Browse files Browse the repository at this point in the history
… "current" connection (#285)

* Adjust hooks to perform properly on queues

* Linting

* Include tests

* Adjust to comply with PHP 8.0

* Return the connection to the tenant connection and replace default connection

* Add a test
  • Loading branch information
ArlonAntonius committed Oct 31, 2023
1 parent 8dc12f3 commit 5c49cd5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Hooks/Migration/Hooks/MigratesHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MigratesHook extends ConfigurableHook

public int $priority = -50;

protected bool $replaceDefaultConnection = true;

public array $paths;

public function __construct()
Expand All @@ -50,6 +52,13 @@ public function for($event): static
return $this;
}

public function withDefaultConnection(bool $replace = true): static
{
$this->replaceDefaultConnection = $replace;

return $this;
}

public function fire(): void
{
$db = resolve('db');
Expand All @@ -66,7 +75,10 @@ public function fire(): void
}
call_user_func([$migrator, $this->action], $this->paths);

$resolver->__invoke(null, $this->connection);
$db->setDefaultConnection($default);
$resolver->__invoke(Tenancy::getTenant(), $this->connection);

if ($this->replaceDefaultConnection) {
$db->setDefaultConnection($default);
}
}
}
21 changes: 21 additions & 0 deletions tests/Hooks/Migration/Unit/ConfiguresMigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,25 @@ public function it_can_clear_paths_from_the_migrator($tenantEvent)
$this->hook->paths
);
}

/**
* @dataProvider tenantEventsProvider
*
* @test */
public function it_can_decide_whether_to_replace_the_default($tenantEvent)
{
$this->events->listen($this->eventClass, function ($event) {
$event->hook->withDefaultConnection(false);
});

$this->hook->for(new $tenantEvent($this->mockTenant()));

$reflection = new \ReflectionClass($this->hook);
$property = $reflection->getProperty('replaceDefaultConnection');
$property->setAccessible(true);

$this->assertFalse(
$property->getValue($this->hook)
);
}
}

0 comments on commit 5c49cd5

Please sign in to comment.