mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 07:14:04 +00:00
Parallel migrations (#57)
* parallelize migration-related commands * Fix code style (php-cs-fixer) --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
15d12e22c7
commit
1b0e7d0507
5 changed files with 285 additions and 43 deletions
|
|
@ -8,16 +8,18 @@ use Illuminate\Contracts\Events\Dispatcher;
|
|||
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
||||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use Stancl\Tenancy\Concerns\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
|
||||
use Stancl\Tenancy\Concerns\HasTenantOptions;
|
||||
use Stancl\Tenancy\Concerns\ParallelCommand;
|
||||
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
|
||||
use Stancl\Tenancy\Events\DatabaseMigrated;
|
||||
use Stancl\Tenancy\Events\MigratingDatabase;
|
||||
|
||||
class Migrate extends MigrateCommand
|
||||
{
|
||||
use HasTenantOptions, DealsWithMigrations, ExtendsLaravelCommand;
|
||||
use HasTenantOptions, DealsWithMigrations, ExtendsLaravelCommand, ParallelCommand;
|
||||
|
||||
protected $description = 'Run migrations for tenant(s)';
|
||||
|
||||
|
|
@ -31,6 +33,7 @@ class Migrate extends MigrateCommand
|
|||
parent::__construct($migrator, $dispatcher);
|
||||
|
||||
$this->addOption('skip-failing', description: 'Continue execution if migration fails for a tenant');
|
||||
$this->addProcessesOption();
|
||||
|
||||
$this->specifyParameters();
|
||||
}
|
||||
|
|
@ -47,26 +50,50 @@ class Migrate extends MigrateCommand
|
|||
return 1;
|
||||
}
|
||||
|
||||
foreach ($this->getTenants() as $tenant) {
|
||||
if ($this->getProcesses() > 1) {
|
||||
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
|
||||
return $this->getTenants(array_values($chunk->all()));
|
||||
}));
|
||||
}
|
||||
|
||||
return $this->migrateTenants($this->getTenants()) ? 0 : 1;
|
||||
}
|
||||
|
||||
protected function childHandle(...$args): bool
|
||||
{
|
||||
$chunk = $args[0];
|
||||
|
||||
return $this->migrateTenants($chunk);
|
||||
}
|
||||
|
||||
protected function migrateTenants(LazyCollection $tenants): bool
|
||||
{
|
||||
$success = true;
|
||||
|
||||
foreach ($tenants as $tenant) {
|
||||
try {
|
||||
$this->components->info("Migrating tenant {$tenant->getTenantKey()}");
|
||||
|
||||
$tenant->run(function ($tenant) {
|
||||
$tenant->run(function ($tenant) use (&$success) {
|
||||
event(new MigratingDatabase($tenant));
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
if (parent::handle() !== 0) {
|
||||
$success = false;
|
||||
}
|
||||
|
||||
event(new DatabaseMigrated($tenant));
|
||||
});
|
||||
} catch (TenantDatabaseDoesNotExistException|QueryException $e) {
|
||||
$this->components->error("Migration failed for tenant {$tenant->getTenantKey()}: {$e->getMessage()}");
|
||||
$success = false;
|
||||
|
||||
if (! $this->option('skip-failing')) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->components->warn("Migration failed for tenant {$tenant->getTenantKey()}: {$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return $success;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue