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:
@@ -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;
|
||||||
@@ -1330,7 +1330,7 @@ model Assignment {
|
|||||||
status AllocationStatus @default(PROPOSED)
|
status AllocationStatus @default(PROPOSED)
|
||||||
metadata Json @db.JsonB @default("{}")
|
metadata Json @db.JsonB @default("{}")
|
||||||
|
|
||||||
demandRequirement DemandRequirement? @relation(fields: [demandRequirementId], references: [id])
|
demandRequirement DemandRequirement? @relation(fields: [demandRequirementId], references: [id], onDelete: SetNull)
|
||||||
resource Resource @relation(fields: [resourceId], references: [id])
|
resource Resource @relation(fields: [resourceId], references: [id])
|
||||||
project Project @relation(fields: [projectId], references: [id])
|
project Project @relation(fields: [projectId], references: [id])
|
||||||
roleEntity Role? @relation(fields: [roleId], references: [id])
|
roleEntity Role? @relation(fields: [roleId], references: [id])
|
||||||
@@ -1346,6 +1346,8 @@ model Assignment {
|
|||||||
@@index([status])
|
@@index([status])
|
||||||
@@index([resourceId, status, startDate])
|
@@index([resourceId, status, startDate])
|
||||||
@@index([projectId, startDate, endDate])
|
@@index([projectId, startDate, endDate])
|
||||||
|
@@index([resourceId, status, endDate])
|
||||||
|
@@index([projectId, status, endDate])
|
||||||
@@map("assignments")
|
@@map("assignments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user