Fix potential crash in migration

This commit is contained in:
nicolas.dorier 2024-10-08 16:45:32 +09:00
parent 7c77b16517
commit 212e8c3654
No known key found for this signature in database
GPG key ID: 6618763EF09186FE
2 changed files with 24 additions and 2 deletions

View file

@ -147,6 +147,23 @@ namespace BTCPayServer.Data
return $"{splitted[0]}-CHAIN";
throw new NotSupportedException("Unknown payment id " + paymentMethodId);
}
public static string TryMigratePaymentMethodId(string paymentMethodId)
{
var splitted = paymentMethodId.Split(new[] { '_', '-' });
if (splitted is [var cryptoCode, var paymentType])
{
return paymentType switch
{
"BTCLike" => $"{cryptoCode}-CHAIN",
"LightningLike" or "LightningNetwork" => $"{cryptoCode}-LN",
"LNURLPAY" => $"{cryptoCode}-LNURL",
_ => paymentMethodId
};
}
if (splitted.Length == 1)
return $"{splitted[0]}-CHAIN";
return paymentMethodId;
}
// Make postgres happy
public static string SanitizeJSON(string json) => json.Replace("\\u0000", string.Empty, StringComparison.OrdinalIgnoreCase);

View file

@ -233,6 +233,12 @@ namespace BTCPayServer.Hosting
private async Task MigrateStoreExcludedPaymentMethods()
{
HashSet<string> oldPaymentIds = new()
{
"LightningLike",
"BTCLike",
"LNURLPAY"
};
await using var ctx = _DBContextFactory.CreateContext();
var stores = await ctx.Stores.ToArrayAsync();
foreach (var store in stores)
@ -243,8 +249,7 @@ namespace BTCPayServer.Hosting
var array = blob["excludedPaymentMethods"] as JArray;
if (array is null || array.Count == 0)
continue;
var newArray = new JArray(array.Select(a => MigrationExtensions.MigratePaymentMethodId(a.Value<string>()))
.ToArray());
var newArray = new JArray(array.Select(a => MigrationExtensions.TryMigratePaymentMethodId(a.Value<string>())).ToArray());
if (array.ToString() == newArray.ToString())
continue;
blob["excludedPaymentMethods"] = newArray;