1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-06-20 22:54:05 +00:00

Add MigrateStatus command and corresponding tests

This commit is contained in:
Tiziano Zonta 2026-05-15 12:05:30 +02:00
parent ab64f4599d
commit ef47884331
3 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Database\Console\Migrations\StatusCommand;
use Illuminate\Database\Migrations\Migrator;
use Stancl\Tenancy\Concerns\DealsWithMigrations;
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
use Stancl\Tenancy\Concerns\HasATenantsOption;
class MigrateStatus extends StatusCommand
{
use HasATenantsOption, DealsWithMigrations, ExtendsLaravelCommand;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Show the status of each migration for tenant(s).';
protected static function getTenantCommandName(): string
{
return 'tenants:migrate-status';
}
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Migrator $migrator)
{
parent::__construct($migrator);
$this->specifyTenantSignature();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
foreach (config('tenancy.migration_parameters') as $parameter => $value) {
if (! $this->input->hasParameterOption($parameter)) {
if ($this->getDefinition()->hasOption(ltrim($parameter, '-'))) {
$this->input->setOption(ltrim($parameter, '-'), $value);
}
}
}
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}");
parent::handle();
});
}
}

View file

@ -65,6 +65,9 @@ class TenancyServiceProvider extends ServiceProvider
$this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator']);
});
$this->app->singleton(Commands\MigrateStatus::class, function ($app) {
return new Commands\MigrateStatus($app['migrator']);
});
$this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db']);
@ -90,6 +93,7 @@ class TenancyServiceProvider extends ServiceProvider
Commands\Rollback::class,
Commands\TenantList::class,
Commands\MigrateFresh::class,
Commands\MigrateStatus::class,
]);
$this->publishes([

View file

@ -106,6 +106,34 @@ class CommandsTest extends TestCase
$this->assertFalse(Schema::hasTable('users'));
}
#[Test]
public function migrate_status_command_works()
{
$tenant = Tenant::create();
$this->artisan('tenants:migrate-status')
->expectsOutputToContain('Tenant: ' . $tenant->getTenantKey());
Artisan::call('tenants:migrate', ['--tenants' => [$tenant->getTenantKey()]]);
$this->artisan('tenants:migrate-status', ['--tenants' => [$tenant->getTenantKey()]])
->expectsOutputToContain('Tenant: ' . $tenant->getTenantKey())
->expectsOutputToContain('Ran');
}
#[Test]
public function migrate_status_command_works_with_multiple_tenants()
{
$tenant1 = Tenant::create()->getTenantKey();
$tenant2 = Tenant::create()->getTenantKey();
Artisan::call('tenants:migrate', ['--tenants' => [$tenant1, $tenant2]]);
$this->artisan('tenants:migrate-status')
->expectsOutputToContain('Tenant: ' . $tenant1)
->expectsOutputToContain('Tenant: ' . $tenant2);
}
#[Test]
public function seed_command_works()
{