From 57851cef9a8dac0da84741ea9637671cf2f2de19 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 4 Apr 2024 22:50:12 +0900 Subject: [PATCH] Fix migration crash --- .../Data/InvoiceData.Migration.cs | 18 ++++++++++-------- .../Data/PaymentData.Migration.cs | 2 +- .../TestData/InvoiceMigrationTestVectors.json | 4 ---- BTCPayServer/Data/InvoiceDataExtensions.cs | 3 ++- .../Services/Invoices/InvoiceEntity.cs | 1 + .../Services/Invoices/InvoiceRepository.cs | 1 - 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/BTCPayServer.Data/Data/InvoiceData.Migration.cs b/BTCPayServer.Data/Data/InvoiceData.Migration.cs index 919d0145c..afb470e33 100644 --- a/BTCPayServer.Data/Data/InvoiceData.Migration.cs +++ b/BTCPayServer.Data/Data/InvoiceData.Migration.cs @@ -14,6 +14,7 @@ using Newtonsoft.Json; using Microsoft.EntityFrameworkCore.Diagnostics; using BTCPayServer.Migrations; using Newtonsoft.Json.Serialization; +using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; namespace BTCPayServer.Data { @@ -28,11 +29,11 @@ namespace BTCPayServer.Data public static readonly MigrationInterceptor Instance = new MigrationInterceptor(); public object InitializedInstance(MaterializationInterceptionData materializationData, object entity) { - if (entity is InvoiceData invoiceData) + if (entity is InvoiceData invoiceData && invoiceData.Currency is null) { invoiceData.Migrate(); } - else if (entity is PaymentData paymentData) + else if (entity is PaymentData paymentData && paymentData.Currency is null) { paymentData.Migrate(); } @@ -65,7 +66,8 @@ namespace BTCPayServer.Data "derivationStrategy", "archived", "isUnderPaid", - "requiresRefundEmail" + "requiresRefundEmail", + "invoiceTime" }; #pragma warning disable CS0618 // Type or member is obsolete @@ -73,13 +75,13 @@ namespace BTCPayServer.Data { if (Currency is not null) return; - if (Blob is not null) + if (Blob is not (null or { Length: 0 })) { Blob2 = MigrationExtensions.Unzip(Blob); Blob = null; } var blob = JObject.Parse(Blob2); - if (blob["cryptoData"]?["BTC"] is not null) + if (blob["cryptoData"]?["BTC"] is not (null or { Type: JTokenType.Null })) { blob.Move(["rate"], ["cryptoData", "BTC", "rate"]); blob.Move(["txFee"], ["cryptoData", "BTC", "txFee"]); @@ -280,7 +282,7 @@ namespace BTCPayServer.Data var divisibility = MigrationExtensions.GetDivisibility(prop.Name); prompt.Add("divisibility", divisibility); - if (prompt["paymentMethodFee"] is { } paymentMethodFee) + if (prompt["paymentMethodFee"] is { Type: JTokenType.Integer } paymentMethodFee) { prompt["paymentMethodFee"] = ((decimal)paymentMethodFee.Value() / (decimal)Math.Pow(10, divisibility)).ToString(CultureInfo.InvariantCulture); prompt.RemoveIfValue("paymentMethodFee", "0"); @@ -304,7 +306,7 @@ namespace BTCPayServer.Data var details = prompt["details"] as JObject ?? new JObject(); details.RemoveIfValue("payjoinEnabled", false); details.RemoveIfNull("feeMode"); - if (details["feeMode"] is not null) + if (details["feeMode"] is not (null or { Type: JTokenType.Null })) { details["feeMode"] = details["feeMode"].Value() switch { @@ -341,7 +343,7 @@ namespace BTCPayServer.Data } } - if (blob["defaultPaymentMethod"] is not null) + if (blob["defaultPaymentMethod"] is not (null or { Type : JTokenType.Null })) blob["defaultPaymentMethod"] = MigrationExtensions.MigratePaymentMethodId(blob["defaultPaymentMethod"].Value()); blob.Remove("derivationStrategies"); diff --git a/BTCPayServer.Data/Data/PaymentData.Migration.cs b/BTCPayServer.Data/Data/PaymentData.Migration.cs index 994b98fbf..207332083 100644 --- a/BTCPayServer.Data/Data/PaymentData.Migration.cs +++ b/BTCPayServer.Data/Data/PaymentData.Migration.cs @@ -20,7 +20,7 @@ namespace BTCPayServer.Data #pragma warning disable CS0618 // Type or member is obsolete if (Currency is not null) return; - if (Blob is not null) + if (Blob is not (null or { Length: 0 })) { Blob2 = MigrationExtensions.Unzip(Blob); Blob = null; diff --git a/BTCPayServer.Tests/TestData/InvoiceMigrationTestVectors.json b/BTCPayServer.Tests/TestData/InvoiceMigrationTestVectors.json index 3b73dcd26..0623dbfca 100644 --- a/BTCPayServer.Tests/TestData/InvoiceMigrationTestVectors.json +++ b/BTCPayServer.Tests/TestData/InvoiceMigrationTestVectors.json @@ -82,7 +82,6 @@ "destination": "39mWwvUDoZ5CxXa6CmgaUda19qYj9LpQD1" } }, - "invoiceTime": 1538395793, "redirectURL": "https://example.com/thanksyou", "receiptOptions": {}, "internalTags": [], @@ -202,7 +201,6 @@ } }, "receiptOptions": {}, - "invoiceTime": 1709806449, "redirectURL": "https://test/", "speedPolicy": 1, "internalTags": [], @@ -437,7 +435,6 @@ "details": {} } }, - "invoiceTime": 1709864059, "speedPolicy": 1, "expirationTime": 1709864959, "receiptOptions": {}, @@ -574,7 +571,6 @@ } } }, - "invoiceTime": 1697828706, "speedPolicy": 1, "internalTags": [], "expirationTime": 1697829606, diff --git a/BTCPayServer/Data/InvoiceDataExtensions.cs b/BTCPayServer/Data/InvoiceDataExtensions.cs index e051cb905..d53ca2d26 100644 --- a/BTCPayServer/Data/InvoiceDataExtensions.cs +++ b/BTCPayServer/Data/InvoiceDataExtensions.cs @@ -21,6 +21,7 @@ namespace BTCPayServer.Data { if (blob.Metadata is null) blob.Metadata = new InvoiceMetadata(); + invoiceData.Created = blob.InvoiceTime; invoiceData.Currency = blob.Currency; invoiceData.Amount = blob.Price; invoiceData.HasTypedBlob().SetBlob(blob, DefaultSerializer); @@ -41,7 +42,7 @@ namespace BTCPayServer.Data { entity.Price = price; } - + entity.InvoiceTime = invoiceData.Created; entity.StoreId = invoiceData.StoreDataId; entity.ExceptionStatus = state.ExceptionStatus; entity.Status = state.Status; diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index 0b1495f1a..ba60f2b8f 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -277,6 +277,7 @@ namespace BTCPayServer.Services.Invoices public SpeedPolicy SpeedPolicy { get; set; } [JsonProperty] public string DefaultLanguage { get; set; } + [JsonIgnore] public DateTimeOffset InvoiceTime { get; set; } public DateTimeOffset ExpirationTime { get; set; } public InvoiceMetadata Metadata { get; set; } diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index bdf5684c9..9e13b227c 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -228,7 +228,6 @@ namespace BTCPayServer.Services.Invoices { StoreDataId = invoice.StoreId, Id = invoice.Id, - Created = invoice.InvoiceTime, OrderId = invoice.Metadata.OrderId, #pragma warning disable CS0618 // Type or member is obsolete Status = invoice.StatusString,