Make sure we don't delete store on Sqlite

This commit is contained in:
nicolas.dorier
2018-07-19 21:32:33 +09:00
parent ce17e3212a
commit bbd19a96ec
5 changed files with 90 additions and 73 deletions

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework> <TargetFramework>netcoreapp2.1</TargetFramework>
<Version>1.0.2.50</Version> <Version>1.0.2.51</Version>
<NoWarn>NU1701,CA1816,CA1308,CA1810,CA2208</NoWarn> <NoWarn>NU1701,CA1816,CA1308,CA1810,CA2208</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -31,6 +31,7 @@ using System.Security.Claims;
using System.Globalization; using System.Globalization;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Data; using BTCPayServer.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace BTCPayServer namespace BTCPayServer
{ {
@@ -82,6 +83,15 @@ namespace BTCPayServer
return activeProvider != "Microsoft.EntityFrameworkCore.Sqlite"; return activeProvider != "Microsoft.EntityFrameworkCore.Sqlite";
} }
public static bool SupportDropForeignKey(this Microsoft.EntityFrameworkCore.Migrations.Migration migration, string activeProvider)
{
return activeProvider != "Microsoft.EntityFrameworkCore.Sqlite";
}
public static bool SupportDropForeignKey(this DatabaseFacade facade)
{
return facade.ProviderName != "Microsoft.EntityFrameworkCore.Sqlite";
}
public static async Task<Dictionary<uint256, TransactionResult>> GetTransactions(this BTCPayWallet client, uint256[] hashes, CancellationToken cts = default(CancellationToken)) public static async Task<Dictionary<uint256, TransactionResult>> GetTransactions(this BTCPayWallet client, uint256[] hashes, CancellationToken cts = default(CancellationToken))
{ {
hashes = hashes.Distinct().ToArray(); hashes = hashes.Distinct().ToArray();

View File

@@ -43,7 +43,7 @@ namespace BTCPayServer.HostedServices
var settings = (await _Settings.GetSettingAsync<MigrationSettings>()) ?? new MigrationSettings(); var settings = (await _Settings.GetSettingAsync<MigrationSettings>()) ?? new MigrationSettings();
if (!settings.DeprecatedLightningConnectionStringCheck) if (!settings.DeprecatedLightningConnectionStringCheck)
{ {
await DepracatedLightningConnectionStringCheck(); await DeprecatedLightningConnectionStringCheck();
settings.DeprecatedLightningConnectionStringCheck = true; settings.DeprecatedLightningConnectionStringCheck = true;
await _Settings.UpdateSetting(settings); await _Settings.UpdateSetting(settings);
} }
@@ -54,7 +54,7 @@ namespace BTCPayServer.HostedServices
await _Settings.UpdateSetting(settings); await _Settings.UpdateSetting(settings);
} }
} }
catch(Exception ex) catch (Exception ex)
{ {
Logs.PayServer.LogError(ex, "Error on the MigratorHostedService"); Logs.PayServer.LogError(ex, "Error on the MigratorHostedService");
throw; throw;
@@ -65,6 +65,8 @@ namespace BTCPayServer.HostedServices
{ {
using (var ctx = _DBContextFactory.CreateContext()) using (var ctx = _DBContextFactory.CreateContext())
{ {
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Count() == 0).ToArrayAsync()) foreach (var store in await ctx.Stores.Where(s => s.UserStores.Count() == 0).ToArrayAsync())
{ {
ctx.Stores.Remove(store); ctx.Stores.Remove(store);
@@ -73,7 +75,7 @@ namespace BTCPayServer.HostedServices
} }
} }
private async Task DepracatedLightningConnectionStringCheck() private async Task DeprecatedLightningConnectionStringCheck()
{ {
using (var ctx = _DBContextFactory.CreateContext()) using (var ctx = _DBContextFactory.CreateContext())
{ {

View File

@@ -6,89 +6,92 @@ namespace BTCPayServer.Migrations
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropForeignKey( if (this.SupportDropForeignKey(migrationBuilder.ActiveProvider))
{
migrationBuilder.DropForeignKey(
name: "FK_AddressInvoices_Invoices_InvoiceDataId", name: "FK_AddressInvoices_Invoices_InvoiceDataId",
table: "AddressInvoices"); table: "AddressInvoices");
migrationBuilder.DropForeignKey( migrationBuilder.DropForeignKey(
name: "FK_Apps_Stores_StoreDataId", name: "FK_Apps_Stores_StoreDataId",
table: "Apps"); table: "Apps");
migrationBuilder.DropForeignKey( migrationBuilder.DropForeignKey(
name: "FK_Invoices_Stores_StoreDataId", name: "FK_Invoices_Stores_StoreDataId",
table: "Invoices"); table: "Invoices");
migrationBuilder.DropForeignKey( migrationBuilder.DropForeignKey(
name: "FK_Payments_Invoices_InvoiceDataId", name: "FK_Payments_Invoices_InvoiceDataId",
table: "Payments"); table: "Payments");
migrationBuilder.DropForeignKey( migrationBuilder.DropForeignKey(
name: "FK_RefundAddresses_Invoices_InvoiceDataId", name: "FK_RefundAddresses_Invoices_InvoiceDataId",
table: "RefundAddresses"); table: "RefundAddresses");
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_AddressInvoices_Invoices_InvoiceDataId", name: "FK_AddressInvoices_Invoices_InvoiceDataId",
table: "AddressInvoices", table: "AddressInvoices",
column: "InvoiceDataId", column: "InvoiceDataId",
principalTable: "Invoices", principalTable: "Invoices",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_ApiKeys_Stores_StoreId", name: "FK_ApiKeys_Stores_StoreId",
table: "ApiKeys", table: "ApiKeys",
column: "StoreId", column: "StoreId",
principalTable: "Stores", principalTable: "Stores",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_Apps_Stores_StoreDataId", name: "FK_Apps_Stores_StoreDataId",
table: "Apps", table: "Apps",
column: "StoreDataId", column: "StoreDataId",
principalTable: "Stores", principalTable: "Stores",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_Invoices_Stores_StoreDataId", name: "FK_Invoices_Stores_StoreDataId",
table: "Invoices", table: "Invoices",
column: "StoreDataId", column: "StoreDataId",
principalTable: "Stores", principalTable: "Stores",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_PairedSINData_Stores_StoreDataId", name: "FK_PairedSINData_Stores_StoreDataId",
table: "PairedSINData", table: "PairedSINData",
column: "StoreDataId", column: "StoreDataId",
principalTable: "Stores", principalTable: "Stores",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_Payments_Invoices_InvoiceDataId", name: "FK_Payments_Invoices_InvoiceDataId",
table: "Payments", table: "Payments",
column: "InvoiceDataId", column: "InvoiceDataId",
principalTable: "Invoices", principalTable: "Invoices",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_PendingInvoices_Invoices_Id", name: "FK_PendingInvoices_Invoices_Id",
table: "PendingInvoices", table: "PendingInvoices",
column: "Id", column: "Id",
principalTable: "Invoices", principalTable: "Invoices",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey( migrationBuilder.AddForeignKey(
name: "FK_RefundAddresses_Invoices_InvoiceDataId", name: "FK_RefundAddresses_Invoices_InvoiceDataId",
table: "RefundAddresses", table: "RefundAddresses",
column: "InvoiceDataId", column: "InvoiceDataId",
principalTable: "Invoices", principalTable: "Invoices",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)

View File

@@ -174,6 +174,8 @@ namespace BTCPayServer.Services.Stores
{ {
using (var ctx = _ContextFactory.CreateContext()) using (var ctx = _ContextFactory.CreateContext())
{ {
if (!ctx.Database.SupportDropForeignKey())
return false;
var store = await ctx.Stores.FindAsync(storeId); var store = await ctx.Stores.FindAsync(storeId);
if (store == null) if (store == null)
return false; return false;