2024-01-06 05:24:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire\Project\Database;
|
|
|
|
|
|
2026-05-20 19:04:43 +00:00
|
|
|
use App\Models\ServiceDatabase;
|
|
|
|
|
use App\Models\StandaloneClickhouse;
|
|
|
|
|
use App\Models\StandaloneDragonfly;
|
|
|
|
|
use App\Models\StandaloneKeydb;
|
|
|
|
|
use App\Models\StandaloneRedis;
|
2026-05-28 17:30:12 +00:00
|
|
|
use Illuminate\Contracts\View\View;
|
2025-08-23 16:50:35 +00:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2026-05-28 17:30:12 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2026-03-25 19:21:39 +00:00
|
|
|
use Livewire\Attributes\Locked;
|
2024-06-10 20:43:34 +00:00
|
|
|
use Livewire\Component;
|
2024-01-06 05:24:57 +00:00
|
|
|
|
|
|
|
|
class Import extends Component
|
|
|
|
|
{
|
2025-08-23 16:50:35 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2026-03-25 19:21:39 +00:00
|
|
|
#[Locked]
|
2026-01-02 15:29:48 +00:00
|
|
|
public ?int $resourceId = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-03-25 19:21:39 +00:00
|
|
|
#[Locked]
|
2026-01-02 15:29:48 +00:00
|
|
|
public ?string $resourceType = null;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-01-02 15:29:48 +00:00
|
|
|
public string $resourceStatus = '';
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public string $resourceUuid = '';
|
2024-01-06 05:24:57 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public bool $unsupported = false;
|
2025-11-02 14:19:13 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public function getListeners(): array
|
|
|
|
|
{
|
|
|
|
|
$listeners = ['databaseUpdated' => 'refreshStatus'];
|
2025-11-02 14:19:13 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$user = Auth::user();
|
|
|
|
|
if (! $user) {
|
|
|
|
|
return $listeners;
|
|
|
|
|
}
|
2025-11-02 14:19:13 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$listeners["echo-private:user.{$user->id},DatabaseStatusChanged"] = 'refreshStatus';
|
2025-11-02 14:19:13 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$team = $user->currentTeam();
|
|
|
|
|
if ($team) {
|
|
|
|
|
$listeners["echo-private:team.{$team->id},ServiceChecked"] = 'refreshStatus';
|
2026-01-02 15:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
return $listeners;
|
2026-01-02 15:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public function mount(): void
|
2026-01-02 15:29:48 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
$resource = $this->resolveResourceFromRoute();
|
|
|
|
|
$this->authorize('view', $resource);
|
2026-01-02 15:29:48 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$this->resourceId = $resource->id;
|
|
|
|
|
$this->resourceType = get_class($resource);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$this->refreshStatus();
|
2025-11-26 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public function refreshStatus(): void
|
2024-01-06 05:24:57 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
$resource = $this->resolveStoredResource();
|
|
|
|
|
$this->authorize('view', $resource);
|
|
|
|
|
|
|
|
|
|
$resource->refresh();
|
|
|
|
|
$this->resourceUuid = $resource->uuid;
|
|
|
|
|
$this->resourceStatus = $resource->status ?? '';
|
|
|
|
|
$this->unsupported = $this->isUnsupportedResource($resource);
|
2024-01-06 05:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
public function render(): View
|
2025-01-07 12:00:41 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
return view('livewire.project.database.import');
|
2025-01-07 12:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
private function resolveResourceFromRoute(): object
|
2024-01-06 05:24:57 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
$parameters = get_route_parameters();
|
|
|
|
|
$teamId = data_get(Auth::user()?->currentTeam(), 'id');
|
|
|
|
|
$databaseUuid = data_get($parameters, 'database_uuid');
|
|
|
|
|
$stackServiceUuid = data_get($parameters, 'stack_service_uuid');
|
2026-01-02 15:29:48 +00:00
|
|
|
|
|
|
|
|
if ($databaseUuid) {
|
|
|
|
|
$resource = getResourceByUuid($databaseUuid, $teamId);
|
2026-05-28 17:30:12 +00:00
|
|
|
if ($resource) {
|
|
|
|
|
return $resource;
|
2026-01-02 15:29:48 +00:00
|
|
|
}
|
2026-05-28 17:30:12 +00:00
|
|
|
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($stackServiceUuid) {
|
2026-05-22 11:38:28 +00:00
|
|
|
$project = currentTeam()
|
|
|
|
|
->projects()
|
|
|
|
|
->select('id', 'uuid', 'team_id')
|
2026-05-28 17:30:12 +00:00
|
|
|
->where('uuid', data_get($parameters, 'project_uuid'))
|
2026-05-22 11:38:28 +00:00
|
|
|
->firstOrFail();
|
|
|
|
|
$environment = $project->environments()
|
|
|
|
|
->select('id', 'uuid', 'name', 'project_id')
|
2026-05-28 17:30:12 +00:00
|
|
|
->where('uuid', data_get($parameters, 'environment_uuid'))
|
2026-05-22 11:38:28 +00:00
|
|
|
->firstOrFail();
|
2026-05-28 17:30:12 +00:00
|
|
|
$service = $environment->services()->whereUuid(data_get($parameters, 'service_uuid'))->firstOrFail();
|
2026-01-02 15:29:48 +00:00
|
|
|
$resource = $service->databases()->whereUuid($stackServiceUuid)->first();
|
2026-05-28 17:30:12 +00:00
|
|
|
if ($resource) {
|
|
|
|
|
return $resource;
|
2026-01-02 15:29:48 +00:00
|
|
|
}
|
2024-01-06 05:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
abort(404);
|
2024-01-06 05:24:57 +00:00
|
|
|
}
|
2024-01-10 14:42:54 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
private function resolveStoredResource(): object
|
2025-01-07 13:02:19 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
if ($this->resourceId === null || $this->resourceType === null) {
|
|
|
|
|
return $this->resolveResourceFromRoute();
|
2025-11-02 14:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
$resource = $this->resourceType::find($this->resourceId);
|
|
|
|
|
if ($resource) {
|
|
|
|
|
return $resource;
|
2025-11-25 15:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
abort(404);
|
2025-11-02 14:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
private function isUnsupportedResource(object $resource): bool
|
2025-11-02 14:19:13 +00:00
|
|
|
{
|
2026-05-28 17:30:12 +00:00
|
|
|
if (
|
|
|
|
|
$resource instanceof StandaloneRedis ||
|
|
|
|
|
$resource instanceof StandaloneKeydb ||
|
|
|
|
|
$resource instanceof StandaloneDragonfly ||
|
|
|
|
|
$resource instanceof StandaloneClickhouse
|
|
|
|
|
) {
|
2026-03-01 11:45:55 +00:00
|
|
|
return true;
|
2025-11-02 14:19:13 +00:00
|
|
|
}
|
2026-03-01 11:45:55 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
if ($resource instanceof ServiceDatabase) {
|
|
|
|
|
$dbType = $resource->databaseType();
|
2026-01-02 15:29:48 +00:00
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
return str_contains($dbType, 'redis') ||
|
|
|
|
|
str_contains($dbType, 'keydb') ||
|
|
|
|
|
str_contains($dbType, 'dragonfly') ||
|
|
|
|
|
str_contains($dbType, 'clickhouse');
|
2025-11-02 14:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 17:30:12 +00:00
|
|
|
return false;
|
2025-11-02 14:19:13 +00:00
|
|
|
}
|
2024-01-06 05:24:57 +00:00
|
|
|
}
|