From 282bb5c4cdcc1cf4080ea6c013040d955a4bcbd5 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 14 Mar 2025 16:11:31 +0100 Subject: [PATCH] fix(redis): set default redis_username for new instances Added functionality to automatically assign a default 'redis_username' of 'default' when a StandaloneRedis instance is retrieved without an existing username. This ensures that all instances have a valid username, improving consistency and reducing potential errors in subsequent operations. Additionally, updated the redisUsername method to create a runtime environment variable for 'REDIS_USERNAME' with a default value if it does not already exist, enhancing the robustness of the configuration management. --- app/Models/StandaloneRedis.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/Models/StandaloneRedis.php b/app/Models/StandaloneRedis.php index ed5cf9870..6037364fe 100644 --- a/app/Models/StandaloneRedis.php +++ b/app/Models/StandaloneRedis.php @@ -38,6 +38,12 @@ class StandaloneRedis extends BaseModel $database->forceFill(['last_online_at' => now()]); } }); + + static::retrieved(function ($database) { + if (! $database->redis_username) { + $database->redis_username = 'default'; + } + }); } protected function serverStatus(): Attribute @@ -193,8 +199,8 @@ class StandaloneRedis extends BaseModel { return Attribute::make( get: fn () => is_null($this->ports_mappings) - ? [] - : explode(',', $this->ports_mappings), + ? [] + : explode(',', $this->ports_mappings), ); } @@ -346,7 +352,12 @@ class StandaloneRedis extends BaseModel get: function () { $username = $this->runtime_environment_variables()->where('key', 'REDIS_USERNAME')->first(); if (! $username) { - return null; + $this->runtime_environment_variables()->create([ + 'key' => 'REDIS_USERNAME', + 'value' => 'default', + ]); + + return 'default'; } return $username->value;