Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Commands/MigrateStatus.php
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
4 changes: 4 additions & 0 deletions src/TenancyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public function register(): void
$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']);
Expand All @@ -90,6 +93,7 @@ public function boot(): void
Commands\Rollback::class,
Commands\TenantList::class,
Commands\MigrateFresh::class,
Commands\MigrateStatus::class,
]);

$this->publishes([
Expand Down
28 changes: 28 additions & 0 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ public function rollback_command_works()
$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()
{
Expand Down
Loading