save progress
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
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;
|
||||
using Npgsql;
|
||||
|
||||
namespace StellaOps.AirGap.Persistence.Extensions;
|
||||
|
||||
@@ -23,6 +26,7 @@ public static class AirGapPersistenceExtensions
|
||||
{
|
||||
services.Configure<PostgresOptions>(sectionName, configuration.GetSection(sectionName));
|
||||
services.AddSingleton<AirGapDataSource>();
|
||||
services.AddHostedService(sp => CreateMigrationHost(sp));
|
||||
services.AddScoped<IAirGapStateStore, PostgresAirGapStateStore>();
|
||||
services.AddScoped<IBundleVersionStore, PostgresBundleVersionStore>();
|
||||
|
||||
@@ -38,9 +42,46 @@ public static class AirGapPersistenceExtensions
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user