diff --git a/src/Commands/MigrateStatus.php b/src/Commands/MigrateStatus.php new file mode 100644 index 000000000..21b38e029 --- /dev/null +++ b/src/Commands/MigrateStatus.php @@ -0,0 +1,62 @@ +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(); + }); + } +} diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index dec8c72e5..557afad89 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -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']); @@ -90,6 +93,7 @@ public function boot(): void Commands\Rollback::class, Commands\TenantList::class, Commands\MigrateFresh::class, + Commands\MigrateStatus::class, ]); $this->publishes([ diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index eabb0aaaf..8bb3e858e 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -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() {