-- The tooth_selection_groups migration tried to drop
-- "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_stepOrder_key",
-- but PostgreSQL truncated/renamed that index to
-- "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCo_key"
-- (see 20260710150528). The old unique index (without selectionGroupId)
-- therefore remained and blocks multiple selection groups that share
-- the same prosthesis type + stepOrder.

DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCo_key";
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_stepOrder_key";
DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_prosthesisTypeCode_s";

-- Ensure the correct unique index exists (idempotent for DBs that already have it).
CREATE UNIQUE INDEX IF NOT EXISTS "lab_case_tasks_labCaseId_treatmentDetailId_selectionGroupId_prosthesisTypeCode_stepOrder_key"
  ON "lab_case_tasks"("labCaseId", "treatmentDetailId", "selectionGroupId", "prosthesisTypeCode", "stepOrder");
