-- CreateEnum CREATE TYPE "FamilyBond" AS ENUM ('PARENT', 'PARTNER', 'SIBLING'); -- AlterTable ALTER TABLE "Relation" ADD COLUMN "bond" "FamilyBond", ADD COLUMN "endDate" TEXT, ADD COLUMN "startDate" TEXT; -- CreateTable CREATE TABLE "StoryEvent" ( "id" TEXT NOT NULL, "bookId" TEXT NOT NULL, "title" TEXT NOT NULL, "date" TEXT NOT NULL, "endDate" TEXT, "description" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "StoryEvent_pkey" PRIMARY KEY ("id") ); -- CreateTable CREATE TABLE "EventParticipant" ( "id" TEXT NOT NULL, "eventId" TEXT NOT NULL, "entityId" TEXT NOT NULL, CONSTRAINT "EventParticipant_pkey" PRIMARY KEY ("id") ); -- CreateIndex CREATE INDEX "StoryEvent_bookId_idx" ON "StoryEvent"("bookId"); -- CreateIndex CREATE INDEX "EventParticipant_entityId_idx" ON "EventParticipant"("entityId"); -- CreateIndex CREATE UNIQUE INDEX "EventParticipant_eventId_entityId_key" ON "EventParticipant"("eventId", "entityId"); -- AddForeignKey ALTER TABLE "StoryEvent" ADD CONSTRAINT "StoryEvent_bookId_fkey" FOREIGN KEY ("bookId") REFERENCES "Book"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "EventParticipant" ADD CONSTRAINT "EventParticipant_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "StoryEvent"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- AddForeignKey ALTER TABLE "EventParticipant" ADD CONSTRAINT "EventParticipant_entityId_fkey" FOREIGN KEY ("entityId") REFERENCES "Entity"("id") ON DELETE CASCADE ON UPDATE CASCADE; -- Befintliga relationer med de vanliga förslagstexterna blir släktband UPDATE "Relation" SET "bond" = 'PARENT' WHERE lower("type") IN ('förälder till', 'parent of'); UPDATE "Relation" SET "bond" = 'PARTNER' WHERE lower("type") IN ('partner till', 'partner of'); UPDATE "Relation" SET "bond" = 'SIBLING' WHERE lower("type") IN ('syskon till', 'sibling of'); -- "Barn till" vänds till "Förälder till" åt andra hållet (bara när relationen saknar extra deltagare) UPDATE "Relation" r SET "bond" = 'PARENT', "fromId" = r."toId", "toId" = r."fromId", "type" = CASE WHEN lower(r."type") = 'child of' THEN 'Parent of' ELSE 'Förälder till' END WHERE lower(r."type") IN ('barn till', 'child of') AND NOT EXISTS (SELECT 1 FROM "RelationMember" m WHERE m."relationId" = r."id");