33 lines
1,002 B
C#
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;
|
|
} |