Skip to content

Commit

Permalink
Feature: Allow users to disable the overwriting of the default connec…
Browse files Browse the repository at this point in the history
…tion and adjust hooks to perform properly on queues (#279)

* Adjust hooks to perform properly on queues

* Linting

* Include tests

* Adjust to comply with PHP 8.0
  • Loading branch information
ArlonAntonius committed Oct 26, 2023
1 parent ea9a166 commit 8dc12f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
24 changes: 10 additions & 14 deletions src/Hooks/Migration/Hooks/MigratesHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace Tenancy\Hooks\Migration\Hooks;

use Illuminate\Database\Migrations\Migrator;
use Tenancy\Affects\Connections\Contracts\ResolvesConnections;
use Tenancy\Facades\Tenancy;
use Tenancy\Hooks\Migration\Events\ConfigureMigrations;
Expand All @@ -25,12 +24,8 @@

class MigratesHook extends ConfigurableHook
{
public Migrator $migrator;

public string $connection;

public ResolvesConnections $resolver;

public $action;

public int $priority = -50;
Expand All @@ -39,11 +34,9 @@ class MigratesHook extends ConfigurableHook

public function __construct()
{
$this->migrator = resolve('migrator');
$this->connection = Tenancy::getTenantConnectionName();
$this->resolver = resolve(ResolvesConnections::class);

$this->paths = $this->migrator->paths();
$this->paths = resolve('migrator')->paths();
}

public function for($event): static
Expand All @@ -60,17 +53,20 @@ public function for($event): static
public function fire(): void
{
$db = resolve('db');
$migrator = resolve('migrator');
$resolver = resolve(ResolvesConnections::class);

$default = $db->getDefaultConnection();

$this->resolver->__invoke($this->event->tenant, $this->connection);
$this->migrator->setConnection($this->connection);
$resolver->__invoke($this->event->tenant, $this->connection);
$migrator->setConnection($this->connection);

if (!$this->migrator->repositoryExists()) {
$this->migrator->getRepository()->createRepository();
if (!$migrator->repositoryExists()) {
$migrator->getRepository()->createRepository();
}
call_user_func([$this->migrator, $this->action], $this->paths);
call_user_func([$migrator, $this->action], $this->paths);

$this->resolver->__invoke(null, $this->connection);
$resolver->__invoke(null, $this->connection);
$db->setDefaultConnection($default);
}
}
27 changes: 20 additions & 7 deletions src/Hooks/Migration/Hooks/SeedsHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ class SeedsHook extends ConfigurableHook
{
public string $connection;

public ResolvesConnections $resolver;

public ?string $action = null;

public int $priority = -40;

protected bool $replaceDefaultConnection = true;

/** @var array|string[] */
public array $seeds = [];

public function __construct()
{
$this->connection = Tenancy::getTenantConnectionName();
$this->resolver = resolve(ResolvesConnections::class);
}

public function for($event): static
Expand All @@ -54,20 +53,31 @@ public function for($event): static
return $this;
}

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

return $this;
}

public function fire(): void
{
if (empty($this->seeds)) {
return;
}

$db = resolve('db');
$resolver = resolve(ResolvesConnections::class);

$default = $db->getDefaultConnection();

Model::unguard();

$this->resolver->__invoke($this->event->tenant, $this->connection);
$db->setDefaultConnection($this->connection);
$resolver->__invoke($this->event->tenant, $this->connection);

if ($this->replaceDefaultConnection) {
$db->setDefaultConnection($this->connection);
}

foreach ($this->seeds as $seed) {
/** @var Seeder $seeder */
Expand All @@ -77,8 +87,11 @@ public function fire(): void
$seeder();
}

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

if ($this->replaceDefaultConnection) {
$db->setDefaultConnection($default);
}

Model::reguard();
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Hooks/Migration/Unit/ConfiguresSeedsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,25 @@ public function it_can_add_seeds($tenantEvent)
$this->hook->seeds
);
}

/**
* @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 8dc12f3

Please sign in to comment.