From 3af456cd356690d83a46d0eecc17de406c43b907 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 6 Jan 2026 16:18:37 +0100 Subject: [PATCH 1/2] chore: remove raw sql from env relationship - raw sql should not be used whenever possible - using sql to order on the relationship environment_variables() causes custom sorting to break or be additionally complicated --- app/Models/Application.php | 10 +--------- app/Models/Service.php | 10 +--------- app/Models/StandaloneClickhouse.php | 10 +--------- app/Models/StandaloneDragonfly.php | 10 +--------- app/Models/StandaloneKeydb.php | 10 +--------- app/Models/StandaloneMariadb.php | 10 +--------- app/Models/StandaloneMongodb.php | 10 +--------- app/Models/StandaloneMysql.php | 10 +--------- app/Models/StandalonePostgresql.php | 10 +--------- app/Models/StandaloneRedis.php | 10 +--------- 10 files changed, 10 insertions(+), 90 deletions(-) diff --git a/app/Models/Application.php b/app/Models/Application.php index 2eab8df20..59b46f7e7 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -845,15 +845,7 @@ public function detectPortFromEnvironment(?bool $isPreview = false): ?int public function environment_variables() { return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->where('is_preview', false) - ->orderByRaw(" - CASE - WHEN is_required = true THEN 1 - WHEN LOWER(key) LIKE 'service_%' THEN 2 - ELSE 3 - END, - LOWER(key) ASC - "); + ->where('is_preview', false); } public function runtime_environment_variables() diff --git a/app/Models/Service.php b/app/Models/Service.php index f28b7b635..93f274241 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -1458,15 +1458,7 @@ public function scheduled_tasks(): HasMany public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->orderByRaw(" - CASE - WHEN is_required = true THEN 1 - WHEN LOWER(key) LIKE 'service_%' THEN 2 - ELSE 3 - END, - LOWER(key) ASC - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } public function workdir() diff --git a/app/Models/StandaloneClickhouse.php b/app/Models/StandaloneClickhouse.php index ee8a27162..86323db8c 100644 --- a/app/Models/StandaloneClickhouse.php +++ b/app/Models/StandaloneClickhouse.php @@ -295,15 +295,7 @@ public function destination() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } public function runtime_environment_variables() diff --git a/app/Models/StandaloneDragonfly.php b/app/Models/StandaloneDragonfly.php index 051a95a80..4db7866b7 100644 --- a/app/Models/StandaloneDragonfly.php +++ b/app/Models/StandaloneDragonfly.php @@ -324,14 +324,6 @@ public function isBackupSolutionAvailable() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } } diff --git a/app/Models/StandaloneKeydb.php b/app/Models/StandaloneKeydb.php index 6cd51edce..f23499608 100644 --- a/app/Models/StandaloneKeydb.php +++ b/app/Models/StandaloneKeydb.php @@ -324,14 +324,6 @@ public function isBackupSolutionAvailable() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } } diff --git a/app/Models/StandaloneMariadb.php b/app/Models/StandaloneMariadb.php index f312473be..e7ba75b67 100644 --- a/app/Models/StandaloneMariadb.php +++ b/app/Models/StandaloneMariadb.php @@ -289,15 +289,7 @@ public function destination(): MorphTo public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } public function runtime_environment_variables() diff --git a/app/Models/StandaloneMongodb.php b/app/Models/StandaloneMongodb.php index d2d8fef12..d6de5874c 100644 --- a/app/Models/StandaloneMongodb.php +++ b/app/Models/StandaloneMongodb.php @@ -349,14 +349,6 @@ public function isBackupSolutionAvailable() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } } diff --git a/app/Models/StandaloneMysql.php b/app/Models/StandaloneMysql.php index c27b2a679..98a5cab77 100644 --- a/app/Models/StandaloneMysql.php +++ b/app/Models/StandaloneMysql.php @@ -328,14 +328,6 @@ public function isBackupSolutionAvailable() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } } diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.php index 73b00742a..be86438fe 100644 --- a/app/Models/StandalonePostgresql.php +++ b/app/Models/StandalonePostgresql.php @@ -323,15 +323,7 @@ public function scheduledBackups() public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } public function isBackupSolutionAvailable() diff --git a/app/Models/StandaloneRedis.php b/app/Models/StandaloneRedis.php index 46e8da434..e906bbb81 100644 --- a/app/Models/StandaloneRedis.php +++ b/app/Models/StandaloneRedis.php @@ -374,14 +374,6 @@ public function redisUsername(): Attribute public function environment_variables() { - return $this->morphMany(EnvironmentVariable::class, 'resourceable') - ->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 - "); + return $this->morphMany(EnvironmentVariable::class, 'resourceable'); } } From 8ba30d75ead1d95a41d17c3d825fd95f24559aef Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 6 Jan 2026 16:19:38 +0100 Subject: [PATCH 2/2] refactor: move all env sorting to one place --- .../Shared/EnvironmentVariable/All.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php index 07938d9d0..55e388b78 100644 --- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php +++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php @@ -63,20 +63,30 @@ public function instantSave() public function getEnvironmentVariablesProperty() { - if ($this->is_env_sorting_enabled === false) { - return $this->resource->environment_variables()->orderBy('order')->get(); + $query = $this->resource->environment_variables() + ->orderByRaw("CASE WHEN is_required = true AND (value IS NULL OR value = '') THEN 0 ELSE 1 END"); + + if ($this->is_env_sorting_enabled) { + $query->orderBy('key'); + } else { + $query->orderBy('order'); } - return $this->resource->environment_variables; + return $query->get(); } public function getEnvironmentVariablesPreviewProperty() { - if ($this->is_env_sorting_enabled === false) { - return $this->resource->environment_variables_preview()->orderBy('order')->get(); + $query = $this->resource->environment_variables_preview() + ->orderByRaw("CASE WHEN is_required = true AND (value IS NULL OR value = '') THEN 0 ELSE 1 END"); + + if ($this->is_env_sorting_enabled) { + $query->orderBy('key'); + } else { + $query->orderBy('order'); } - return $this->resource->environment_variables_preview; + return $query->get(); } public function getDevView()