feat(data): add task numbers schema, allocator, and backfill migration
TaskEntity.Number is a global, monotonically increasing, never-reused integer (displayed as #123), allocated from AppSettingsEntity.NextTaskNumber via a single UPDATE...RETURNING statement rather than MAX(number)+1, which would reissue a deleted task's number. Both insert paths (TaskRepository. AddAsync and CreateChildAsync) route through the new TaskNumberAllocator, with a bounded retry on a unique-index collision. One migration adds the columns, backfills existing rows in creation order, and creates the unique index afterwards. Data-layer only; MCP/UI wiring is later slices.
This commit is contained in:
@@ -26,8 +26,7 @@ public sealed class TaskRepository
|
||||
.MaxAsync(ct);
|
||||
entity.SortOrder = (maxSort ?? -1) + 1;
|
||||
|
||||
_context.Tasks.Add(entity);
|
||||
await _context.SaveChangesAsync(ct);
|
||||
await TaskNumberAllocator.AddWithNumberAsync(_context, entity, ct);
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(TaskEntity entity, CancellationToken ct = default)
|
||||
@@ -50,6 +49,11 @@ public sealed class TaskRepository
|
||||
return await _context.Tasks.AsNoTracking().FirstOrDefaultAsync(t => t.Id == taskId, ct);
|
||||
}
|
||||
|
||||
public async Task<TaskEntity?> GetByNumberAsync(int number, CancellationToken ct = default)
|
||||
{
|
||||
return await _context.Tasks.AsNoTracking().FirstOrDefaultAsync(t => t.Number == number, ct);
|
||||
}
|
||||
|
||||
public async Task<List<TaskEntity>> GetByListIdAsync(string listId, CancellationToken ct = default)
|
||||
{
|
||||
return await _context.Tasks
|
||||
@@ -309,8 +313,7 @@ public sealed class TaskRepository
|
||||
Model = ModelRegistry.NormalizeAlias(model),
|
||||
MaxTurns = maxTurns,
|
||||
};
|
||||
_context.Tasks.Add(child);
|
||||
await _context.SaveChangesAsync(ct);
|
||||
await TaskNumberAllocator.AddWithNumberAsync(_context, child, ct);
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user