mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-12 10:30:47 +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";
|
return $"{splitted[0]}-CHAIN";
|
||||||
throw new NotSupportedException("Unknown payment id " + paymentMethodId);
|
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
|
// Make postgres happy
|
||||||
public static string SanitizeJSON(string json) => json.Replace("\\u0000", string.Empty, StringComparison.OrdinalIgnoreCase);
|
public static string SanitizeJSON(string json) => json.Replace("\\u0000", string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
|
@ -233,6 +233,12 @@ namespace BTCPayServer.Hosting
|
||||||
|
|
||||||
private async Task MigrateStoreExcludedPaymentMethods()
|
private async Task MigrateStoreExcludedPaymentMethods()
|
||||||
{
|
{
|
||||||
|
HashSet<string> oldPaymentIds = new()
|
||||||
|
{
|
||||||
|
"LightningLike",
|
||||||
|
"BTCLike",
|
||||||
|
"LNURLPAY"
|
||||||
|
};
|
||||||
await using var ctx = _DBContextFactory.CreateContext();
|
await using var ctx = _DBContextFactory.CreateContext();
|
||||||
var stores = await ctx.Stores.ToArrayAsync();
|
var stores = await ctx.Stores.ToArrayAsync();
|
||||||
foreach (var store in stores)
|
foreach (var store in stores)
|
||||||
|
@ -243,8 +249,7 @@ namespace BTCPayServer.Hosting
|
||||||
var array = blob["excludedPaymentMethods"] as JArray;
|
var array = blob["excludedPaymentMethods"] as JArray;
|
||||||
if (array is null || array.Count == 0)
|
if (array is null || array.Count == 0)
|
||||||
continue;
|
continue;
|
||||||
var newArray = new JArray(array.Select(a => MigrationExtensions.MigratePaymentMethodId(a.Value<string>()))
|
var newArray = new JArray(array.Select(a => MigrationExtensions.TryMigratePaymentMethodId(a.Value<string>())).ToArray());
|
||||||
.ToArray());
|
|
||||||
if (array.ToString() == newArray.ToString())
|
if (array.ToString() == newArray.ToString())
|
||||||
continue;
|
continue;
|
||||||
blob["excludedPaymentMethods"] = newArray;
|
blob["excludedPaymentMethods"] = newArray;
|
||||||
|
|
Loading…
Add table
Reference in a new issue