mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 20:54:03 +00:00
Code and comment style improvements
This commit is contained in:
parent
1e75221e12
commit
f4136f697c
5 changed files with 19 additions and 22 deletions
|
|
@ -58,10 +58,10 @@ class ClearPendingTenants extends Command
|
||||||
$query->where('data->pending_since', '<', $expireDate->timestamp);
|
$query->where('data->pending_since', '<', $expireDate->timestamp);
|
||||||
})
|
})
|
||||||
->get()
|
->get()
|
||||||
->each // This makes sure the events or triggered on the model
|
->each // Make sure the model events are triggered by deleting the tenants one by one
|
||||||
->delete()
|
->delete()
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$this->info("$deletedPendingCount pending tenant(s) deleted.");
|
$this->info($deletedPendingCount . ' pending ' . str('tenant')->plural($deletedPendingCount) . ' deleted.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,20 +38,22 @@ class CreatePendingTenants extends Command
|
||||||
$deployedCount = 0;
|
$deployedCount = 0;
|
||||||
while ($pendingCurrentCount < $pendingObjectifCount) {
|
while ($pendingCurrentCount < $pendingObjectifCount) {
|
||||||
tenancy()->model()::createPending();
|
tenancy()->model()::createPending();
|
||||||
// We update the number of pending tenants every time with a query to get a live count.
|
// Update the number of pending tenants every time with a query to get a live count
|
||||||
// this prevents to deploy too many tenants if pending tenants are being created simultaneous somewhere else
|
// To prevent deploying too many tenants if pending tenants are being created simultaneously somewhere else
|
||||||
// during the runtime of this command.
|
// While running this command
|
||||||
$pendingCurrentCount = $this->getPendingTenantCount();
|
$pendingCurrentCount = $this->getPendingTenantCount();
|
||||||
$deployedCount++;
|
$deployedCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->info("$deployedCount tenants deployed, $pendingObjectifCount tenant(s) are ready to be used.");
|
$this->info($deployedCount . ' ' . str('tenant')->plural($deployedCount) . ' deployed.');
|
||||||
|
$this->info($pendingObjecifCount . ' ' . str('tenant')->plural($pendingObjectifCount) . ' ready to be used.');
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the number of pending tenants currently deployed
|
* Calculate the number of currently deployed pending tenants.
|
||||||
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
private function getPendingTenantCount(): int
|
private function getPendingTenantCount(): int
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ trait HasPending
|
||||||
*/
|
*/
|
||||||
public function pending()
|
public function pending()
|
||||||
{
|
{
|
||||||
return !is_null($this->pending_since);
|
return ! is_null($this->pending_since);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function createPending($attributes = []): void
|
public static function createPending($attributes = []): void
|
||||||
|
|
@ -56,8 +56,8 @@ trait HasPending
|
||||||
|
|
||||||
event(new CreatingPendingTenant($tenant));
|
event(new CreatingPendingTenant($tenant));
|
||||||
|
|
||||||
// We add the pending value only after the model has then been created.
|
// Add the pending value only after creating the model
|
||||||
// this ensures the model is not marked as pending until the migrations, seeders, etc. are done
|
// To ensure it's not marked as pending until finishing running the migrations, seeders, etc.
|
||||||
$tenant->update([
|
$tenant->update([
|
||||||
'pending_since' => now()->timestamp
|
'pending_since' => now()->timestamp
|
||||||
]);
|
]);
|
||||||
|
|
@ -67,14 +67,14 @@ trait HasPending
|
||||||
|
|
||||||
public static function pullPendingTenant(bool $firstOrCreate = false): ?Tenant
|
public static function pullPendingTenant(bool $firstOrCreate = false): ?Tenant
|
||||||
{
|
{
|
||||||
if (!static::onlyPending()->exists()) {
|
if (! static::onlyPending()->exists()) {
|
||||||
if (!$firstOrCreate) {
|
if (! $firstOrCreate) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
static::createPending();
|
static::createPending();
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point we can guarantee a pending tenant is free and can be called.
|
// At this point, we can guarantee a pending tenant is free and can be called
|
||||||
$tenant = static::onlyPending()->first();
|
$tenant = static::onlyPending()->first();
|
||||||
|
|
||||||
event(new PullingPendingTenant($tenant));
|
event(new PullingPendingTenant($tenant));
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ class PendingScope implements Scope
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $model)
|
public function apply(Builder $builder, Model $model)
|
||||||
{
|
{
|
||||||
$builder->when(!config('tenancy.pending.include_in_queries'), function (Builder $builder){
|
$builder->when(! config('tenancy.pending.include_in_queries'), function (Builder $builder){
|
||||||
$builder->whereNull('data->pending_since');
|
$builder->whereNull('data->pending_since');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -71,10 +71,8 @@ class PendingScope implements Scope
|
||||||
{
|
{
|
||||||
$builder->macro('withoutPending', function (Builder $builder) {
|
$builder->macro('withoutPending', function (Builder $builder) {
|
||||||
|
|
||||||
// Only use whereNull('data->pending_since') when Laravel 6 support is dropped
|
|
||||||
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
|
|
||||||
$builder->withoutGlobalScope($this)
|
$builder->withoutGlobalScope($this)
|
||||||
->where('data->pending_since', 'like', 'null')
|
->whereNull('data->pending_since')
|
||||||
->orWhereNull('data');
|
->orWhereNull('data');
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
|
|
@ -90,10 +88,7 @@ class PendingScope implements Scope
|
||||||
protected function addOnlyPending(Builder $builder)
|
protected function addOnlyPending(Builder $builder)
|
||||||
{
|
{
|
||||||
$builder->macro('onlyPending', function (Builder $builder) {
|
$builder->macro('onlyPending', function (Builder $builder) {
|
||||||
|
$builder->withoutGlobalScope($this)->whereNotNull('data->pending_since');
|
||||||
// Use whereNotNull when Laravel 6 is dropped
|
|
||||||
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
|
|
||||||
$builder->withoutGlobalScope($this)->where('data->pending_since', 'not like', 'null');
|
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -152,7 +152,7 @@ class Tenancy
|
||||||
// Wrap string in array
|
// Wrap string in array
|
||||||
$tenants = is_string($tenants) ? [$tenants] : $tenants;
|
$tenants = is_string($tenants) ? [$tenants] : $tenants;
|
||||||
|
|
||||||
// Use all tenants if $tenants is false
|
// Use all tenants if $tenants is falsy
|
||||||
$tenants = $tenants ?: $query->cursor();
|
$tenants = $tenants ?: $query->cursor();
|
||||||
|
|
||||||
$originalTenant = $this->tenant;
|
$originalTenant = $this->tenant;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue