Add postgres support

This commit is contained in:
NicolasDorier 2017-09-27 22:09:59 +09:00
parent d499ef0977
commit 6b7cc77667
10 changed files with 67 additions and 30 deletions

View file

@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Hangfire" Version="1.6.17" />
<PackageReference Include="Hangfire.MemoryStorage" Version="1.5.1" />
<PackageReference Include="Hangfire.PostgreSql" Version="1.4.8.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.2" />
<PackageReference Include="NBitcoin" Version="4.0.0.38" />
<PackageReference Include="NBitpayClient" Version="1.0.0.9" />
@ -20,6 +21,7 @@
<PackageReference Include="NicolasDorier.CommandLine" Version="1.0.0.1" />
<PackageReference Include="NicolasDorier.CommandLine.Configuration" Version="1.0.0.2" />
<PackageReference Include="NicolasDorier.StandardConfiguration" Version="1.0.0.11" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="System.Xml.XmlSerializer" Version="4.0.11" />
</ItemGroup>
@ -36,10 +38,6 @@
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
<ItemGroup>
<None Include="wwwroot\img\bitcoin-symbol.svg" />
<None Include="wwwroot\js\core.js" />

View file

@ -56,11 +56,17 @@ namespace BTCPayServer.Configuration
Explorer = conf.GetOrDefault<Uri>("explorer.url", networkInfo.DefaultExplorerUrl);
CookieFile = conf.GetOrDefault<string>("explorer.cookiefile", networkInfo.DefaultExplorerCookieFile);
RequireHttps = conf.GetOrDefault<bool>("requirehttps", false);
PostgresConnectionString = conf.GetOrDefault<string>("postgres", null);
}
public bool RequireHttps
{
get; set;
}
public string PostgresConnectionString
{
get;
set;
}
}
}

View file

@ -56,7 +56,11 @@ namespace BTCPayServer.Configuration
db = new DBreezeEngine(CreateDBPath(opts, "InvoiceDB"));
_Resources.Add(db);
var dbContext = new ApplicationDbContextFactory(Path.Combine(opts.DataDir, "sqllite.db"));
ApplicationDbContextFactory dbContext = null;
if(opts.PostgresConnectionString == null)
dbContext = new ApplicationDbContextFactory(DatabaseType.Sqlite, "Data Source=" + Path.Combine(opts.DataDir, "sqllite.db"));
else
dbContext = new ApplicationDbContextFactory(DatabaseType.Postgres, opts.PostgresConnectionString);
DBFactory = dbContext;
InvoiceRepository = new InvoiceRepository(dbContext, db, Network);

View file

@ -27,6 +27,7 @@ namespace BTCPayServer.Configuration
app.Option("--testnet | -testnet", $"Use testnet", CommandOptionType.BoolValue);
app.Option("--regtest | -regtest", $"Use regtest", CommandOptionType.BoolValue);
app.Option("--requirehttps", $"Will redirect to https version of the website (default: false)", CommandOptionType.BoolValue);
app.Option("--postgres", $"Connection string to postgres database (default: sqlite is used)", CommandOptionType.SingleValue);
app.Option("--explorerurl", $"Url of the NBxplorer (default: : Default setting of NBXplorer for the network)", CommandOptionType.SingleValue);
app.Option("--explorercookiefile", $"Path to the cookie file (default: Default setting of NBXplorer for the network)", CommandOptionType.SingleValue);
@ -78,6 +79,9 @@ namespace BTCPayServer.Configuration
builder.AppendLine("#port=" + network.DefaultPort);
builder.AppendLine("#bind=127.0.0.1");
builder.AppendLine();
builder.AppendLine("### Database ###");
builder.AppendLine("#postgres=User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;");
builder.AppendLine();
builder.AppendLine("### NBXplorer settings ###");
builder.AppendLine("#explorer.url=" + network.DefaultExplorerUrl.AbsoluteUri);
builder.AppendLine("#explorer.cookiefile=" + network.DefaultExplorerCookieFile);

View file

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using BTCPayServer.Models;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace BTCPayServer.Data
{
@ -52,8 +53,8 @@ namespace BTCPayServer.Data
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var options = optionsBuilder.Options.FindExtension<SqliteOptionsExtension>();
if(options?.ConnectionString == null)
var isConfigured = optionsBuilder.Options.Extensions.OfType<RelationalOptionsExtension>().Any();
if(!isConfigured)
optionsBuilder.UseSqlite("Data Source=temp.db");
}
@ -70,7 +71,8 @@ namespace BTCPayServer.Data
.HasIndex(o => o.InvoiceDataId);
builder.Entity<UserStore>()
.HasKey(t => new {
.HasKey(t => new
{
t.ApplicationUserId,
t.StoreDataId
});

View file

@ -3,22 +3,48 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Hangfire;
using Hangfire.MemoryStorage;
using Hangfire.PostgreSql;
namespace BTCPayServer.Data
{
public enum DatabaseType
{
Sqlite,
Postgres
}
public class ApplicationDbContextFactory
{
string _Path;
public ApplicationDbContextFactory(string path)
string _ConnectionString;
DatabaseType _Type;
public ApplicationDbContextFactory(DatabaseType type, string connectionString)
{
_Path = path ?? throw new ArgumentNullException(nameof(path));
_ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_Type = type;
}
public ApplicationDbContext CreateContext()
{
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlite("Data Source=" + _Path);
ConfigureBuilder(builder);
return new ApplicationDbContext(builder.Options);
}
public void ConfigureBuilder(DbContextOptionsBuilder builder)
{
if(_Type == DatabaseType.Sqlite)
builder.UseSqlite(_ConnectionString);
else if(_Type == DatabaseType.Postgres)
builder.UseNpgsql(_ConnectionString);
}
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);
}
}
}

View file

@ -92,8 +92,8 @@ namespace BTCPayServer.Hosting
{
services.AddDbContext<ApplicationDbContext>((provider, o) =>
{
var path = Path.Combine(provider.GetRequiredService<BTCPayServerOptions>().DataDir, "sqllite.db");
o.UseSqlite("Data Source=" + path);
var factory = provider.GetRequiredService<ApplicationDbContextFactory>();
factory.ConfigureBuilder(o);
});
services.TryAddSingleton<SettingsRepository>();
services.TryAddSingleton<InvoicePaymentNotification>();

View file

@ -19,7 +19,6 @@ using Microsoft.AspNetCore.Identity;
using BTCPayServer.Data;
using Microsoft.Extensions.Logging;
using Hangfire;
using Hangfire.MemoryStorage;
using BTCPayServer.Logging;
using Microsoft.AspNetCore.Authorization;
using System.Threading.Tasks;
@ -82,10 +81,9 @@ namespace BTCPayServer.Hosting
Action<IGlobalConfiguration> configuration = o =>
{
var scope = AspNetCoreJobActivator.Current.BeginScope(null);
var options = (ApplicationDbContext)scope.Resolve(typeof(ApplicationDbContext));
var path = Path.Combine(((BTCPayServerOptions)scope.Resolve(typeof(BTCPayServerOptions))).DataDir, "hangfire.db");
o.UseMemoryStorage(); //SQLite provider can work with only one background job :/
};
var options = (ApplicationDbContextFactory)scope.Resolve(typeof(ApplicationDbContextFactory));
options.ConfigureHangfireBuilder(o);
};
ServiceCollectionDescriptorExtensions.TryAddSingleton<Action<IGlobalConfiguration>>(services, (IServiceProvider serviceProvider) => new Action<IGlobalConfiguration>((config) =>
{

View file

@ -30,16 +30,16 @@ namespace BTCPayServer.Migrations
AccessFailedCount = table.Column<int>(type: "INTEGER", nullable: false),
ConcurrencyStamp = table.Column<string>(type: "TEXT", nullable: true),
Email = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(type: "INTEGER", nullable: false),
LockoutEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(type: "TEXT", nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
LockoutEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
NormalizedEmail = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
PasswordHash = table.Column<string>(type: "TEXT", nullable: true),
PhoneNumber = table.Column<string>(type: "TEXT", nullable: true),
PhoneNumberConfirmed = table.Column<bool>(type: "INTEGER", nullable: false),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
SecurityStamp = table.Column<string>(type: "TEXT", nullable: true),
TwoFactorEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
UserName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true)
},
constraints: table =>
@ -54,7 +54,7 @@ namespace BTCPayServer.Migrations
Id = table.Column<string>(type: "TEXT", nullable: false),
DerivationStrategy = table.Column<string>(type: "TEXT", nullable: true),
SpeedPolicy = table.Column<int>(type: "INTEGER", nullable: false),
StoreCertificate = table.Column<byte[]>(type: "BLOB", nullable: true),
StoreCertificate = table.Column<byte[]>(nullable: true),
StoreName = table.Column<string>(type: "TEXT", nullable: true),
StoreWebsite = table.Column<string>(type: "TEXT", nullable: true)
},
@ -174,8 +174,8 @@ namespace BTCPayServer.Migrations
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Blob = table.Column<byte[]>(type: "BLOB", nullable: true),
Created = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
Blob = table.Column<byte[]>(nullable: true),
Created = table.Column<DateTimeOffset>(nullable: false),
CustomerEmail = table.Column<string>(type: "TEXT", nullable: true),
ExceptionStatus = table.Column<string>(type: "TEXT", nullable: true),
ItemCode = table.Column<string>(type: "TEXT", nullable: true),
@ -224,7 +224,7 @@ namespace BTCPayServer.Migrations
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Blob = table.Column<byte[]>(type: "BLOB", nullable: true),
Blob = table.Column<byte[]>(nullable: true),
InvoiceDataId = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
@ -243,7 +243,7 @@ namespace BTCPayServer.Migrations
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Blob = table.Column<byte[]>(type: "BLOB", nullable: true),
Blob = table.Column<byte[]>(nullable: true),
InvoiceDataId = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>

View file

@ -11,7 +11,6 @@ namespace BTCPayServer.Migrations
migrationBuilder.AddColumn<bool>(
name: "RequiresEmailConfirmation",
table: "AspNetUsers",
type: "INTEGER",
nullable: false,
defaultValue: false);
}