From 2147f8ec8b1dd39e2395dbca4c5bb3e17dc1089b Mon Sep 17 00:00:00 2001 From: Kukks Date: Tue, 6 Oct 2020 17:56:55 +0200 Subject: [PATCH 1/2] Make U2F devices cascade delete fixes #1949 --- BTCPayServer.Data/Data/ApplicationDbContext.cs | 3 ++- BTCPayServer.Data/Data/U2FDevice.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/BTCPayServer.Data/Data/ApplicationDbContext.cs b/BTCPayServer.Data/Data/ApplicationDbContext.cs index 1de87caa7..0e18da7de 100644 --- a/BTCPayServer.Data/Data/ApplicationDbContext.cs +++ b/BTCPayServer.Data/Data/ApplicationDbContext.cs @@ -90,7 +90,8 @@ namespace BTCPayServer.Data PullPaymentData.OnModelCreating(builder); PayoutData.OnModelCreating(builder); RefundData.OnModelCreating(builder); - + U2FDevice.OnModelCreating(builder); + if (Database.IsSqlite() && !_designTime) { // SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations diff --git a/BTCPayServer.Data/Data/U2FDevice.cs b/BTCPayServer.Data/Data/U2FDevice.cs index abf752310..57252f0e6 100644 --- a/BTCPayServer.Data/Data/U2FDevice.cs +++ b/BTCPayServer.Data/Data/U2FDevice.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -18,5 +19,15 @@ namespace BTCPayServer.Data public string ApplicationUserId { get; set; } public ApplicationUser ApplicationUser { get; set; } + + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.ApplicationUser) + .WithMany(i => i.U2FDevices) + .HasForeignKey(i => i.ApplicationUserId).OnDelete(DeleteBehavior.Cascade); + + } } } From 9ff93ac2d551762f7610940d0d0833e7cbd7bda0 Mon Sep 17 00:00:00 2001 From: Kukks Date: Wed, 7 Oct 2020 11:08:11 +0200 Subject: [PATCH 2/2] add migration --- .../20201007090617_u2fDeviceCascade.cs | 47 +++++++++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 6 ++- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 BTCPayServer.Data/Migrations/20201007090617_u2fDeviceCascade.cs diff --git a/BTCPayServer.Data/Migrations/20201007090617_u2fDeviceCascade.cs b/BTCPayServer.Data/Migrations/20201007090617_u2fDeviceCascade.cs new file mode 100644 index 000000000..2b97a86d0 --- /dev/null +++ b/BTCPayServer.Data/Migrations/20201007090617_u2fDeviceCascade.cs @@ -0,0 +1,47 @@ +using BTCPayServer.Data; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace BTCPayServer.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20201007090617_u2fDeviceCascade")] + public partial class u2fDeviceCascade : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + if (this.SupportDropForeignKey(migrationBuilder.ActiveProvider)) + { + migrationBuilder.DropForeignKey( + name: "FK_U2FDevices_AspNetUsers_ApplicationUserId", + table: "U2FDevices"); + + migrationBuilder.AddForeignKey( + name: "FK_U2FDevices_AspNetUsers_ApplicationUserId", + table: "U2FDevices", + column: "ApplicationUserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + if (this.SupportDropForeignKey(migrationBuilder.ActiveProvider)) + { + migrationBuilder.DropForeignKey( + name: "FK_U2FDevices_AspNetUsers_ApplicationUserId", + table: "U2FDevices"); + + migrationBuilder.AddForeignKey( + name: "FK_U2FDevices_AspNetUsers_ApplicationUserId", + table: "U2FDevices", + column: "ApplicationUserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + } + } +} diff --git a/BTCPayServer.Data/Migrations/ApplicationDbContextModelSnapshot.cs b/BTCPayServer.Data/Migrations/ApplicationDbContextModelSnapshot.cs index 4e0ae306e..cbc070198 100644 --- a/BTCPayServer.Data/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/BTCPayServer.Data/Migrations/ApplicationDbContextModelSnapshot.cs @@ -525,7 +525,8 @@ namespace BTCPayServer.Migrations .HasColumnType("TEXT"); b.Property("PullPaymentDataId") - .HasColumnType("TEXT"); + .HasColumnType("TEXT") + .HasMaxLength(30); b.HasKey("InvoiceDataId", "PullPaymentDataId"); @@ -960,7 +961,8 @@ namespace BTCPayServer.Migrations { b.HasOne("BTCPayServer.Data.ApplicationUser", "ApplicationUser") .WithMany("U2FDevices") - .HasForeignKey("ApplicationUserId"); + .HasForeignKey("ApplicationUserId") + .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity("BTCPayServer.Data.UserStore", b =>