1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 06:54:03 +00:00
This commit is contained in:
Lucas Michot 2026-08-05 12:12:02 +02:00 committed by GitHub
commit 47c00dbdb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 11 deletions

View file

@ -18,21 +18,28 @@ class Seed extends SeedCommand
public function __construct(ConnectionResolverInterface $resolver) public function __construct(ConnectionResolverInterface $resolver)
{ {
// See https://github.com/archtechx/tenancy/issues/1474 parent::__construct($resolver);
if (version_compare(app()->version(), '13.24.0', '>=')) {
$this->signature = 'tenants:seed // Our --tenants/--skip-tenants/--with-pending options only get added automatically
{class? : The class name of the root seeder} // when the parent command isn't signature-based. Since Laravel 13.24, SeedCommand is,
{--class=Database\\Seeders\\DatabaseSeeder : The class name of the root seeder} // so we add them ourselves here -- checking first so we don't add them twice on older
{--database= : The database connection to seed} // Laravel versions, where they're already there by this point.
{--force : Force the operation to run when in production}'; if (! $this->getDefinition()->hasOption('tenants')) {
parent::__construct($resolver);
$this->specifyParameters(); $this->specifyParameters();
} else {
$this->name = 'tenants:seed';
parent::__construct($resolver);
} }
} }
protected function configure(): void
{
parent::configure();
// We inherit SeedCommand's name ('db:seed') since we don't redeclare $name/$signature,
// so without this we'd overwrite Laravel's own db:seed command (see #1474). configure()
// always runs after the name is set, regardless of Laravel version, so setting it here
// is safe no matter which of $name/$signature the installed Laravel version uses.
$this->setName('tenants:seed');
}
public function handle(): int public function handle(): int
{ {
foreach (config('tenancy.seeder_parameters') as $parameter => $value) { foreach (config('tenancy.seeder_parameters') as $parameter => $value) {

View file

@ -16,7 +16,9 @@ use Stancl\Tenancy\Jobs\DeleteDatabase;
use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseManager;
use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Commands\Seed;
use Stancl\Tenancy\Tests\Etc\TestSeeder; use Stancl\Tenancy\Tests\Etc\TestSeeder;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Stancl\Tenancy\Events\DeletingTenant; use Stancl\Tenancy\Events\DeletingTenant;
use Stancl\Tenancy\Events\DatabaseMigrated; use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
@ -288,6 +290,16 @@ test('seed command works', function () {
}); });
}); });
test('tenants:seed does not overwrite db:seed', function () {
$commands = Artisan::all();
expect($commands)->toHaveKey('db:seed')
->and($commands)->toHaveKey('tenants:seed')
->and($commands['db:seed'])->toBeInstanceOf(SeedCommand::class)
->and($commands['db:seed'])->not()->toBeInstanceOf(Seed::class)
->and($commands['tenants:seed'])->toBeInstanceOf(Seed::class);
});
test('database connection is switched to default after running commands', function (bool $initializeTenancy) { test('database connection is switched to default after running commands', function (bool $initializeTenancy) {
$tenant = Tenant::create(); $tenant = Tenant::create();