Fix: Impossible to create invoice after migration from Sqlite (Close #4743)

This commit is contained in:
nicolas.dorier
2023-03-07 10:26:47 +09:00
parent 87ccae0d90
commit f787058c17
3 changed files with 24 additions and 1 deletions

View File

@@ -242,6 +242,12 @@ namespace BTCPayServer.Hosting
settings.FileSystemStorageAsDefault = true; settings.FileSystemStorageAsDefault = true;
await _Settings.UpdateSetting(settings); await _Settings.UpdateSetting(settings);
} }
if (!settings.FixSeqAfterSqliteMigration)
{
await FixSeqAfterSqliteMigration();
settings.FixSeqAfterSqliteMigration = true;
await _Settings.UpdateSetting(settings);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -250,6 +256,17 @@ namespace BTCPayServer.Hosting
} }
} }
private async Task FixSeqAfterSqliteMigration()
{
await using var ctx = _DBContextFactory.CreateContext();
if (!ctx.Database.IsNpgsql())
return;
var state = await ToPostgresMigrationStartupTask.GetMigrationState(ctx);
if (state != "complete")
return;
await ToPostgresMigrationStartupTask.UpdateSequenceInvoiceSearch(ctx);
}
#pragma warning disable CS0612 // Type or member is obsolete #pragma warning disable CS0612 // Type or member is obsolete
static WalletBlobInfo GetBlobInfo(WalletData walletData) static WalletBlobInfo GetBlobInfo(WalletData walletData)

View File

@@ -264,6 +264,7 @@ namespace BTCPayServer.Hosting
} }
await postgresContext.SaveChangesAsync(); await postgresContext.SaveChangesAsync();
postgresContext.ChangeTracker.Clear(); postgresContext.ChangeTracker.Clear();
await UpdateSequenceInvoiceSearch(postgresContext);
await SetMigrationState(postgresContext, migratingFrom, "complete"); await SetMigrationState(postgresContext, migratingFrom, "complete");
} }
otherContext.Dispose(); otherContext.Dispose();
@@ -273,8 +274,12 @@ namespace BTCPayServer.Hosting
Logger.LogInformation($"Migration to postgres from {migratingFrom} successful"); Logger.LogInformation($"Migration to postgres from {migratingFrom} successful");
} }
internal static async Task UpdateSequenceInvoiceSearch(ApplicationDbContext postgresContext)
{
await postgresContext.Database.ExecuteSqlRawAsync("SELECT SETVAL('\"InvoiceSearches_Id_seq\"', (SELECT max(\"Id\") FROM \"InvoiceSearches\"));");
}
private static async Task<string?> GetMigrationState(ApplicationDbContext postgresContext) internal static async Task<string?> GetMigrationState(ApplicationDbContext postgresContext)
{ {
var o = (await postgresContext.Settings.FromSqlRaw("SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'").AsNoTracking().FirstOrDefaultAsync())?.Value; var o = (await postgresContext.Settings.FromSqlRaw("SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'").AsNoTracking().FirstOrDefaultAsync())?.Value;
if (o is null) if (o is null)

View File

@@ -35,5 +35,6 @@ namespace BTCPayServer.Services
public bool MigrateEmailServerDisableTLSCerts { get; set; } public bool MigrateEmailServerDisableTLSCerts { get; set; }
public bool MigrateWalletColors { get; set; } public bool MigrateWalletColors { get; set; }
public bool FileSystemStorageAsDefault { get; set; } public bool FileSystemStorageAsDefault { get; set; }
public bool FixSeqAfterSqliteMigration { get; set; }
} }
} }