Remove dead code from multi db support (#5971)

This commit is contained in:
Nicolas Dorier 2024-05-07 09:21:49 +09:00 committed by GitHub
parent a89c0d4797
commit 8bb4ceaaac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 98 additions and 214 deletions

View file

@ -16,8 +16,6 @@ namespace BTCPayServer.Migrations
public partial class WebhookDeliveriesCleanup : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql("DROP TABLE IF EXISTS \"InvoiceWebhookDeliveries\", \"WebhookDeliveries\";");
@ -73,7 +71,6 @@ namespace BTCPayServer.Migrations
onDelete: ReferentialAction.Cascade);
});
}
}
protected override void Down(MigrationBuilder migrationBuilder)
{

View file

@ -12,8 +12,6 @@ namespace BTCPayServer.Migrations
public partial class removecurrentrefund : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.DropForeignKey(
name: "FK_Invoices_Refunds_Id_CurrentRefundId",
@ -27,7 +25,6 @@ namespace BTCPayServer.Migrations
name: "CurrentRefundId",
table: "Invoices");
}
}
protected override void Down(MigrationBuilder migrationBuilder)
{

View file

@ -12,12 +12,9 @@ namespace BTCPayServer.Migrations
public partial class appssettingstojson : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql("ALTER TABLE \"Apps\" ALTER COLUMN \"Settings\" TYPE JSONB USING \"Settings\"::JSONB");
}
}
protected override void Down(MigrationBuilder migrationBuilder)
{

View file

@ -12,12 +12,9 @@ namespace BTCPayServer.Migrations
public partial class FixWalletObjectsWithEmptyWalletId : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql("DELETE FROM \"WalletObjects\" WHERE \"WalletId\"='';");
}
}
protected override void Down(MigrationBuilder migrationBuilder)
{

View file

@ -12,14 +12,11 @@ namespace BTCPayServer.Migrations
public partial class PayoutAndPullPaymentToJsonBlob : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql("ALTER TABLE \"Payouts\" ALTER COLUMN \"Blob\" TYPE JSONB USING regexp_replace(convert_from(\"Blob\",'UTF8'), '\\\\u0000', '', 'g')::JSONB");
migrationBuilder.Sql("ALTER TABLE \"Payouts\" ALTER COLUMN \"Proof\" TYPE JSONB USING regexp_replace(convert_from(\"Proof\",'UTF8'), '\\\\u0000', '', 'g')::JSONB");
migrationBuilder.Sql("ALTER TABLE \"PullPayments\" ALTER COLUMN \"Blob\" TYPE JSONB USING regexp_replace(convert_from(\"Blob\",'UTF8'), '\\\\u0000', '', 'g')::JSONB");
}
}
protected override void Down(MigrationBuilder migrationBuilder)
{

View file

@ -1,4 +1,4 @@
using BTCPayServer.Data;
using BTCPayServer.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
@ -12,24 +12,16 @@ namespace BTCPayServer.Migrations
[Migration("20240229092905_AddManagerAndEmployeeToStoreRoles")]
public partial class AddManagerAndEmployeeToStoreRoles : Migration
{
object GetPermissionsData(MigrationBuilder migrationBuilder, string[] permissions)
{
return migrationBuilder.IsNpgsql()
? permissions
: JsonConvert.SerializeObject(permissions);
}
protected override void Up(MigrationBuilder migrationBuilder)
{
var permissionsType = migrationBuilder.IsNpgsql() ? "TEXT[]" : "TEXT";
migrationBuilder.InsertData(
"StoreRoles",
columns: new[] { "Id", "Role", "Permissions" },
columnTypes: new[] { "TEXT", "TEXT", permissionsType },
columnTypes: new[] { "TEXT", "TEXT", "TEXT[]" },
values: new object[,]
{
{
"Manager", "Manager", GetPermissionsData(migrationBuilder, new[]
"Manager", "Manager", new[]
{
"btcpay.store.canviewstoresettings",
"btcpay.store.canmodifyinvoices",
@ -37,17 +29,17 @@ namespace BTCPayServer.Migrations
"btcpay.store.canmodifypaymentrequests",
"btcpay.store.canmanagepullpayments",
"btcpay.store.canmanagepayouts"
})
}
},
{
"Employee", "Employee", GetPermissionsData(migrationBuilder, new[]
"Employee", "Employee", new[]
{
"btcpay.store.canmodifyinvoices",
"btcpay.store.canmodifypaymentrequests",
"btcpay.store.cancreatenonapprovedpullpayments",
"btcpay.store.canviewpayouts",
"btcpay.store.canviewpullpayments"
})
}
}
});
@ -57,16 +49,16 @@ namespace BTCPayServer.Migrations
keyColumnTypes: new[] { "TEXT" },
keyValues: new[] { "Guest" },
columns: new[] { "Permissions" },
columnTypes: new[] { permissionsType },
columnTypes: new[] { "TEXT[]" },
values: new object[]
{
GetPermissionsData(migrationBuilder, new[]
new[]
{
"btcpay.store.canmodifyinvoices",
"btcpay.store.canviewpaymentrequests",
"btcpay.store.canviewpullpayments",
"btcpay.store.canviewpayouts"
})
}
});
}
@ -75,23 +67,22 @@ namespace BTCPayServer.Migrations
migrationBuilder.DeleteData("StoreRoles", "Id", "Manager");
migrationBuilder.DeleteData("StoreRoles", "Id", "Employee");
var permissionsType = migrationBuilder.IsNpgsql() ? "TEXT[]" : "TEXT";
migrationBuilder.UpdateData(
"StoreRoles",
keyColumns: new[] { "Id" },
keyColumnTypes: new[] { "TEXT" },
keyValues: new[] { "Guest" },
columns: new[] { "Permissions" },
columnTypes: new[] { permissionsType },
columnTypes: new[] { "TEXT[]" },
values: new object[]
{
GetPermissionsData(migrationBuilder, new[]
new[]
{
"btcpay.store.canviewstoresettings",
"btcpay.store.canmodifyinvoices",
"btcpay.store.canviewcustodianaccounts",
"btcpay.store.candeposittocustodianaccount"
})
}
});
}
}

View file

@ -18,7 +18,7 @@ namespace BTCPayServer.Migrations
migrationBuilder.AddColumn<decimal>(
name: "Amount",
table: "Invoices",
type: migrationBuilder.IsNpgsql() ? "NUMERIC" : "TEXT",
type: "NUMERIC",
nullable: true);
migrationBuilder.AddColumn<string>(

View file

@ -18,7 +18,7 @@ namespace BTCPayServer.Migrations
migrationBuilder.AddColumn<decimal>(
name: "Amount",
table: "Payments",
type: migrationBuilder.IsNpgsql() ? "NUMERIC" : "TEXT",
type: "NUMERIC",
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
@ -37,14 +37,12 @@ namespace BTCPayServer.Migrations
table: "Payments",
type: "TEXT",
nullable: true);
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.AlterColumn<bool?>(
name: "Accounted",
table: "Payments",
nullable: true);
}
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)

View file

@ -14,15 +14,12 @@ namespace BTCPayServer.Migrations
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql(@"
DELETE FROM ""AddressInvoices"" WHERE ""Address"" LIKE '%_LightningLike';
ALTER TABLE ""AddressInvoices"" DROP COLUMN IF EXISTS ""CreatedTime"";
VACUUM (FULL, ANALYZE) ""AddressInvoices"";", true);
}
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)

View file

@ -196,12 +196,6 @@ namespace BTCPayServer.Hosting
settings.FileSystemStorageAsDefault = true;
await _Settings.UpdateSetting(settings);
}
if (!settings.FixSeqAfterSqliteMigration)
{
await FixSeqAfterSqliteMigration();
settings.FixSeqAfterSqliteMigration = true;
await _Settings.UpdateSetting(settings);
}
if (!settings.FixMappedDomainAppType)
{
await FixMappedDomainAppType();
@ -326,16 +320,6 @@ namespace BTCPayServer.Hosting
setting.Value = data.ToString();
await ctx.SaveChangesAsync();
}
private async Task FixSeqAfterSqliteMigration()
{
await using var ctx = _DBContextFactory.CreateContext();
var state = await GetMigrationState(ctx);
if (state != "complete")
return;
await UpdateSequenceInvoiceSearch(ctx);
}
static async Task<string> GetMigrationState(ApplicationDbContext postgresContext)
{
var o = (await postgresContext.Settings.FromSqlRaw("SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'").AsNoTracking().FirstOrDefaultAsync())?.Value;

View file

@ -28,7 +28,6 @@ namespace BTCPayServer.Services
public bool MigrateEmailServerDisableTLSCerts { get; set; }
public bool MigrateWalletColors { get; set; }
public bool FileSystemStorageAsDefault { get; set; }
public bool FixSeqAfterSqliteMigration { get; set; }
public bool FixMappedDomainAppType { get; set; }
public bool MigrateAppYmlToJson { get; set; }
public bool MigrateToStoreConfig { get; set; }

View file

@ -349,19 +349,9 @@ namespace BTCPayServer.Services
}
private async Task EnsureWalletObjectLinks(ApplicationDbContext ctx, DbConnection connection, IEnumerable<WalletObjectLinkData> links)
{
if (!ctx.Database.IsNpgsql())
{
foreach (var link in links)
{
await EnsureWalletObjectLink(link);
}
}
else
{
await connection.ExecuteAsync("INSERT INTO \"WalletObjectLinks\" VALUES (@WalletId, @AType, @AId, @BType, @BId, @Data::JSONB) ON CONFLICT DO NOTHING", links);
}
}
public static WalletObjectLinkData NewWalletObjectLinkData(WalletObjectId a, WalletObjectId b,
JObject? data = null)
@ -379,24 +369,6 @@ namespace BTCPayServer.Services
}
private static async Task UpdateWalletObjectLink(WalletObjectLinkData l, ApplicationDbContext ctx, bool doNothingIfExists)
{
if (!ctx.Database.IsNpgsql())
{
var e = ctx.WalletObjectLinks.Add(l);
try
{
await ctx.SaveChangesAsync();
}
catch (DbUpdateException) // already exists
{
if (!doNothingIfExists)
{
e.State = EntityState.Modified;
await ctx.SaveChangesAsync();
}
}
}
else
{
var connection = ctx.Database.GetDbConnection();
var conflict = doNothingIfExists ? "ON CONFLICT DO NOTHING" : "ON CONFLICT ON CONSTRAINT \"PK_WalletObjectLinks\" DO UPDATE SET \"Data\"=EXCLUDED.\"Data\"";
@ -409,7 +381,6 @@ namespace BTCPayServer.Services
throw new DbUpdateException();
}
}
}
class WalletObjectIdComparer : IComparer<WalletObjectId>
{
@ -590,25 +561,9 @@ namespace BTCPayServer.Services
ArgumentNullException.ThrowIfNull(id);
await using var ctx = _ContextFactory.CreateContext();
var wo = NewWalletObjectData(id, data);
if (!ctx.Database.IsNpgsql())
{
ctx.WalletObjects.Add(wo);
try
{
await ctx.SaveChangesAsync();
}
catch (DbUpdateException) // already exists
{
ctx.Entry(wo).State = EntityState.Modified;
await ctx.SaveChangesAsync();
}
}
else
{
var connection = ctx.Database.GetDbConnection();
await connection.ExecuteAsync("INSERT INTO \"WalletObjects\" VALUES (@WalletId, @Type, @Id, @Data::JSONB) ON CONFLICT ON CONSTRAINT \"PK_WalletObjects\" DO UPDATE SET \"Data\"=EXCLUDED.\"Data\"", wo);
}
}
public async Task EnsureWalletObject(WalletObjectId id, JObject? data = null)
{
@ -621,42 +576,17 @@ namespace BTCPayServer.Services
private async Task EnsureWalletObject(WalletObjectData wo, ApplicationDbContext ctx)
{
ArgumentNullException.ThrowIfNull(wo);
if (!ctx.Database.IsNpgsql())
{
var entry = ctx.WalletObjects.Add(wo);
try
{
await ctx.SaveChangesAsync();
}
catch (DbUpdateException) // already exists
{
entry.State = EntityState.Unchanged;
}
}
else
{
var connection = ctx.Database.GetDbConnection();
await connection.ExecuteAsync("INSERT INTO \"WalletObjects\" VALUES (@WalletId, @Type, @Id, @Data::JSONB) ON CONFLICT DO NOTHING", wo);
}
}
private async Task EnsureWalletObjects(ApplicationDbContext ctx,DbConnection connection, IEnumerable<WalletObjectData> data)
{
var walletObjectDatas = data as WalletObjectData[] ?? data.ToArray();
if(!walletObjectDatas.Any())
return;
if (!ctx.Database.IsNpgsql())
{
foreach(var d in walletObjectDatas)
{
await EnsureWalletObject(d, ctx);
}
}
else
{
var conn = ctx.Database.GetDbConnection();
await conn.ExecuteAsync("INSERT INTO \"WalletObjects\" VALUES (@WalletId, @Type, @Id, @Data::JSONB) ON CONFLICT DO NOTHING", walletObjectDatas);
}
}
public async Task EnsureCreated(List<WalletObjectData>? walletObjects,
List<WalletObjectLinkData>? walletObjectLinks)