diff --git a/config/database.php b/config/database.php index 94c27f038..9238a7055 100644 --- a/config/database.php +++ b/config/database.php @@ -3,6 +3,24 @@ use Illuminate\Support\Str; use Pdo\Pgsql; +$parseDatabaseHosts = function (mixed $hosts, mixed $fallback = 'coolify-db'): array { + $parsedHosts = array_values(array_filter( + array_map('trim', explode(',', (string) $hosts)), + 'strlen', + )); + + if ($parsedHosts !== []) { + return $parsedHosts; + } + + $fallbackHosts = array_values(array_filter( + array_map('trim', explode(',', (string) $fallback)), + 'strlen', + )); + + return $fallbackHosts === [] ? ['coolify-db'] : $fallbackHosts; +}; + $pgsql = [ 'driver' => 'pgsql', 'url' => env('DATABASE_URL'), @@ -28,13 +46,13 @@ $pgsql = [ */ if (env('DB_READ_HOST')) { $pgsql['read'] = [ - 'host' => array_map('trim', explode(',', (string) env('DB_READ_HOST'))), + 'host' => $parseDatabaseHosts(env('DB_READ_HOST'), env('DB_HOST', 'coolify-db')), 'port' => env('DB_READ_PORT', env('DB_PORT', '5432')), 'username' => env('DB_READ_USERNAME', env('DB_USERNAME', 'coolify')), 'password' => env('DB_READ_PASSWORD', env('DB_PASSWORD', '')), ]; $pgsql['write'] = [ - 'host' => array_map('trim', explode(',', (string) env('DB_WRITE_HOST', env('DB_HOST', 'coolify-db')))), + 'host' => $parseDatabaseHosts(env('DB_WRITE_HOST'), env('DB_HOST', 'coolify-db')), 'port' => env('DB_WRITE_PORT', env('DB_PORT', '5432')), 'username' => env('DB_WRITE_USERNAME', env('DB_USERNAME', 'coolify')), 'password' => env('DB_WRITE_PASSWORD', env('DB_PASSWORD', '')), diff --git a/tests/Unit/DatabaseReadWriteHostConfigTest.php b/tests/Unit/DatabaseReadWriteHostConfigTest.php new file mode 100644 index 000000000..03ad1ec2f --- /dev/null +++ b/tests/Unit/DatabaseReadWriteHostConfigTest.php @@ -0,0 +1,64 @@ +clear($key); + } + + try { + foreach ($overrides as $key => $value) { + $repository->set($key, (string) $value); + } + + return require __DIR__.'/../../config/database.php'; + } finally { + foreach ($keys as $key) { + $repository->clear($key); + + if ($original[$key] !== null) { + $repository->set($key, (string) $original[$key]); + } + } + } +} + +it('trims and filters read hosts from comma separated values', function () { + $config = databaseConfigWithEnvironment([ + 'DB_READ_HOST' => ' read-1, read-2, ', + ]); + + expect($config['connections']['pgsql']['read']['host'])->toBe(['read-1', 'read-2']); +}); + +it('falls back to db host when write host is empty', function () { + $config = databaseConfigWithEnvironment([ + 'DB_HOST' => 'primary-db', + 'DB_READ_HOST' => 'read-db', + 'DB_WRITE_HOST' => '', + ]); + + expect($config['connections']['pgsql']['write']['host'])->toBe(['primary-db']); +}); + +it('falls back to the default host when write host and db host are empty', function () { + $config = databaseConfigWithEnvironment([ + 'DB_HOST' => '', + 'DB_READ_HOST' => 'read-db', + 'DB_WRITE_HOST' => '', + ]); + + expect($config['connections']['pgsql']['write']['host'])->toBe(['coolify-db']); +});