This commit is contained in:
6d486f49
2026-08-17 00:45:08 -04:00
parent 3a2f112a73
commit 1d6207fbfe
154 changed files with 126007 additions and 47385 deletions
@@ -1,8 +1,8 @@
using System.Text.RegularExpressions;
using Cloud;
using DatabaseTest.Models;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
using Cloud.Models;
using Npgsql;
namespace DatabaseTest.Services;
@@ -23,89 +23,118 @@ public interface IDatabaseConfigService
public class DatabaseConfigService : IDatabaseConfigService
{
private readonly IConfiguration _configuration;
private DatabaseProviderType _providerType;
private string _connectionString;
private string _inMemoryDbName = "chrono_database_test";
public event Action? OnConfigurationChanged;
public DatabaseConfigService(IConfiguration configuration)
{
_configuration = configuration;
var envPassword = Environment.GetEnvironmentVariable("PostgreSQL_Password")
?? (OperatingSystem.IsWindows()
? Environment.GetEnvironmentVariable("PostgreSQL_Password",
EnvironmentVariableTarget.User)
: null)
?? (OperatingSystem.IsWindows()
? Environment.GetEnvironmentVariable("PostgreSQL_Password",
EnvironmentVariableTarget.Machine)
: null)
?? Environment.GetEnvironmentVariable("POSTGRESQL_PASSWORD")
?? Environment.GetEnvironmentVariable("POSTGRES_PASSWORD");
var configuredConn = _configuration.GetConnectionString("DefaultConnection")
?? _configuration.GetConnectionString("Postgres")
?? Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING")
?? "Host=localhost;Port=5432;Database=chrono;Username=postgres;Password=postgres";
?? Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING");
_connectionString = configuredConn;
_providerType = DatabaseProviderType.PostgreSQL;
if (string.IsNullOrWhiteSpace(configuredConn))
{
var csb = new NpgsqlConnectionStringBuilder
{
Host = "localhost",
Database = "chrono",
Username = "postgres",
Password = envPassword
};
configuredConn = csb.ConnectionString;
}
else if (!string.IsNullOrEmpty(envPassword))
{
try
{
var csb = new NpgsqlConnectionStringBuilder(configuredConn)
{
Password = envPassword
};
configuredConn = csb.ConnectionString;
}
catch
{
}
}
ConnectionString = configuredConn;
ProviderType = DatabaseProviderType.PostgreSQL;
}
public DatabaseProviderType ProviderType => _providerType;
public string ConnectionString => _connectionString;
public string InMemoryDbName => _inMemoryDbName;
public event Action? OnConfigurationChanged;
public DatabaseProviderType ProviderType { get; private set; }
public string ConnectionString { get; private set; }
public string InMemoryDbName { get; private set; } = "chrono_database_test";
public void SetProvider(DatabaseProviderType providerType)
{
if (_providerType != providerType)
if (ProviderType != providerType)
{
_providerType = providerType;
ProviderType = providerType;
OnConfigurationChanged?.Invoke();
}
}
public void SetConnectionString(string connectionString)
{
if (_connectionString != connectionString)
if (ConnectionString != connectionString)
{
_connectionString = connectionString;
ConnectionString = connectionString;
OnConfigurationChanged?.Invoke();
}
}
public void SetInMemoryDbName(string name)
{
if (_inMemoryDbName != name)
if (InMemoryDbName != name)
{
_inMemoryDbName = name;
InMemoryDbName = name;
OnConfigurationChanged?.Invoke();
}
}
public string GetActiveProviderDescription()
{
return _providerType switch
return ProviderType switch
{
DatabaseProviderType.PostgreSQL => $"PostgreSQL ({GetMaskedConnectionString(_connectionString)})",
DatabaseProviderType.InMemory => $"In-Memory Test Database ({_inMemoryDbName})",
_ => _providerType.ToString()
DatabaseProviderType.PostgreSQL => $"PostgreSQL ({GetMaskedConnectionString(ConnectionString)})",
DatabaseProviderType.InMemory => $"In-Memory Test Database ({InMemoryDbName})",
_ => ProviderType.ToString()
};
}
private static string GetMaskedConnectionString(string conn)
{
if (string.IsNullOrWhiteSpace(conn)) return "Not configured";
// Hide password in display
return System.Text.RegularExpressions.Regex.Replace(conn, @"(?i)Password=[^;]+", "Password=******");
}
public AppDbContext CreateDbContext()
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
if (_providerType == DatabaseProviderType.PostgreSQL)
{
builder.UseNpgsql(_connectionString, npgsqlOptions =>
{
npgsqlOptions.CommandTimeout(15);
});
}
if (ProviderType == DatabaseProviderType.PostgreSQL)
builder.UseNpgsql(ConnectionString, npgsqlOptions => { npgsqlOptions.CommandTimeout(15); });
else
{
builder.UseInMemoryDatabase(_inMemoryDbName);
}
builder.UseInMemoryDatabase(InMemoryDbName);
return new AppDbContext(builder.Options);
}
}
private static string GetMaskedConnectionString(string conn)
{
if (string.IsNullOrWhiteSpace(conn)) return "Not configured";
// Hide password in display
return Regex.Replace(conn, @"(?i)Password=[^;]+", "Password=******");
}
}