Make sure default variable value is set and that variables are created when viewing server (#1758)

This commit is contained in:
Boy132 2025-09-29 15:14:18 +02:00 committed by GitHub
parent ec5fd3262a
commit 2fc30e14fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 10 deletions

View File

@ -14,7 +14,6 @@ use App\Models\Allocation;
use App\Models\Egg;
use App\Models\Node;
use App\Models\Server;
use App\Models\ServerVariable;
use App\Models\User;
use App\Repositories\Daemon\DaemonServerRepository;
use App\Services\Eggs\EggChangerService;
@ -637,14 +636,7 @@ class EditServer extends EditRecord
/** @var Server $server */
$server = $this->getRecord();
foreach ($server->variables as $variable) {
ServerVariable::query()->firstOrCreate([
'server_id' => $server->id,
'variable_id' => $variable->id,
], [
'variable_value' => $variable->server_value ?? '',
]);
}
$server->ensureVariablesExist();
return $query->orderByPowerJoins('variable.sort');
})

View File

@ -102,7 +102,11 @@ class Startup extends ServerFormPage
->schema([
Repeater::make('server_variables')
->hiddenLabel()
->relationship('serverVariables', fn (Builder $query) => $query->where('egg_variables.user_viewable', true)->orderByPowerJoins('variable.sort'))
->relationship('serverVariables', function (Builder $query, Server $server) {
$server->ensureVariablesExist();
return $query->where('egg_variables.user_viewable', true)->orderByPowerJoins('variable.sort');
})
->grid()
->disabled(fn (Server $server) => !auth()->user()->can(Permission::ACTION_STARTUP_UPDATE, $server))
->reorderable(false)->addable(false)->deletable(false)

View File

@ -325,6 +325,18 @@ class Server extends Model implements Validatable
return $this->hasMany(ServerVariable::class);
}
public function ensureVariablesExist(): void
{
foreach ($this->eggVariables as $variable) {
ServerVariable::firstOrCreate([
'server_id' => $this->id,
'variable_id' => $variable->id,
], [
'variable_value' => $variable->default_value,
]);
}
}
/**
* Gets information for the node associated with this server.
*/