diff --git a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs index 74e03cb9b3..f1cde771bc 100644 --- a/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs +++ b/backend/FwLite/LcmCrdt.Tests/SnapshotAtCommitServiceTests.cs @@ -5,6 +5,7 @@ using SIL.Harmony.Db; using Microsoft.Data.Sqlite; using SIL.Harmony.Core; +using Bogus.DataSets; namespace LcmCrdt.Tests; @@ -329,8 +330,10 @@ await _api.CreateWritingSystem(new WritingSystem() public async Task DisposeAsync() { await _dbContext.Database.CloseConnectionAsync(); - SqliteConnection.ClearAllPools(); + using var clearConn = new SqliteConnection($"Data Source={_dbPath}"); + SqliteConnection.ClearPool(clearConn); await _dbContext.Database.EnsureDeletedAsync(); + await _dbContext.DisposeAsync(); await _services.DisposeAsync(); if (File.Exists(_dbPath)) { diff --git a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs index 6f7f3bc5f4..c6ce53549f 100644 --- a/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs +++ b/backend/FwLite/LcmCrdt/SnapshotAtCommitService.cs @@ -24,7 +24,7 @@ public class SnapshotAtCommitService( { using var activity = _activitySource.StartActivity(); activity?.SetTag("app.commit_id", commitId); - var dbContext = await crdtDbContextFactory.CreateDbContextAsync(); + await using var dbContext = await crdtDbContextFactory.CreateDbContextAsync(); var commit = await dbContext.Commits.SingleOrDefaultAsync(c => c.Id == commitId); if (commit is null) { @@ -42,7 +42,7 @@ public class SnapshotAtCommitService( await ForkDatabase(dbPath, forkDbPath); var project = new CrdtProject(currentProjectService.Project.Name, forkDbPath); - var serviceScope = serviceProvider.CreateAsyncScope(); + await using var serviceScope = serviceProvider.CreateAsyncScope(); var scopedCurrentProjectService = serviceScope.ServiceProvider.GetRequiredService(); var projectData = await scopedCurrentProjectService.SetupProjectContext(project); @@ -51,7 +51,7 @@ public class SnapshotAtCommitService( var optionsBuilder = new DbContextOptionsBuilder() .UseSqlite($"Data Source={forkDbPath}"); LcmCrdtKernel.ConfigureDbOptions(serviceScope.ServiceProvider, optionsBuilder); - ICrdtDbContext forkDbContext = new LcmCrdtDbContext(optionsBuilder.Options, crdtConfig); + await using var forkDbContext = new LcmCrdtDbContext(optionsBuilder.Options, crdtConfig); var deleted = await DeleteCommitsAfter(forkDbContext, commit, preserveAllFieldWorksCommits); logger.LogInformation("Deleted {Deleted} commits after {CommitId}", deleted, commitId); @@ -68,7 +68,8 @@ public class SnapshotAtCommitService( { try { - SqliteConnection.ClearAllPools(); + using var clearConn = new SqliteConnection($"Data Source={forkDbPath}"); + SqliteConnection.ClearPool(clearConn); File.Delete(forkDbPath); } catch (Exception ex)