fix: wrap chart cascade delete in db.transaction() and add spanAnnotations deletion

- Import spanAnnotations from schema
- Wrap all delete operations in db.transaction() for atomicity
- Delete spanAnnotations first to satisfy FK constraints, then annotations, candles, chart
- Mark task 4.8 as done in tasks.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marko Djordjevic 2026-02-18 11:17:19 +01:00
parent 103bfa89cb
commit 1678da2d9d
2 changed files with 9 additions and 6 deletions

View file

@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { charts, candles, annotations } from '@/lib/db/schema';
import { charts, candles, annotations, spanAnnotations } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
export async function GET(
@ -39,10 +39,13 @@ export async function DELETE(
return NextResponse.json({ error: 'Chart not found' }, { status: 404 });
}
// Delete in order: annotations, candles, then chart (application-level cascade)
await db.delete(annotations).where(eq(annotations.chart_id, chartId));
await db.delete(candles).where(eq(candles.chart_id, chartId));
await db.delete(charts).where(eq(charts.id, chartId));
// Delete atomically in FK-safe order: spanAnnotations, annotations, candles, then chart
await db.transaction(async (tx) => {
await tx.delete(spanAnnotations).where(eq(spanAnnotations.chart_id, chartId));
await tx.delete(annotations).where(eq(annotations.chart_id, chartId));
await tx.delete(candles).where(eq(candles.chart_id, chartId));
await tx.delete(charts).where(eq(charts.id, chartId));
});
return NextResponse.json({ success: true });
}