-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginMigrationRunner.cs
54 lines (45 loc) · 1.71 KB
/
PluginMigrationRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Plugins.B2PCentral.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
namespace BTCPayServer.Plugins.B2PCentral;
public class PluginMigrationRunner : IHostedService
{
private readonly B2PCentralPluginDbContextFactory _pluginDbContextFactory;
private readonly B2PCentralPluginService _pluginService;
private readonly ISettingsRepository _settingsRepository;
public PluginMigrationRunner(
ISettingsRepository settingsRepository,
B2PCentralPluginDbContextFactory pluginDbContextFactory,
B2PCentralPluginService pluginService)
{
_settingsRepository = settingsRepository;
_pluginDbContextFactory = pluginDbContextFactory;
_pluginService = pluginService;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
var settings = await _settingsRepository.GetSettingAsync<PluginDataMigrationHistory>() ??
new PluginDataMigrationHistory();
await using var ctx = _pluginDbContextFactory.CreateContext();
await ctx.Database.MigrateAsync(cancellationToken);
// settings migrations
if (!settings.UpdatedSomething)
{
settings.UpdatedSomething = true;
await _settingsRepository.UpdateSetting(settings);
}
// test record
// await _pluginService.AddTestDataRecord();
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private class PluginDataMigrationHistory
{
public bool UpdatedSomething { get; set; }
}
}