mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-11 01:35:22 +01:00
Fix potential crash in migration
This commit is contained in:
parent
7c77b16517
commit
212e8c3654
2 changed files with 24 additions and 2 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue