Skip to content

Commit f159e42

Browse files
authored
feat: migrations for community collections (#3883)
Signed-off-by: Gašper Grom <gasper.grom@gmail.com>
1 parent 2457b41 commit f159e42

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Drop collectionLikes table
2+
DROP TABLE IF EXISTS public."collectionLikes";
3+
4+
-- Drop collectionsRepositories table
5+
DROP TABLE IF EXISTS public."collectionsRepositories";
6+
7+
-- Drop FK before dropping insightsSsoUsers
8+
ALTER TABLE public.collections
9+
DROP CONSTRAINT IF EXISTS "fk_collections_ssoUserId";
10+
11+
-- Drop columns from collections
12+
ALTER TABLE public.collections
13+
DROP COLUMN IF EXISTS "isPrivate",
14+
DROP COLUMN IF EXISTS "ssoUserId",
15+
DROP COLUMN IF EXISTS "logoUrl";
16+
17+
-- Drop insightsSsoUsers table
18+
DROP TABLE IF EXISTS public."insightsSsoUsers";
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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;

services/libs/data-access-layer/src/collections/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface ICreateCollection {
1919
name: string
2020
slug?: string
2121
starred: boolean
22+
isPrivate?: boolean
23+
ssoUserId?: string | null
24+
logoUrl?: string | null
2225
}
2326

2427
export interface ICollection extends ICreateCollection {
@@ -80,8 +83,11 @@ export enum CollectionField {
8083
CREATED_AT = 'createdAt',
8184
DESCRIPTION = 'description',
8285
ID = 'id',
86+
IS_PRIVATE = 'isPrivate',
87+
LOGO_URL = 'logoUrl',
8388
NAME = 'name',
8489
SLUG = 'slug',
90+
SSO_USER_ID = 'ssoUserId',
8591
STARRED = 'starred',
8692
UPDATED_AT = 'updatedAt',
8793
DELETED_AT = 'deletedAt',

0 commit comments

Comments
 (0)