mirror of
https://github.com/coollabsio/coolify.git
synced 2026-06-13 19:09:50 +00:00
fix(database): normalize read/write host lists
Trim comma-separated database host values and fall back to DB_HOST or the default host when write hosts are empty. Add unit coverage for read/write host parsing.
This commit is contained in:
+20
-2
@@ -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', '')),
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Env;
|
||||
|
||||
function databaseConfigWithEnvironment(array $overrides): array
|
||||
{
|
||||
$keys = [
|
||||
'DB_HOST',
|
||||
'DB_READ_HOST',
|
||||
'DB_WRITE_HOST',
|
||||
];
|
||||
|
||||
$repository = Env::getRepository();
|
||||
$original = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$original[$key] = env($key);
|
||||
$repository->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']);
|
||||
});
|
||||
Reference in New Issue
Block a user