feat(worker,ui): wire EF Core into DI and update all consumers to IDbContextFactory
Worker and App Program.cs: replace SqliteConnectionFactory+SchemaInitializer with AddDbContextFactory<ClaudeDoDbContext> + Database.Migrate(). Repos changed from AddSingleton to AddScoped. All singleton services (QueueService, StaleTaskRecovery, WorktreeManager, TaskRunner) and singleton ViewModels (MainWindowViewModel, TaskDetailViewModel, TaskListViewModel, TaskEditorViewModel) now take IDbContextFactory<ClaudeDoDbContext> and create short-lived contexts per operation. Test infrastructure: DbFixture now uses EF migrations instead of SchemaInitializer; all test classes create contexts via DbFixture.CreateContext(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
using ClaudeDo.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Worker.Tests.Infrastructure;
|
||||
|
||||
public sealed class DbFixture : IDisposable
|
||||
{
|
||||
public string DbPath { get; }
|
||||
public SqliteConnectionFactory Factory { get; }
|
||||
|
||||
public DbFixture()
|
||||
{
|
||||
DbPath = Path.Combine(Path.GetTempPath(), $"claudedo_test_{Guid.NewGuid():N}.db");
|
||||
Factory = new SqliteConnectionFactory(DbPath);
|
||||
SchemaInitializer.Apply(Factory);
|
||||
// Apply migrations so the schema is created.
|
||||
using var ctx = CreateContext();
|
||||
ctx.Database.Migrate();
|
||||
}
|
||||
|
||||
public ClaudeDoDbContext CreateContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
||||
.UseSqlite($"Data Source={DbPath}")
|
||||
.Options;
|
||||
return new ClaudeDoDbContext(options);
|
||||
}
|
||||
|
||||
public TestDbContextFactory CreateFactory() => new(this);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try { File.Delete(DbPath); } catch { /* best effort */ }
|
||||
@@ -21,3 +32,10 @@ public sealed class DbFixture : IDisposable
|
||||
try { File.Delete(DbPath + "-shm"); } catch { }
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TestDbContextFactory : IDbContextFactory<ClaudeDoDbContext>
|
||||
{
|
||||
private readonly DbFixture _fixture;
|
||||
public TestDbContextFactory(DbFixture fixture) => _fixture = fixture;
|
||||
public ClaudeDoDbContext CreateDbContext() => _fixture.CreateContext();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user