fix(db): add SetNull cascade on Assignment→DemandRequirement + composite indexes

Prevents orphaned Assignment rows when a DemandRequirement is deleted.
Adds (resourceId, status, endDate) and (projectId, status, endDate)
indexes to support capacity range queries.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 14:03:11 +02:00
parent d3bfa8ca98
commit aebe5bc57d
3 changed files with 14 additions and 1 deletions
@@ -0,0 +1,4 @@
-- Migration: Add composite indexes on Assignment for capacity range queries
CREATE INDEX IF NOT EXISTS "assignments_resourceId_status_endDate_idx" ON "assignments"("resourceId", "status", "endDate");
CREATE INDEX IF NOT EXISTS "assignments_projectId_status_endDate_idx" ON "assignments"("projectId", "status", "endDate");
@@ -0,0 +1,7 @@
-- Migration: Add SetNull cascade on Assignment → DemandRequirement FK
-- When a DemandRequirement is deleted, assignments lose their reference but are not deleted.
ALTER TABLE "assignments" DROP CONSTRAINT IF EXISTS "assignments_demandRequirementId_fkey";
ALTER TABLE "assignments" ADD CONSTRAINT "assignments_demandRequirementId_fkey"
FOREIGN KEY ("demandRequirementId") REFERENCES "demand_requirements"("id")
ON DELETE SET NULL ON UPDATE CASCADE;