Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions backend/FwLite/LcmCrdt/CurrentProjectService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -82,18 +83,27 @@ public async ValueTask<ProjectData> RefreshProjectData()
return projectData;
}

private async Task MigrateDb()
private static readonly ConcurrentDictionary<string, Lazy<Task>> MigrationTasks = [];
private Task MigrateDb()
{
try
//ensure we only execute once, otherwise we'll have a conflict as Migrate is not thread safe.
//design based on https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/
#pragma warning disable VSTHRD011
return MigrationTasks.GetOrAdd(Project.DbPath, _ => new Lazy<Task>(Execute)).Value;
Comment thread
hahn-kev marked this conversation as resolved.
#pragma warning restore VSTHRD011
async Task Execute()
{

await DbContext.Database.MigrateAsync();
}
catch (Exception e)
{
logger.LogError(e, "Failed to migrate database for project '{Project}'", Project.Name);
throw;
try
{
await DbContext.Database.MigrateAsync();
}
catch (Exception e)
{
logger.LogError(e, "Failed to migrate database for project '{Project}'", Project.Name);
throw;
}
}

}

public async Task SetProjectSyncOrigin(Uri? domain, Guid? id)
Expand Down