refactor(environment): streamline environment variable handling by replacing sorting methods with direct property access and enhancing query ordering for improved performance

This commit is contained in:
Andras Bacsai
2025-09-12 12:09:03 +02:00
parent c6b47da1e9
commit 8e155f25b3
12 changed files with 108 additions and 43 deletions

View File

@@ -40,7 +40,7 @@ class All extends Component
if (str($this->resourceClass)->contains($resourceWithPreviews) && ! $simpleDockerfile) { if (str($this->resourceClass)->contains($resourceWithPreviews) && ! $simpleDockerfile) {
$this->showPreview = true; $this->showPreview = true;
} }
$this->sortEnvironmentVariables(); $this->getDevView();
} }
public function instantSave() public function instantSave()
@@ -50,33 +50,36 @@ class All extends Component
$this->resource->settings->is_env_sorting_enabled = $this->is_env_sorting_enabled; $this->resource->settings->is_env_sorting_enabled = $this->is_env_sorting_enabled;
$this->resource->settings->save(); $this->resource->settings->save();
$this->sortEnvironmentVariables(); $this->getDevView();
$this->dispatch('success', 'Environment variable settings updated.'); $this->dispatch('success', 'Environment variable settings updated.');
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }
} }
public function sortEnvironmentVariables() public function getEnvironmentVariablesProperty()
{ {
if ($this->is_env_sorting_enabled === false) { if ($this->is_env_sorting_enabled === false) {
if ($this->resource->environment_variables) { return $this->resource->environment_variables()->orderBy('order')->get();
$this->resource->environment_variables = $this->resource->environment_variables->sortBy('order')->values();
} }
if ($this->resource->environment_variables_preview) { return $this->resource->environment_variables;
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('order')->values();
}
} }
$this->getDevView(); public function getEnvironmentVariablesPreviewProperty()
{
if ($this->is_env_sorting_enabled === false) {
return $this->resource->environment_variables_preview()->orderBy('order')->get();
}
return $this->resource->environment_variables_preview;
} }
public function getDevView() public function getDevView()
{ {
$this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables); $this->variables = $this->formatEnvironmentVariables($this->environmentVariables);
if ($this->showPreview) { if ($this->showPreview) {
$this->variablesPreview = $this->formatEnvironmentVariables($this->resource->environment_variables_preview); $this->variablesPreview = $this->formatEnvironmentVariables($this->environmentVariablesPreview);
} }
} }
@@ -97,7 +100,7 @@ class All extends Component
public function switch() public function switch()
{ {
$this->view = $this->view === 'normal' ? 'dev' : 'normal'; $this->view = $this->view === 'normal' ? 'dev' : 'normal';
$this->sortEnvironmentVariables(); $this->getDevView();
} }
public function submit($data = null) public function submit($data = null)
@@ -111,7 +114,7 @@ class All extends Component
} }
$this->updateOrder(); $this->updateOrder();
$this->sortEnvironmentVariables(); $this->getDevView();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} finally { } finally {
@@ -292,7 +295,6 @@ class All extends Component
public function refreshEnvs() public function refreshEnvs()
{ {
$this->resource->refresh(); $this->resource->refresh();
$this->sortEnvironmentVariables();
$this->getDevView(); $this->getDevView();
} }
} }

View File

@@ -728,7 +728,14 @@ class Application extends BaseModel
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->where('is_preview', false) ->where('is_preview', false)
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
public function runtime_environment_variables() public function runtime_environment_variables()
@@ -749,7 +756,14 @@ class Application extends BaseModel
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->where('is_preview', true) ->where('is_preview', true)
->orderByRaw("LOWER(key) LIKE LOWER('SERVICE%') DESC, LOWER(key) ASC"); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
public function runtime_environment_variables_preview() public function runtime_environment_variables_preview()

View File

@@ -1229,14 +1229,14 @@ class Service extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
} CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
public function environment_variables_preview() WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
{ ELSE 3
return $this->morphMany(EnvironmentVariable::class, 'resourceable') END,
->where('is_preview', true) LOWER(key) ASC
->orderByRaw("LOWER(key) LIKE LOWER('SERVICE%') DESC, LOWER(key) ASC"); ");
} }
public function workdir() public function workdir()

View File

@@ -266,7 +266,14 @@ class StandaloneClickhouse extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
public function runtime_environment_variables() public function runtime_environment_variables()

View File

@@ -341,6 +341,13 @@ class StandaloneDragonfly extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
} }

View File

@@ -341,6 +341,13 @@ class StandaloneKeydb extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
} }

View File

@@ -262,7 +262,14 @@ class StandaloneMariadb extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
public function runtime_environment_variables() public function runtime_environment_variables()

View File

@@ -363,6 +363,13 @@ class StandaloneMongodb extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
} }

View File

@@ -345,6 +345,13 @@ class StandaloneMysql extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
} }

View File

@@ -296,7 +296,14 @@ class StandalonePostgresql extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
public function isBackupSolutionAvailable() public function isBackupSolutionAvailable()

View File

@@ -388,6 +388,13 @@ class StandaloneRedis extends BaseModel
public function environment_variables() public function environment_variables()
{ {
return $this->morphMany(EnvironmentVariable::class, 'resourceable') return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc'); ->orderByRaw("
CASE
WHEN LOWER(key) LIKE 'service_%' THEN 1
WHEN is_required = true AND (value IS NULL OR value = '') THEN 2
ELSE 3
END,
LOWER(key) ASC
");
} }
} }

View File

@@ -45,14 +45,7 @@
<h3>Production Environment Variables</h3> <h3>Production Environment Variables</h3>
<div>Environment (secrets) variables for Production.</div> <div>Environment (secrets) variables for Production.</div>
</div> </div>
@php @forelse ($this->environmentVariables as $env)
$requiredEmptyVars = $resource->environment_variables->filter(function ($env) {
return $env->is_required && empty($env->value);
});
$otherVars = $resource->environment_variables->diff($requiredEmptyVars);
@endphp
@forelse ($requiredEmptyVars->merge($otherVars) as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}" <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" :type="$resource->type()" /> :env="$env" :type="$resource->type()" />
@empty @empty
@@ -63,7 +56,7 @@
<h3>Preview Deployments Environment Variables</h3> <h3>Preview Deployments Environment Variables</h3>
<div>Environment (secrets) variables for Preview Deployments.</div> <div>Environment (secrets) variables for Preview Deployments.</div>
</div> </div>
@foreach ($resource->environment_variables_preview as $env) @foreach ($this->environmentVariablesPreview as $env)
<livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}" <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"
:env="$env" :type="$resource->type()" /> :env="$env" :type="$resource->type()" />
@endforeach @endforeach