1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 18:04:03 +00:00

[4.x] Add tenant schema dump command (#807)

* Add tenant dump command

* Register tenant schema dump command

* Added tests for tenant schema dump command

* remove docblocks, fix tenant() logic

* trigger ci

* Install mysql-client

* mysql-client -> mariadb-client

* add tenant-schema-test.dump to .gitignore

Co-authored-by: Samuel Štancl <samuel@archte.ch>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
Victor R 2022-06-01 10:12:59 -04:00 committed by GitHub
parent d0de09aa53
commit 7d98ebb5d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 155 additions and 1 deletions

View file

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Console\DumpCommand;
use Stancl\Tenancy\Contracts\Tenant;
use Symfony\Component\Console\Input\InputOption;
class TenantDump extends DumpCommand
{
public function __construct()
{
parent::__construct();
$this->setName('tenants:dump');
$this->specifyParameters();
}
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int
{
$this->tenant()->run(fn() => parent::handle($connections, $dispatcher));
return Command::SUCCESS;
}
public function tenant(): Tenant
{
$tenant = $this->option('tenant')
?? tenant()
?? $this->ask('What tenant do you want to dump the schema for?')
?? tenancy()->query()->first();
if (! $tenant instanceof Tenant) {
$tenant = tenancy()->find($tenant);
}
throw_if(! $tenant, 'Could not identify the tenant to use for dumping the schema.');
return $tenant;
}
protected function getOptions(): array
{
return array_merge([
['tenant', null, InputOption::VALUE_OPTIONAL, '', null],
], parent::getOptions());
}
}

View file

@ -88,6 +88,7 @@ class TenancyServiceProvider extends ServiceProvider
Commands\Migrate::class,
Commands\Rollback::class,
Commands\TenantList::class,
Commands\TenantDump::class,
Commands\MigrateFresh::class,
]);