Files
ClaudeDo/tests/ClaudeDo.Data.Tests/TaskDependencyMigrationTests.cs
T
Mika Kuns c25732cff8 feat(claude-do): depends_on als Menge statt Einzelfeld
Ein Task kann jetzt auf beliebig viele Vorgaenger warten. Die Einzelspalte
tasks.depends_on_task_id wird durch die Beziehungstabelle task_dependencies
ersetzt (Composite-PK, beide FKs ON DELETE CASCADE -- das reproduziert das
alte ON DELETE SET NULL: geloeschter Vorgaenger = Kante weg = Task wieder
pickbar). BlockedByTaskId bleibt unberuehrt.

- Migration AddTaskDependencies kopiert jede bestehende depends_on_task_id
  verlustfrei in die Tabelle, bevor die Spalte fallengelassen wird
- QueuePicker gatet per doppelt negiertem NOT EXISTS: kein Task mit 0 Kanten
  wird blockiert, alle Kanten muessen done sein, eine Kante auf einen
  verschwundenen Task blockiert weiter
- SetDependsOnAsync ersetzt die ganze Menge; Zykluspruefung ist jetzt eine
  BFS ueber den Graphen, faengt also auch A->B->C->A
- blockedReason nennt alle offenen Vorgaenger (External/TaskBlocking, von
  ExternalMcpService und TaskWaitMcpTools gemeinsam genutzt)
- add_task/update_task nehmen dependsOnTaskIds; die alte Skalarform bleibt
  als Ein-Element-Menge inkl. ""-Clear-Sentinel gueltig
- UI: Fan-in-Zeile bleibt flach und listet alle Vorgaenger im After-Label,
  die lineare Kette rendert unveraendert
2026-09-02 15:08:05 +02:00

101 lines
4.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ClaudeDo.Data.Tests;
/// <summary>
/// AddTaskDependencies moves every <c>tasks.depends_on_task_id</c> value into the new
/// <c>task_dependencies</c> table and then drops the column. Runs a real <c>Migrate()</c> — the
/// EnsureCreated fixtures every other Data.Tests file uses skip migrations entirely and would
/// never notice a data copy that silently dropped rows (see
/// <c>.claudedo/traps/ef-migration-parallel-designer-collision.md</c>).
/// </summary>
public sealed class TaskDependencyMigrationTests : IDisposable
{
private const string PreviousMigrationId = "20260902102003_AddExpectsNoDiff";
private readonly string _dbPath;
private readonly DbContextOptions<ClaudeDoDbContext> _options;
public TaskDependencyMigrationTests()
{
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_deps_{Guid.NewGuid():N}.db");
_options = new DbContextOptionsBuilder<ClaudeDoDbContext>()
.UseSqlite($"Data Source={_dbPath}")
.Options;
}
public void Dispose()
{
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
foreach (var suffix in new[] { "", "-wal", "-shm" })
try { File.Delete(_dbPath + suffix); } catch { }
}
[Fact]
public void Existing_depends_on_links_survive_as_one_element_sets()
{
SeedLegacyDatabase();
using (var ctx = new ClaudeDoDbContext(_options))
ctx.Database.Migrate();
using var check = new ClaudeDoDbContext(_options);
Assert.Empty(check.Database.GetPendingMigrations());
var edges = check.TaskDependencies.AsNoTracking()
.OrderBy(d => d.TaskId)
.Select(d => new { d.TaskId, d.DependsOnTaskId })
.ToList();
Assert.Equal(2, edges.Count);
Assert.Equal(("t2", "t1"), (edges[0].TaskId, edges[0].DependsOnTaskId));
Assert.Equal(("t3", "t2"), (edges[1].TaskId, edges[1].DependsOnTaskId));
// The tasks themselves are untouched, and the independent one gained no edge.
Assert.Equal(4, check.Tasks.Count());
Assert.DoesNotContain("t4", edges.Select(e => e.TaskId));
}
[Fact]
public void Column_is_gone_after_the_migration()
{
SeedLegacyDatabase();
using var ctx = new ClaudeDoDbContext(_options);
ctx.Database.Migrate();
var conn = ctx.Database.GetDbConnection();
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM pragma_table_info('tasks') WHERE name = 'depends_on_task_id'";
Assert.Equal(0L, Convert.ToInt64(cmd.ExecuteScalar()));
conn.Close();
}
// Migrates *to* the migration before this one, then seeds via raw SQL: EnsureCreated would
// build today's schema (no depends_on_task_id column at all), so there'd be nothing to copy.
private void SeedLegacyDatabase()
{
using var seed = new ClaudeDoDbContext(_options);
seed.Database.GetService<IMigrator>().Migrate(PreviousMigrationId);
var conn = seed.Database.GetDbConnection();
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = """
INSERT INTO lists (id, name, created_at) VALUES ('l1', 'List', '2026-01-01 00:00:00');
INSERT INTO tasks (id, number, list_id, title, status, created_at)
VALUES ('t1', 1, 'l1', 'first', 'done', '2026-01-01 00:00:00');
INSERT INTO tasks (id, number, list_id, title, status, created_at, depends_on_task_id)
VALUES ('t2', 2, 'l1', 'second', 'queued', '2026-01-01 00:00:00', 't1');
INSERT INTO tasks (id, number, list_id, title, status, created_at, depends_on_task_id)
VALUES ('t3', 3, 'l1', 'third', 'queued', '2026-01-01 00:00:00', 't2');
INSERT INTO tasks (id, number, list_id, title, status, created_at)
VALUES ('t4', 4, 'l1', 'independent', 'idle', '2026-01-01 00:00:00');
""";
cmd.ExecuteNonQuery();
conn.Close();
}
}