diff --git a/BTCPayServer/Hosting/MigrationStartupTask.cs b/BTCPayServer/Hosting/MigrationStartupTask.cs index c4fd616fc6..02e0d35d24 100644 --- a/BTCPayServer/Hosting/MigrationStartupTask.cs +++ b/BTCPayServer/Hosting/MigrationStartupTask.cs @@ -242,6 +242,12 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default) settings.FileSystemStorageAsDefault = true; await _Settings.UpdateSetting(settings); } + if (!settings.FixSeqAfterSqliteMigration) + { + await FixSeqAfterSqliteMigration(); + settings.FixSeqAfterSqliteMigration = true; + await _Settings.UpdateSetting(settings); + } } catch (Exception ex) { @@ -250,6 +256,17 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default) } } + 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 static WalletBlobInfo GetBlobInfo(WalletData walletData) diff --git a/BTCPayServer/Hosting/ToPostgresMigrationStartupTask.cs b/BTCPayServer/Hosting/ToPostgresMigrationStartupTask.cs index f06b5f6ea5..2df3d15812 100644 --- a/BTCPayServer/Hosting/ToPostgresMigrationStartupTask.cs +++ b/BTCPayServer/Hosting/ToPostgresMigrationStartupTask.cs @@ -264,6 +264,7 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default) } await postgresContext.SaveChangesAsync(); postgresContext.ChangeTracker.Clear(); + await UpdateSequenceInvoiceSearch(postgresContext); await SetMigrationState(postgresContext, migratingFrom, "complete"); } otherContext.Dispose(); @@ -273,8 +274,12 @@ public async Task ExecuteAsync(CancellationToken cancellationToken = default) 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 GetMigrationState(ApplicationDbContext postgresContext) + internal static async Task GetMigrationState(ApplicationDbContext postgresContext) { var o = (await postgresContext.Settings.FromSqlRaw("SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'").AsNoTracking().FirstOrDefaultAsync())?.Value; if (o is null) diff --git a/BTCPayServer/Services/MigrationSettings.cs b/BTCPayServer/Services/MigrationSettings.cs index b0877d09a5..8249f32a36 100644 --- a/BTCPayServer/Services/MigrationSettings.cs +++ b/BTCPayServer/Services/MigrationSettings.cs @@ -35,5 +35,6 @@ public override string ToString() public bool MigrateEmailServerDisableTLSCerts { get; set; } public bool MigrateWalletColors { get; set; } public bool FileSystemStorageAsDefault { get; set; } + public bool FixSeqAfterSqliteMigration { get; set; } } }