mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 14:34:04 +00:00
runForMultiple can scope pending tenants
This commit is contained in:
parent
b73d047a39
commit
0a37eb487a
9 changed files with 47 additions and 11 deletions
|
|
@ -63,6 +63,6 @@ class Migrate extends MigrateCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseMigrated($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ final class MigrateFresh extends Command
|
|||
'--tenants' => [$tenant->getTenantKey()],
|
||||
'--force' => true,
|
||||
]);
|
||||
});
|
||||
}, $this->withPending());
|
||||
|
||||
$this->info('Done.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,6 @@ class Rollback extends RollbackCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseRolledBack($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,6 +52,6 @@ class Run extends Command
|
|||
|
||||
// Run command
|
||||
$this->call($this->argument('commandname'), array_merge($arguments, $options));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,6 @@ class Seed extends SeedCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseSeeded($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ trait HasATenantsOption
|
|||
{
|
||||
return array_merge([
|
||||
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
|
||||
['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query', null],
|
||||
], parent::getOptions());
|
||||
}
|
||||
|
||||
|
|
@ -26,6 +27,11 @@ trait HasATenantsOption
|
|||
->cursor();
|
||||
}
|
||||
|
||||
protected function withPending(): bool
|
||||
{
|
||||
return $this->option('with-pending');
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant;
|
|||
*/
|
||||
class TenantCollection extends Collection
|
||||
{
|
||||
public function runForEach(callable $callable): self
|
||||
public function runForEach(callable $callable, bool $withPending = null): self
|
||||
{
|
||||
tenancy()->runForMultiple($this->items, $callable);
|
||||
tenancy()->runForMultiple($this->items, $callable, $withPending);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
use Illuminate\Support\Traits\Macroable;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Database\Concerns\PendingScope;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById;
|
||||
|
||||
class Tenancy
|
||||
|
|
@ -135,10 +136,15 @@ class Tenancy
|
|||
* @param callable $callback
|
||||
* @return void
|
||||
*/
|
||||
public function runForMultiple($tenants, callable $callback)
|
||||
public function runForMultiple($tenants, callable $callback, bool $withPending = null)
|
||||
{
|
||||
$query = $this->model()->newQuery();
|
||||
|
||||
if (is_bool($withPending) && $this->model()::hasGlobalScope(PendingScope::class)){
|
||||
$query->withPending($withPending);
|
||||
}
|
||||
// Convert null to all tenants
|
||||
$tenants = is_null($tenants) ? $this->model()->cursor() : $tenants;
|
||||
$tenants = is_null($tenants) ? $query->cursor() : $tenants;
|
||||
|
||||
// Convert incrementing int ids to strings
|
||||
$tenants = is_int($tenants) ? (string) $tenants : $tenants;
|
||||
|
|
@ -146,8 +152,8 @@ class Tenancy
|
|||
// Wrap string in array
|
||||
$tenants = is_string($tenants) ? [$tenants] : $tenants;
|
||||
|
||||
// Use all tenants if $tenants is falsey
|
||||
$tenants = $tenants ?: $this->model()->cursor();
|
||||
// Use all tenants if $tenants is false
|
||||
$tenants = $tenants ?: $query->cursor();
|
||||
|
||||
$originalTenant = $this->tenant;
|
||||
|
||||
|
|
|
|||
|
|
@ -169,4 +169,28 @@ class PendingTenantsTest extends TestCase
|
|||
Event::assertDispatched(PullingPendingTenant::class);
|
||||
Event::assertDispatched(PendingTenantPulled::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tenancy_run_for_multiple_can_scope_pending_tenants()
|
||||
{
|
||||
config(['tenancy.pending.include_in_queries' => false]);
|
||||
|
||||
Tenant::createPending();
|
||||
Tenant::create();
|
||||
|
||||
$executedCount = 0;
|
||||
tenancy()->runForMultiple([], function () use (&$executedCount){
|
||||
$executedCount++;
|
||||
}, false);
|
||||
|
||||
self::assertEquals(1, $executedCount);
|
||||
|
||||
$executedCount = 0;
|
||||
|
||||
tenancy()->runForMultiple([], function () use (&$executedCount){
|
||||
$executedCount++;
|
||||
}, true);
|
||||
|
||||
self::assertEquals(2, $executedCount);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue