2017-09-13 08:47:34 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-09-27 15:09:59 +02:00
|
|
|
|
using Hangfire;
|
|
|
|
|
using Hangfire.MemoryStorage;
|
|
|
|
|
using Hangfire.PostgreSql;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public enum DatabaseType
|
|
|
|
|
{
|
|
|
|
|
Sqlite,
|
|
|
|
|
Postgres
|
|
|
|
|
}
|
|
|
|
|
public class ApplicationDbContextFactory
|
|
|
|
|
{
|
|
|
|
|
string _ConnectionString;
|
|
|
|
|
DatabaseType _Type;
|
|
|
|
|
public ApplicationDbContextFactory(DatabaseType type, string connectionString)
|
|
|
|
|
{
|
|
|
|
|
_ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
|
|
|
|
|
_Type = type;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public ApplicationDbContext CreateContext()
|
|
|
|
|
{
|
|
|
|
|
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
|
|
|
|
|
ConfigureBuilder(builder);
|
|
|
|
|
return new ApplicationDbContext(builder.Options);
|
|
|
|
|
}
|
2017-09-27 15:09:59 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public void ConfigureBuilder(DbContextOptionsBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
if (_Type == DatabaseType.Sqlite)
|
|
|
|
|
builder.UseSqlite(_ConnectionString);
|
|
|
|
|
else if (_Type == DatabaseType.Postgres)
|
|
|
|
|
builder.UseNpgsql(_ConnectionString);
|
|
|
|
|
}
|
2017-09-27 15:09:59 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public void ConfigureHangfireBuilder(IGlobalConfiguration builder)
|
|
|
|
|
{
|
|
|
|
|
if (_Type == DatabaseType.Sqlite)
|
|
|
|
|
builder.UseMemoryStorage(); //Sql provider does not support multiple workers
|
|
|
|
|
else if (_Type == DatabaseType.Postgres)
|
|
|
|
|
builder.UsePostgreSqlStorage(_ConnectionString);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|