From 5ef225762f9806c74577ef54db4a0ca48ea2e58d Mon Sep 17 00:00:00 2001 From: IWT Date: Thu, 19 Nov 2020 20:15:59 +0700 Subject: [PATCH] add --only-selected option for tenants:migrate command --- assets/config.php | 5 +++++ src/Commands/Migrate.php | 23 ++++++++++++++++++++++- tests/CommandsTest.php | 19 +++++++++++++++++++ tests/Etc/Tenant.php | 5 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/assets/config.php b/assets/config.php index e89c6c9d..be992831 100644 --- a/assets/config.php +++ b/assets/config.php @@ -178,6 +178,11 @@ return [ '--realpath' => true, ], + /** + * The class and method name used by tenants:migrate command with --only-selected option. + */ + 'migration_filter_tenants_method' => [\App\Repositories\TenantRepository::class, 'filterBy'], + /** * Parameters used by the tenants:seed command. */ diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 4bf8408c..db7f1976 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -16,6 +16,20 @@ class Migrate extends MigrateCommand { use HasATenantsOption, DealsWithMigrations; + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'migrate {--database= : The database connection to use} + {--force : Force the operation to run when in production} + {--path=* : The path(s) to the migrations files to be executed} + {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} + {--pretend : Dump the SQL queries that would be run} + {--seed : Indicates if the seed task should be re-run} + {--step : Force the migrations to be run so they can be rolled back individually} + {--only-selected : Filter the tenants by a method in the model}'; + /** * The console command description. * @@ -54,7 +68,14 @@ class Migrate extends MigrateCommand return; } - tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { + $tenants = $this->option('tenants'); + $filterMethod = config('tenancy.migration_filter_tenants_method'); + + if ($this->option('only-selected') && method_exists($filterMethod[0], $filterMethod[1])) { + $tenants = (new $filterMethod[0])->{$filterMethod[1]}($tenants); + } + + tenancy()->runForMultiple($tenants, function ($tenant) { $this->line("Tenant: {$tenant['id']}"); event(new MigratingDatabase($tenant)); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d7da0cab..1f62781f 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -91,6 +91,25 @@ class CommandsTest extends TestCase $this->assertTrue(Schema::hasTable('users')); } + /** @test */ + public function migrate_command_works_with_only_selected_tenants_option() + { + $tenants = [Tenant::create(), Tenant::create()]; + + config()->set('tenancy.migration_filter_tenants_method', [Stancl\Tenancy\Tests\Etc\Tenant::class, 'getTheFirstOne']); + + Artisan::call('tenants:migrate', [ + '--tenants' => [$tenants[0]['id'], $tenants[1]['id']], + '--only-selected' => true + ]); + + tenancy()->initialize($tenants[0]); + $this->assertTrue(Schema::hasTable('users')); + + tenancy()->initialize($tenants[1]); + $this->assertFalse(Schema::hasTable('users')); + } + /** @test */ public function rollback_command_works() { diff --git a/tests/Etc/Tenant.php b/tests/Etc/Tenant.php index 83840280..b5188a3e 100644 --- a/tests/Etc/Tenant.php +++ b/tests/Etc/Tenant.php @@ -12,4 +12,9 @@ use Stancl\Tenancy\Database\Models; class Tenant extends Models\Tenant implements TenantWithDatabase { use HasDatabase, HasDomains; + + public function getTheFirstOne(array $tenantIDs = []) + { + return $tenantIDs[0]; + } }