feat(data): seed default Lists (My Day, Important, Planned)
This commit is contained in:
@@ -2,6 +2,7 @@ using Avalonia;
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Git;
|
||||
using ClaudeDo.Data.Repositories;
|
||||
using ClaudeDo.Data.Seeding;
|
||||
using ClaudeDo.Ui;
|
||||
using ClaudeDo.Ui.Services;
|
||||
using ClaudeDo.Ui.ViewModels;
|
||||
@@ -28,8 +29,9 @@ sealed class Program
|
||||
|
||||
using (var scope = services.CreateScope())
|
||||
{
|
||||
ClaudeDoDbContext.MigrateAndConfigure(
|
||||
scope.ServiceProvider.GetRequiredService<ClaudeDoDbContext>());
|
||||
var db = scope.ServiceProvider.GetRequiredService<ClaudeDoDbContext>();
|
||||
ClaudeDoDbContext.MigrateAndConfigure(db);
|
||||
DefaultListsSeeder.SeedAsync(db).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
try
|
||||
|
||||
25
src/ClaudeDo.Data/Seeding/DefaultListsSeeder.cs
Normal file
25
src/ClaudeDo.Data/Seeding/DefaultListsSeeder.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using ClaudeDo.Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Data.Seeding;
|
||||
|
||||
public static class DefaultListsSeeder
|
||||
{
|
||||
private static readonly string[] Defaults = { "My Day", "Important", "Planned" };
|
||||
|
||||
public static async Task SeedAsync(ClaudeDoDbContext ctx, CancellationToken ct = default)
|
||||
{
|
||||
var existing = await ctx.Lists.Select(l => l.Name).ToListAsync(ct);
|
||||
var now = DateTime.UtcNow;
|
||||
foreach (var name in Defaults.Where(n => !existing.Contains(n)))
|
||||
{
|
||||
ctx.Lists.Add(new ListEntity
|
||||
{
|
||||
Id = Guid.NewGuid().ToString("N"),
|
||||
Name = name,
|
||||
CreatedAt = now,
|
||||
});
|
||||
}
|
||||
await ctx.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Seeding;
|
||||
using ClaudeDo.Installer.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -20,6 +21,7 @@ public sealed class InitDatabaseStep : IInstallStep
|
||||
.Options;
|
||||
using var context = new ClaudeDoDbContext(options);
|
||||
ClaudeDoDbContext.MigrateAndConfigure(context);
|
||||
DefaultListsSeeder.SeedAsync(context).GetAwaiter().GetResult();
|
||||
|
||||
progress.Report("Schema applied successfully");
|
||||
return Task.FromResult(StepResult.Ok());
|
||||
|
||||
41
tests/ClaudeDo.Worker.Tests/UiSchema/DefaultListSeedTests.cs
Normal file
41
tests/ClaudeDo.Worker.Tests/UiSchema/DefaultListSeedTests.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Seeding;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Xunit;
|
||||
|
||||
namespace ClaudeDo.Worker.Tests.UiSchema;
|
||||
|
||||
public class DefaultListSeedTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"claudedo-seed-{Guid.NewGuid():N}.db");
|
||||
|
||||
private ClaudeDoDbContext NewContext()
|
||||
{
|
||||
var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
||||
.UseSqlite($"Data Source={_dbPath}").Options;
|
||||
var ctx = new ClaudeDoDbContext(opts);
|
||||
ctx.Database.EnsureCreated();
|
||||
return ctx;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Seeds_MyDay_Important_Planned_Lists_Idempotently()
|
||||
{
|
||||
await using (var ctx = NewContext())
|
||||
{
|
||||
await DefaultListsSeeder.SeedAsync(ctx);
|
||||
await DefaultListsSeeder.SeedAsync(ctx); // idempotent
|
||||
}
|
||||
|
||||
await using var verify = NewContext();
|
||||
var names = verify.Lists.Select(l => l.Name).OrderBy(n => n).ToList();
|
||||
Assert.Equal(new[] { "Important", "My Day", "Planned" }, names);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try { if (File.Exists(_dbPath)) File.Delete(_dbPath); } catch { }
|
||||
try { if (File.Exists(_dbPath + "-wal")) File.Delete(_dbPath + "-wal"); } catch { }
|
||||
try { if (File.Exists(_dbPath + "-shm")) File.Delete(_dbPath + "-shm"); } catch { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user