Path: redb-route/tests/Shared/TestDatabases.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/Shared/TestDatabases.cs
----
using System.Data.Common;
namespace redb.Route.Tests.Shared;
///
/// Where the database suites find PostgreSQL and SQL Server. The connection strings in the tests and their
/// appsettings stay as they are; REDB_TEST_PG (host:port) and REDB_TEST_MSSQL (host,port)
/// move them to another server, for example the release gate's fresh test databases instead of a machine's permanent
/// ones. Without the variables a connection string comes back unchanged. A variable that carries a whole connection
/// string of its own (ROUTE_SQL_PG_CS, REDB_MSSQL_CS and the like) still wins: these apply only to the defaults.
/// REDB_TEST_MSSQL_DATABASE also replaces the database of a SQL Server string: the Linux gate runs the suites on
/// the host's permanent SQL Server, in a database of the run, never in the shared redb the strings name.
///
internal static class TestDatabases
{
public const string PostgresVariable = "REDB_TEST_PG";
public const string MsSqlVariable = "REDB_TEST_MSSQL";
public const string MsSqlDatabaseVariable = "REDB_TEST_MSSQL_DATABASE";
/// The PostgreSQL connection string, with host and port from REDB_TEST_PG when it is set.
public static string Postgres(string connectionString)
=> Postgres(connectionString, Environment.GetEnvironmentVariable(PostgresVariable));
/// The PostgreSQL connection string moved to (host:port); null or empty keeps it.
public static string Postgres(string connectionString, string? target)
{
target = target?.Trim();
if (string.IsNullOrEmpty(target)) return connectionString;
var colon = target.LastIndexOf(':');
var host = colon > 0 ? target[..colon] : target;
var port = colon > 0 ? target[(colon + 1)..] : "5432";
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };
foreach (var key in new[] { "Host", "Server", "Port" })
builder.Remove(key);
builder["Host"] = host;
builder["Port"] = port;
return builder.ConnectionString;
}
///
/// The SQL Server connection string, with its server from REDB_TEST_MSSQL and its database from
/// REDB_TEST_MSSQL_DATABASE when they are set.
///
public static string MsSql(string connectionString)
=> MsSql(connectionString, Environment.GetEnvironmentVariable(MsSqlVariable),
Environment.GetEnvironmentVariable(MsSqlDatabaseVariable));
///
/// The SQL Server connection string moved to (host,port) and to
/// ; null or empty keeps that part.
///
public static string MsSql(string connectionString, string? target, string? database = null)
{
target = target?.Trim();
database = database?.Trim();
if (string.IsNullOrEmpty(target) && string.IsNullOrEmpty(database)) return connectionString;
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };
if (!string.IsNullOrEmpty(target))
{
foreach (var key in new[] { "Server", "Data Source", "Address", "Addr", "Network Address" })
builder.Remove(key);
builder["Server"] = target;
}
if (!string.IsNullOrEmpty(database))
{
foreach (var key in new[] { "Database", "Initial Catalog" })
builder.Remove(key);
builder["Database"] = database;
}
return builder.ConnectionString;
}
}