using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.Auth.Abstractions.ApiKeys;
namespace ZB.MOM.WW.Auth.ApiKeys.DependencyInjection;
///
/// Runs the registered at application startup when
/// is . The migrator
/// registered in DI is whichever store provider's implementation won the TryAdd race —
/// SQLite by default, or a provider package (e.g. SQL Server) registered before
/// AddZbApiKeyAuth. The migration is idempotent, so repeated restarts are safe.
///
internal sealed class ApiKeyMigrationHostedService(
IApiKeyStoreMigrator migrator,
IOptions options) : IHostedService
{
///
public async Task StartAsync(CancellationToken cancellationToken)
{
if (options.Value.RunMigrationsOnStartup)
{
await migrator.MigrateAsync(cancellationToken).ConfigureAwait(false);
}
}
///
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}