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:
parent
ab64f4599d
commit
ef47884331
3 changed files with 94 additions and 0 deletions
62
src/Commands/MigrateStatus.php
Normal file
62
src/Commands/MigrateStatus.php
Normal 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -65,6 +65,9 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
$this->app->singleton(Commands\Rollback::class, function ($app) {
|
$this->app->singleton(Commands\Rollback::class, function ($app) {
|
||||||
return new Commands\Rollback($app['migrator']);
|
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) {
|
$this->app->singleton(Commands\Seed::class, function ($app) {
|
||||||
return new Commands\Seed($app['db']);
|
return new Commands\Seed($app['db']);
|
||||||
|
|
@ -90,6 +93,7 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Commands\Rollback::class,
|
Commands\Rollback::class,
|
||||||
Commands\TenantList::class,
|
Commands\TenantList::class,
|
||||||
Commands\MigrateFresh::class,
|
Commands\MigrateFresh::class,
|
||||||
|
Commands\MigrateStatus::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,34 @@ class CommandsTest extends TestCase
|
||||||
$this->assertFalse(Schema::hasTable('users'));
|
$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]
|
#[Test]
|
||||||
public function seed_command_works()
|
public function seed_command_works()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue