mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 07:54:03 +00:00
* Add and test Migrate command's skip-failing option * Improve naming * Move migration event dispatching inside try block * Change test name * Fix skip-failing test * Use QueryException instead of Exception * Correct TenantDatabaseDoesNotExistException import * Correct test * Check for the the testing env in DB bootstrapper * Correct the Migrate command * Fix code style (php-cs-fixer) * add docs todo * Add QueryException to the Migrat command try/catch * Return status codes in Migrate * Fix code style (php-cs-fixer) * Add test for not stopping tenants:migrate after the first failure * Update Migrate command * Fix code style (php-cs-fixer) * Fix code style (php-cs-fixer) * Use `getTenants()` * Use withtenantDatabases where needed * Add withTenantDatabases to test --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
70 lines
2 KiB
PHP
70 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Commands;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
|
use Illuminate\Database\Migrations\Migrator;
|
|
use Illuminate\Database\QueryException;
|
|
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
|
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
|
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
|
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
|
|
use Stancl\Tenancy\Events\DatabaseMigrated;
|
|
use Stancl\Tenancy\Events\MigratingDatabase;
|
|
|
|
class Migrate extends MigrateCommand
|
|
{
|
|
use HasTenantOptions, DealsWithMigrations, ExtendsLaravelCommand;
|
|
|
|
protected $description = 'Run migrations for tenant(s)';
|
|
|
|
protected static function getTenantCommandName(): string
|
|
{
|
|
return 'tenants:migrate';
|
|
}
|
|
|
|
public function __construct(Migrator $migrator, Dispatcher $dispatcher)
|
|
{
|
|
parent::__construct($migrator, $dispatcher);
|
|
|
|
$this->addOption('skip-failing');
|
|
|
|
$this->specifyParameters();
|
|
}
|
|
|
|
public function handle(): int
|
|
{
|
|
foreach (config('tenancy.migration_parameters') as $parameter => $value) {
|
|
if (! $this->input->hasParameterOption($parameter)) {
|
|
$this->input->setOption(ltrim($parameter, '-'), $value);
|
|
}
|
|
}
|
|
|
|
if (! $this->confirmToProceed()) {
|
|
return 1;
|
|
}
|
|
|
|
foreach ($this->getTenants() as $tenant) {
|
|
try {
|
|
$tenant->run(function ($tenant) {
|
|
$this->line("Tenant: {$tenant->getTenantKey()}");
|
|
|
|
event(new MigratingDatabase($tenant));
|
|
// Migrate
|
|
parent::handle();
|
|
|
|
event(new DatabaseMigrated($tenant));
|
|
});
|
|
} catch (TenantDatabaseDoesNotExistException|QueryException $th) {
|
|
if (! $this->option('skip-failing')) {
|
|
throw $th;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|