Files
ersatztv/ErsatzTV.Application/Scheduling/Commands/ErasePlayoutHistoryHandler.cs
T
timothyandClaude Opus 4.8 e383c253cc
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 5m54s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 6m45s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(api): #269 review — force-write ErasePlayoutHistory + document boundaries
Independent adversarial review (cold fork + Codex) of the first cut converged
on one real miss and two boundaries to document.

- **ErasePlayoutHistoryHandler** (HIGH, both reviewers): modifies Playout ROOT
  scalars (Seed/Anchor/OnDemandCheckpoint) *without* bumping Version, inside an
  explicit transaction with no try/catch, so it 500s on a concurrent bump —
  reachable via POST /api/playouts/{id}/erase-items-and-history. My first sweep
  filtered on "Version-bumpers + deletes"; the true exposure surface is "any
  handler leaving a versioned root Modified/Deleted", so this slipped through.
  Now routes through SaveChangesForcingVersion (+ a non-vacuous through-handler
  test that exercises the explicit-transaction path). Re-swept with the correct
  filter: ErasePlayoutItems (AsNoTracking + ExecuteDelete children only) and
  ResetAllPlayouts (read-only + enqueue) are NOT exposed.

- **Background build/time-shift Playout-scalar writers** (BuildPlayout via
  PlayoutBuilder, PlayoutTimeShifter): token-guarded too, but intentionally left
  on plain save — they already catch (build-failure, not 500), and force-writing
  would persist output built from stale config (the concurrent config bump already
  enqueues a rebuild). Documented as a deliberate boundary, not a gap.

- **Item-add index collision** under force-write: documented as an accepted
  Phase-1 effect (non-corrupting, self-correcting; reload-recompute refinement
  is a #197 candidate).

Also corrects the docs' "every Version bumper" framing to the true filter and the
test docstring's over-broad non-vacuity claim. Full ErsatzTV.Tests green (1483).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-12 16:59:01 +02:00

68 lines
2.8 KiB
C#

using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Scheduling;
public class ErasePlayoutHistoryHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<ErasePlayoutHistory>
{
public async Task Handle(ErasePlayoutHistory request, CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<Playout> maybePlayout = await dbContext.Playouts
.Filter(p => p.ScheduleKind == PlayoutScheduleKind.Block ||
p.ScheduleKind == PlayoutScheduleKind.Sequential ||
p.ScheduleKind == PlayoutScheduleKind.Scripted ||
p.ScheduleKind == PlayoutScheduleKind.Classic)
.SelectOneAsync(p => p.Id, p => p.Id == request.PlayoutId, cancellationToken);
foreach (Playout playout in maybePlayout)
{
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
int nextSeed = new Random().Next();
playout.Seed = nextSeed;
// this deletes the owned PlayoutAnchor
playout.Anchor = null;
playout.OnDemandCheckpoint = null;
// These are Playout ROOT-scalar writes, so once #253 made Playout.Version an
// IsConcurrencyToken the UPDATE is guarded by `WHERE Version=@orig`. Force-write past a
// concurrent config bump (alt-schedule/template edit) instead of throwing an unhandled
// DbUpdateConcurrencyException → 500 — an unconditional erase should win (#269).
await dbContext.SaveChangesForcingVersion(cancellationToken);
await dbContext.PlayoutItems
.Where(pi => pi.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await dbContext.PlayoutHistory
.Where(ph => ph.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await dbContext.PlayoutProgramScheduleItemAnchors
.Where(a => a.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await dbContext.RerunHistory
.Where(rh => rh.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await dbContext.PlayoutGaps
.Where(pg => pg.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await dbContext.PlayoutBuildStatus
.Where(pb => pb.PlayoutId == playout.Id)
.ExecuteDeleteAsync(cancellationToken);
await transaction.CommitAsync(cancellationToken);
}
}
}