Skip to content

Commit

Permalink
Fix: Impossible to create invoice after migration from Sqlite (Close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Mar 7, 2023
1 parent 87ccae0 commit f787058
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions BTCPayServer/Hosting/MigrationStartupTask.cs
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion BTCPayServer/Hosting/ToPostgresMigrationStartupTask.cs
Expand Up @@ -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();
Expand All @@ -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<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;
if (o is null)
Expand Down
1 change: 1 addition & 0 deletions BTCPayServer/Services/MigrationSettings.cs
Expand Up @@ -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; }
}
}

0 comments on commit f787058

Please sign in to comment.