89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Npgsql;
|
|
using StellaOps.AirGap.Controller.Stores;
|
|
using StellaOps.AirGap.Importer.Versioning;
|
|
using StellaOps.AirGap.Persistence.Postgres;
|
|
using StellaOps.AirGap.Persistence.Postgres.Repositories;
|
|
using StellaOps.Infrastructure.Postgres.Options;
|
|
|
|
namespace StellaOps.AirGap.Persistence.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extension methods for configuring AirGap persistence services.
|
|
/// </summary>
|
|
public static class AirGapPersistenceExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds AirGap PostgreSQL persistence services.
|
|
/// </summary>
|
|
public static IServiceCollection AddAirGapPersistence(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
string sectionName = "Postgres:AirGap")
|
|
{
|
|
services.Configure<PostgresOptions>(sectionName, configuration.GetSection(sectionName));
|
|
services.AddSingleton<AirGapDataSource>();
|
|
services.AddHostedService(sp => CreateMigrationHost(sp));
|
|
services.AddScoped<IAirGapStateStore, PostgresAirGapStateStore>();
|
|
services.AddScoped<IBundleVersionStore, PostgresBundleVersionStore>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds AirGap PostgreSQL persistence services with explicit options.
|
|
/// </summary>
|
|
public static IServiceCollection AddAirGapPersistence(
|
|
this IServiceCollection services,
|
|
Action<PostgresOptions> configureOptions)
|
|
{
|
|
services.Configure(configureOptions);
|
|
services.AddSingleton<AirGapDataSource>();
|
|
services.AddHostedService(sp => CreateMigrationHost(sp));
|
|
services.AddScoped<IAirGapStateStore, PostgresAirGapStateStore>();
|
|
services.AddScoped<IBundleVersionStore, PostgresBundleVersionStore>();
|
|
|
|
return services;
|
|
}
|
|
|
|
private static IHostedService CreateMigrationHost(IServiceProvider serviceProvider)
|
|
{
|
|
var options = serviceProvider.GetRequiredService<Microsoft.Extensions.Options.IOptions<PostgresOptions>>().Value;
|
|
var schemaName = string.IsNullOrWhiteSpace(options.SchemaName)
|
|
? AirGapDataSource.DefaultSchemaName
|
|
: options.SchemaName!;
|
|
|
|
var connectionString = BuildMigrationConnectionString(options, schemaName);
|
|
var logger = serviceProvider.GetRequiredService<ILoggerFactory>()
|
|
.CreateLogger("Migration.AirGap.Persistence");
|
|
var lifetime = serviceProvider.GetRequiredService<IHostApplicationLifetime>();
|
|
|
|
return new AirGapStartupMigrationHost(
|
|
connectionString,
|
|
schemaName,
|
|
"AirGap.Persistence",
|
|
typeof(AirGapDataSource).Assembly,
|
|
logger,
|
|
lifetime);
|
|
}
|
|
|
|
private static string BuildMigrationConnectionString(PostgresOptions options, string schemaName)
|
|
{
|
|
var builder = new NpgsqlConnectionStringBuilder(options.ConnectionString)
|
|
{
|
|
CommandTimeout = options.CommandTimeoutSeconds
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(schemaName))
|
|
{
|
|
builder.SearchPath = $"{schemaName}, public";
|
|
}
|
|
|
|
return builder.ConnectionString;
|
|
}
|
|
}
|