Test
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using Cloud;
|
||||
using DatabaseTest.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json;
|
||||
using Cloud.Models;
|
||||
|
||||
namespace DatabaseTest.Services;
|
||||
|
||||
public interface IDatabaseConfigService
|
||||
{
|
||||
DatabaseProviderType ProviderType { get; }
|
||||
string ConnectionString { get; }
|
||||
string InMemoryDbName { get; }
|
||||
event Action? OnConfigurationChanged;
|
||||
|
||||
void SetProvider(DatabaseProviderType providerType);
|
||||
void SetConnectionString(string connectionString);
|
||||
void SetInMemoryDbName(string name);
|
||||
AppDbContext CreateDbContext();
|
||||
string GetActiveProviderDescription();
|
||||
}
|
||||
|
||||
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 configuredConn = _configuration.GetConnectionString("DefaultConnection")
|
||||
?? _configuration.GetConnectionString("Postgres")
|
||||
?? Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING")
|
||||
?? "Host=localhost;Port=5432;Database=chrono;Username=postgres;Password=postgres";
|
||||
|
||||
_connectionString = configuredConn;
|
||||
_providerType = DatabaseProviderType.PostgreSQL;
|
||||
}
|
||||
|
||||
public DatabaseProviderType ProviderType => _providerType;
|
||||
public string ConnectionString => _connectionString;
|
||||
public string InMemoryDbName => _inMemoryDbName;
|
||||
|
||||
public void SetProvider(DatabaseProviderType providerType)
|
||||
{
|
||||
if (_providerType != providerType)
|
||||
{
|
||||
_providerType = providerType;
|
||||
OnConfigurationChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetConnectionString(string connectionString)
|
||||
{
|
||||
if (_connectionString != connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
OnConfigurationChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetInMemoryDbName(string name)
|
||||
{
|
||||
if (_inMemoryDbName != name)
|
||||
{
|
||||
_inMemoryDbName = name;
|
||||
OnConfigurationChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public string GetActiveProviderDescription()
|
||||
{
|
||||
return _providerType switch
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.UseInMemoryDatabase(_inMemoryDbName);
|
||||
}
|
||||
|
||||
return new AppDbContext(builder.Options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user