mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 00:04:03 +00:00
Merge branch 'master' into add-skip-failing-options-to-migrate
This commit is contained in:
commit
29d13ae5b4
86 changed files with 2001 additions and 236 deletions
57
src/Commands/ClearPendingTenants.php
Normal file
57
src/Commands/ClearPendingTenants.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ClearPendingTenants extends Command
|
||||
{
|
||||
protected $signature = 'tenants:pending-clear
|
||||
{--older-than-days= : Deletes all pending tenants older than the amount of days}
|
||||
{--older-than-hours= : Deletes all pending tenants older than the amount of hours}';
|
||||
|
||||
protected $description = 'Remove pending tenants.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->components->info('Removing pending tenants.');
|
||||
|
||||
$expirationDate = now();
|
||||
// We compare the original expiration date to the new one to check if the new one is different later
|
||||
$originalExpirationDate = $expirationDate->copy()->toImmutable();
|
||||
|
||||
$olderThanDays = (int) $this->option('older-than-days');
|
||||
$olderThanHours = (int) $this->option('older-than-hours');
|
||||
|
||||
if ($olderThanDays && $olderThanHours) {
|
||||
$this->components->error("Cannot use '--older-than-days' and '--older-than-hours' together. Please, choose only one of these options.");
|
||||
|
||||
return 1; // Exit code for failure
|
||||
}
|
||||
|
||||
if ($olderThanDays) {
|
||||
$expirationDate->subDays($olderThanDays);
|
||||
}
|
||||
|
||||
if ($olderThanHours) {
|
||||
$expirationDate->subHours($olderThanHours);
|
||||
}
|
||||
|
||||
$deletedTenantCount = tenancy()->query()
|
||||
->onlyPending()
|
||||
->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) {
|
||||
$query->where($query->getModel()->getColumnForQuery('pending_since'), '<', $expirationDate->timestamp);
|
||||
})
|
||||
->get()
|
||||
->each // Trigger the model events by deleting the tenants one by one
|
||||
->delete()
|
||||
->count();
|
||||
|
||||
$this->components->info($deletedTenantCount . ' pending ' . str('tenant')->plural($deletedTenantCount) . ' deleted.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
46
src/Commands/CreatePendingTenants.php
Normal file
46
src/Commands/CreatePendingTenants.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CreatePendingTenants extends Command
|
||||
{
|
||||
protected $signature = 'tenants:pending-create {--count= : The number of pending tenants to be created}';
|
||||
|
||||
protected $description = 'Create pending tenants.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->components->info('Creating pending tenants.');
|
||||
|
||||
$maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count'));
|
||||
$pendingTenantCount = $this->getPendingTenantCount();
|
||||
$createdCount = 0;
|
||||
|
||||
while ($pendingTenantCount < $maxPendingTenantCount) {
|
||||
tenancy()->model()::createPending();
|
||||
|
||||
// Fetching the pending tenant count in each iteration prevents creating too many tenants
|
||||
// If pending tenants are being created somewhere else while running this command
|
||||
$pendingTenantCount = $this->getPendingTenantCount();
|
||||
|
||||
$createdCount++;
|
||||
}
|
||||
|
||||
$this->components->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.');
|
||||
$this->components->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Calculate the number of currently available pending tenants. */
|
||||
protected function getPendingTenantCount(): int
|
||||
{
|
||||
return tenancy()->query()
|
||||
->onlyPending()
|
||||
->count();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,11 +5,11 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Foundation\Console\DownCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
|
||||
class Down extends DownCommand
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions;
|
||||
|
||||
protected $signature = 'tenants:down
|
||||
{--redirect= : The path that users should be redirected to}
|
||||
|
|
|
|||
|
|
@ -117,6 +117,7 @@ class Install extends Command
|
|||
$this->newLine();
|
||||
}
|
||||
} else {
|
||||
/** @var string $warning */
|
||||
$this->components->warn($warning);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ use Illuminate\Console\Command;
|
|||
use Illuminate\Support\LazyCollection;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
|
||||
class Link extends Command
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions;
|
||||
|
||||
protected $signature = 'tenants:link
|
||||
{--tenants=* : The tenant(s) to run the command for. Default: all}
|
||||
|
|
@ -34,7 +34,7 @@ class Link extends Command
|
|||
$this->createLinks($tenants);
|
||||
}
|
||||
} catch (Exception $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
$this->components->error($exception->getMessage());
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,14 +9,15 @@ use Illuminate\Database\Console\Migrations\MigrateCommand;
|
|||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
|
||||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||
use Stancl\Tenancy\Events\MigratingDatabase;
|
||||
|
||||
class Migrate extends MigrateCommand
|
||||
{
|
||||
use HasATenantsOption, ExtendsLaravelCommand;
|
||||
use HasTenantOptions, DealsWithMigrations, ExtendsLaravelCommand;
|
||||
|
||||
protected $description = 'Run migrations for tenant(s)';
|
||||
|
||||
|
|
|
|||
|
|
@ -4,13 +4,14 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Illuminate\Database\Console\Migrations\BaseCommand;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class MigrateFresh extends Command
|
||||
class MigrateFresh extends BaseCommand
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions, DealsWithMigrations;
|
||||
|
||||
protected $description = 'Drop all tables and re-run all migrations for tenant(s)';
|
||||
|
||||
|
|
|
|||
|
|
@ -5,13 +5,18 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Database\Console\Migrations\FreshCommand;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class MigrateFreshOverride extends FreshCommand
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
if (config('tenancy.database.drop_tenant_databases_on_migrate_fresh')) {
|
||||
tenancy()->model()::cursor()->each->delete();
|
||||
$tenantModel = tenancy()->model();
|
||||
|
||||
if (Schema::hasTable($tenantModel->getTable())) {
|
||||
$tenantModel::cursor()->each->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return parent::handle();
|
||||
|
|
|
|||
|
|
@ -6,14 +6,15 @@ namespace Stancl\Tenancy\Commands;
|
|||
|
||||
use Illuminate\Database\Console\Migrations\RollbackCommand;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Stancl\Tenancy\Events\DatabaseRolledBack;
|
||||
use Stancl\Tenancy\Events\RollingBackDatabase;
|
||||
|
||||
class Rollback extends RollbackCommand
|
||||
{
|
||||
use HasATenantsOption, ExtendsLaravelCommand;
|
||||
use HasTenantOptions, DealsWithMigrations, ExtendsLaravelCommand;
|
||||
|
||||
protected $description = 'Rollback migrations for tenant(s).';
|
||||
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@ namespace Stancl\Tenancy\Commands;
|
|||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Symfony\Component\Console\Input\ArgvInput;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
|
||||
class Run extends Command
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions;
|
||||
|
||||
protected $description = 'Run a command for tenant(s)';
|
||||
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@ namespace Stancl\Tenancy\Commands;
|
|||
|
||||
use Illuminate\Database\ConnectionResolverInterface;
|
||||
use Illuminate\Database\Console\Seeds\SeedCommand;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Stancl\Tenancy\Events\DatabaseSeeded;
|
||||
use Stancl\Tenancy\Events\SeedingDatabase;
|
||||
|
||||
class Seed extends SeedCommand
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions;
|
||||
|
||||
protected $description = 'Seed tenant database(s).';
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ class TenantDump extends DumpCommand
|
|||
|
||||
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int
|
||||
{
|
||||
if (is_null($this->option('path'))) {
|
||||
$this->input->setOption('path', config('tenancy.migration_parameters.--schema-path') ?? database_path('schema/tenant-schema.dump'));
|
||||
}
|
||||
|
||||
$tenant = $this->option('tenant')
|
||||
?? tenant()
|
||||
?? $this->ask('What tenant do you want to dump the schema for?')
|
||||
|
|
@ -37,7 +41,7 @@ class TenantDump extends DumpCommand
|
|||
return 1;
|
||||
}
|
||||
|
||||
parent::handle($connections, $dispatcher);
|
||||
$tenant->run(fn () => parent::handle($connections, $dispatcher));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
|
||||
class Up extends Command
|
||||
{
|
||||
use HasATenantsOption;
|
||||
use HasTenantOptions;
|
||||
|
||||
protected $signature = 'tenants:up';
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue