|
| 1 | +-- Add new columns to collections table |
| 2 | +ALTER TABLE public.collections |
| 3 | + ADD COLUMN "isPrivate" BOOLEAN NOT NULL DEFAULT false, |
| 4 | + ADD COLUMN "ssoUserId" VARCHAR(255), -- nullable: only set for community collections |
| 5 | + ADD COLUMN "logoUrl" TEXT; -- nullable: only set for curated collections |
| 6 | + |
| 7 | +-- Create ssoUsers table (id = SSO subject ID, e.g. "auth0|abc123") |
| 8 | +CREATE TABLE public."insightsSsoUsers" ( |
| 9 | + id VARCHAR(255) PRIMARY KEY, |
| 10 | + "displayName" VARCHAR(255), |
| 11 | + "avatarUrl" TEXT, |
| 12 | + "email" VARCHAR(255), |
| 13 | + "username" VARCHAR(255), |
| 14 | + "accountId" VARCHAR(255), |
| 15 | + "accountName" VARCHAR(255), |
| 16 | + "accountWebsite" TEXT, |
| 17 | + "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() |
| 18 | +); |
| 19 | + |
| 20 | +-- FK: collections.ssoUserId -> ssoUsers.id |
| 21 | +ALTER TABLE public.collections |
| 22 | + ADD CONSTRAINT "fk_collections_ssoUserId" |
| 23 | + FOREIGN KEY ("ssoUserId") REFERENCES public."insightsSsoUsers"(id) ON DELETE SET NULL; |
| 24 | + |
| 25 | +-- Create collectionsRepositories table |
| 26 | +CREATE TABLE public."collectionsRepositories" ( |
| 27 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 28 | + "collectionId" UUID NOT NULL REFERENCES public.collections(id) ON DELETE CASCADE, |
| 29 | + "repoId" UUID NOT NULL REFERENCES public.repositories(id) ON DELETE CASCADE, |
| 30 | + "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), |
| 31 | + "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), |
| 32 | + "deletedAt" TIMESTAMP WITH TIME ZONE |
| 33 | +); |
| 34 | + |
| 35 | +CREATE INDEX "ix_collectionsRepositories_collectionId" |
| 36 | + ON public."collectionsRepositories" ("collectionId") |
| 37 | + WHERE "deletedAt" IS NULL; |
| 38 | + |
| 39 | +-- Create collectionLikes table |
| 40 | +CREATE TABLE public."collectionLikes" ( |
| 41 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 42 | + "collectionId" UUID NOT NULL REFERENCES public.collections(id) ON DELETE CASCADE, |
| 43 | + "ssoUserId" VARCHAR(255) NOT NULL REFERENCES public."insightsSsoUsers"(id) ON DELETE CASCADE, |
| 44 | + "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), |
| 45 | + "deletedAt" TIMESTAMP WITH TIME ZONE |
| 46 | +); |
| 47 | + |
| 48 | +-- Partial unique index so a user can re-like after unliking (soft delete) |
| 49 | +CREATE UNIQUE INDEX "ix_collectionLikes_unique_active" |
| 50 | + ON public."collectionLikes" ("collectionId", "ssoUserId") |
| 51 | + WHERE "deletedAt" IS NULL; |
| 52 | + |
| 53 | +CREATE INDEX "ix_collectionLikes_collectionId" |
| 54 | + ON public."collectionLikes" ("collectionId") |
| 55 | + WHERE "deletedAt" IS NULL; |
0 commit comments