-- Existing Issue comments remain unchanged. The Issue relation becomes
-- optional so the same comment table can also target one RGC Decision update.
ALTER TABLE "progress_report_issue_comments"
ALTER COLUMN "progress_report_update_issue_id" DROP NOT NULL,
ADD COLUMN "progress_report_update_plenary_decision_id" INTEGER;

-- CreateIndex
CREATE INDEX "progress_report_issue_comments_update_decision_id_idx" ON "progress_report_issue_comments"("progress_report_update_plenary_decision_id");

-- Each comment must target exactly one resource: an Issue update or an RGC
-- Decision update.
ALTER TABLE "progress_report_issue_comments"
ADD CONSTRAINT "progress_report_issue_comments_one_target_check"
CHECK (
    ("progress_report_update_issue_id" IS NOT NULL AND "progress_report_update_plenary_decision_id" IS NULL)
    OR
    ("progress_report_update_issue_id" IS NULL AND "progress_report_update_plenary_decision_id" IS NOT NULL)
);

-- AddForeignKey
ALTER TABLE "progress_report_issue_comments" ADD CONSTRAINT "progress_report_issue_comments_update_decision_id_fkey" FOREIGN KEY ("progress_report_update_plenary_decision_id") REFERENCES "progress_report_update_plenary_decisions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
