Reconcile schema with migrations and fix failed migration

- Align schema.prisma with add_15_features migration (15 discrepancies):
  nullability, column names, PKs, missing/extra columns, onDelete behavior
- Make universal_apply_programid migration idempotent for safe re-execution
- Add reconciliation migration for missing FKs and indexes
- Fix message.ts and mentor.ts to match corrected schema field names

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-08 14:37:32 +01:00
parent 04d0deced1
commit e0e4cb2a32
18 changed files with 1174 additions and 353 deletions

View File

@@ -1,51 +1,91 @@
-- Universal Apply Page: Make Project.roundId nullable and add programId FK
-- This migration enables projects to be submitted to a program/edition without being assigned to a specific round
-- NOTE: Written to be idempotent (safe to re-run if partially applied)
-- Step 1: Add Program.slug for edition-wide apply URLs (nullable for existing programs)
ALTER TABLE "Program" ADD COLUMN "slug" TEXT;
CREATE UNIQUE INDEX "Program_slug_key" ON "Program"("slug");
ALTER TABLE "Program" ADD COLUMN IF NOT EXISTS "slug" TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS "Program_slug_key" ON "Program"("slug");
-- Step 2: Add programId column (nullable initially to handle existing data)
ALTER TABLE "Project" ADD COLUMN "programId" TEXT;
ALTER TABLE "Project" ADD COLUMN IF NOT EXISTS "programId" TEXT;
-- Step 3: Backfill programId from existing round relationships
-- Every project currently has a roundId, so we can populate programId from Round.programId
-- Only update rows where programId is still NULL (idempotent)
UPDATE "Project" p
SET "programId" = r."programId"
FROM "Round" r
WHERE p."roundId" = r.id;
WHERE p."roundId" = r.id
AND p."programId" IS NULL;
-- Step 4: Verify backfill succeeded (should be 0 rows with NULL programId)
-- If this fails, manual intervention is needed
-- Step 4: Handle orphaned projects (no roundId = no way to derive programId)
-- Assign them to the first available program, or delete them if no program exists
DO $$
DECLARE
null_count INTEGER;
fallback_program_id TEXT;
BEGIN
SELECT COUNT(*) INTO null_count FROM "Project" WHERE "programId" IS NULL;
IF null_count > 0 THEN
RAISE EXCEPTION 'Migration failed: % projects have NULL programId after backfill', null_count;
SELECT id INTO fallback_program_id FROM "Program" ORDER BY "createdAt" ASC LIMIT 1;
IF fallback_program_id IS NOT NULL THEN
UPDATE "Project" SET "programId" = fallback_program_id WHERE "programId" IS NULL;
RAISE NOTICE 'Assigned % orphaned projects to fallback program %', null_count, fallback_program_id;
ELSE
DELETE FROM "Project" WHERE "programId" IS NULL;
RAISE NOTICE 'Deleted % orphaned projects (no program exists to assign them to)', null_count;
END IF;
END IF;
END $$;
-- Step 5: Make programId required (NOT NULL constraint)
ALTER TABLE "Project" ALTER COLUMN "programId" SET NOT NULL;
-- Step 5: Make programId required (NOT NULL constraint) - safe if already NOT NULL
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'Project' AND column_name = 'programId' AND is_nullable = 'YES'
) THEN
ALTER TABLE "Project" ALTER COLUMN "programId" SET NOT NULL;
END IF;
END $$;
-- Step 6: Add foreign key constraint for programId
ALTER TABLE "Project" ADD CONSTRAINT "Project_programId_fkey"
FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE;
-- Step 6: Add foreign key constraint for programId (skip if already exists)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'Project_programId_fkey' AND table_name = 'Project'
) THEN
ALTER TABLE "Project" ADD CONSTRAINT "Project_programId_fkey"
FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE;
END IF;
END $$;
-- Step 7: Make roundId nullable (allow projects without round assignment)
ALTER TABLE "Project" ALTER COLUMN "roundId" DROP NOT NULL;
-- Safe: DROP NOT NULL is idempotent if already nullable
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'Project' AND column_name = 'roundId' AND is_nullable = 'NO'
) THEN
ALTER TABLE "Project" ALTER COLUMN "roundId" DROP NOT NULL;
END IF;
END $$;
-- Step 8: Update round FK to SET NULL on delete (instead of CASCADE)
-- Projects should remain in the database if their round is deleted
ALTER TABLE "Project" DROP CONSTRAINT "Project_roundId_fkey";
ALTER TABLE "Project" ADD CONSTRAINT "Project_roundId_fkey"
FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL;
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'Project_roundId_fkey' AND table_name = 'Project'
) THEN
ALTER TABLE "Project" DROP CONSTRAINT "Project_roundId_fkey";
END IF;
ALTER TABLE "Project" ADD CONSTRAINT "Project_roundId_fkey"
FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL;
END $$;
-- Step 9: Add performance indexes
-- Index for filtering unassigned projects (WHERE roundId IS NULL)
CREATE INDEX "Project_programId_idx" ON "Project"("programId");
CREATE INDEX "Project_programId_roundId_idx" ON "Project"("programId", "roundId");
-- Note: The existing "Project_roundId_idx" remains for queries filtering by round
CREATE INDEX IF NOT EXISTS "Project_programId_idx" ON "Project"("programId");
CREATE INDEX IF NOT EXISTS "Project_programId_roundId_idx" ON "Project"("programId", "roundId");

View File

@@ -0,0 +1,41 @@
-- Reconciliation migration: Add missing foreign keys and indexes
-- The add_15_features migration omitted some FKs and indexes that the schema expects
-- This migration brings the database in line with the Prisma schema
-- =====================================================
-- Missing Foreign Keys
-- =====================================================
-- RoundTemplate → Program
ALTER TABLE "RoundTemplate" ADD CONSTRAINT "RoundTemplate_programId_fkey"
FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- RoundTemplate → User (creator)
ALTER TABLE "RoundTemplate" ADD CONSTRAINT "RoundTemplate_createdBy_fkey"
FOREIGN KEY ("createdBy") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- Message → Round
ALTER TABLE "Message" ADD CONSTRAINT "Message_roundId_fkey"
FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- EvaluationDiscussion → Round
ALTER TABLE "EvaluationDiscussion" ADD CONSTRAINT "EvaluationDiscussion_roundId_fkey"
FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- ProjectFile → ProjectFile (self-relation for file versioning)
ALTER TABLE "ProjectFile" ADD CONSTRAINT "ProjectFile_replacedById_fkey"
FOREIGN KEY ("replacedById") REFERENCES "ProjectFile"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- =====================================================
-- Missing Indexes
-- =====================================================
CREATE INDEX "RoundTemplate_roundType_idx" ON "RoundTemplate"("roundType");
CREATE INDEX "MentorNote_authorId_idx" ON "MentorNote"("authorId");
CREATE INDEX "MentorMilestoneCompletion_completedById_idx" ON "MentorMilestoneCompletion"("completedById");
CREATE INDEX "Webhook_createdById_idx" ON "Webhook"("createdById");
CREATE INDEX "WebhookDelivery_event_idx" ON "WebhookDelivery"("event");
CREATE INDEX "Message_roundId_idx" ON "Message"("roundId");
CREATE INDEX "EvaluationDiscussion_closedById_idx" ON "EvaluationDiscussion"("closedById");
CREATE INDEX "DiscussionComment_discussionId_idx" ON "DiscussionComment"("discussionId");
CREATE INDEX "DiscussionComment_userId_idx" ON "DiscussionComment"("userId");