using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Elsa.Dapper.HostedServices;
///
/// Runs database migrations on startup.
///
public class RunMigrationsHostedService : IHostedService
{
private readonly IServiceScopeFactory _scopeFactory;
///
/// Initializes a new instance of the class.
///
public RunMigrationsHostedService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
///
public Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _scopeFactory.CreateScope();
var runner = scope.ServiceProvider.GetRequiredService();
runner.MigrateUp();
return Task.CompletedTask;
}
///
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}