elsa-core/src/modules/Elsa.Dapper/HostedServices/RunMigrationsHostedService.cs
Sipke Schoorstra 6c1cca372c
Dapper persistence provider (#4109)
* Implement Dapper stores
2023-06-06 20:49:14 +02:00

33 lines
1,002 B
C#

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