mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 16:14:03 +00:00
add --only-selected option for tenants:migrate command
This commit is contained in:
parent
126afcd0dd
commit
5ef225762f
4 changed files with 51 additions and 1 deletions
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue