45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Environments;
|
|
|
|
use App\Enums\OperationKind;
|
|
use App\Enums\OperationStatus;
|
|
use App\Enums\ServiceType;
|
|
use App\Models\Environment;
|
|
use App\Models\Operation;
|
|
use App\Models\Service;
|
|
use InvalidArgumentException;
|
|
|
|
class CreateMigrationOperation
|
|
{
|
|
public function __construct(
|
|
private readonly BuildMigrationScript $buildMigrationScript,
|
|
) {}
|
|
|
|
public function execute(Environment $environment, ?Service $service = null): Operation
|
|
{
|
|
$service ??= $environment->services()
|
|
->where('type', ServiceType::LARAVEL)
|
|
->get()
|
|
->first(fn (Service $service): bool => in_array('web', $service->process_roles ?? [], true));
|
|
|
|
if (! $service || $service->type !== ServiceType::LARAVEL) {
|
|
throw new InvalidArgumentException('Laravel migrations must run against a Laravel runtime service.');
|
|
}
|
|
|
|
$operation = $service->operations()->create([
|
|
'kind' => OperationKind::CONFIG_CHANGE,
|
|
'status' => OperationStatus::PENDING,
|
|
]);
|
|
|
|
$operation->steps()->create([
|
|
'name' => 'Run Laravel migrations',
|
|
'order' => 1,
|
|
'status' => OperationStatus::PENDING,
|
|
'script' => $this->buildMigrationScript->execute($service, respectAutomaticMode: false),
|
|
]);
|
|
|
|
return $operation;
|
|
}
|
|
}
|