feat: initial collaboration implementation

This commit is contained in:
2024-12-22 23:43:27 +01:00
parent c5f2dce36e
commit 737ffca4bc
21 changed files with 489 additions and 30 deletions

View File

@@ -0,0 +1,24 @@
-- thanks ai
-- Step 1: Create the new UserProject table
CREATE TABLE "UserProject" (
"userId" TEXT NOT NULL,
"projectId" TEXT NOT NULL,
CONSTRAINT "UserProject_pkey" PRIMARY KEY ("userId","projectId")
);
-- Step 2: Insert existing userId and projectId pairs into the UserProject table
INSERT INTO "UserProject" ("userId", "projectId")
SELECT "userId", "id" FROM "Project" WHERE "userId" IS NOT NULL;
-- Step 3: Drop the foreign key constraint and the userId column from the Project table
ALTER TABLE "Project" DROP CONSTRAINT "Project_userId_fkey";
ALTER TABLE "Project" DROP COLUMN "userId";
-- Step 4: Add foreign key constraints to the UserProject table
ALTER TABLE "UserProject" ADD CONSTRAINT "UserProject_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "UserProject" ADD CONSTRAINT "UserProject_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- Step 5: Drop the unnecessary _UserProjects table and its constraints
DROP TABLE IF EXISTS "_UserProjects";

View File

@@ -0,0 +1,40 @@
/*
Warnings:
- A unique constraint covering the columns `[inviteCode]` on the table `Project` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "Project" ADD COLUMN "inviteCode" TEXT NOT NULL DEFAULT floor(random() * 90000000 + 10000000)::text;
-- CreateTable
CREATE TABLE "_UserProjects" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL,
CONSTRAINT "_UserProjects_AB_pkey" PRIMARY KEY ("A","B")
);
-- CreateIndex
CREATE INDEX "_UserProjects_B_index" ON "_UserProjects"("B");
-- CreateIndex
CREATE INDEX "Feedback_projectId_idx" ON "Feedback"("projectId");
-- CreateIndex
CREATE UNIQUE INDEX "Project_inviteCode_key" ON "Project"("inviteCode");
-- CreateIndex
CREATE INDEX "Session_userId_idx" ON "Session"("userId");
-- CreateIndex
CREATE INDEX "UserProject_userId_idx" ON "UserProject"("userId");
-- CreateIndex
CREATE INDEX "UserProject_projectId_idx" ON "UserProject"("projectId");
-- AddForeignKey
ALTER TABLE "_UserProjects" ADD CONSTRAINT "_UserProjects_A_fkey" FOREIGN KEY ("A") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "_UserProjects" ADD CONSTRAINT "_UserProjects_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Project" ALTER COLUMN "inviteCode" SET DEFAULT floor(random() * 90000000 + 10000000)::text;
-- AlterTable
ALTER TABLE "UserProject" ADD COLUMN "isOwner" BOOLEAN NOT NULL DEFAULT false;

View File

@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"

View File

@@ -14,12 +14,13 @@ datasource db {
}
model User {
id String @id @default(cuid())
githubId String @unique
id String @id @default(cuid())
githubId String @unique
username String
installations String[]
projects Project[]
projects Project[] @relation("UserProjects")
sessions Session[]
UserProject UserProject[]
}
model Session {
@@ -27,6 +28,8 @@ model Session {
userId String
expiresAt DateTime
user User @relation(references: [id], fields: [userId], onDelete: Cascade)
@@index([userId])
}
model Project {
@@ -37,17 +40,32 @@ model Project {
customData String[]
rateLimitReq Int @default(5)
rateLimitTime Int @default(60)
// 8 digit random number
inviteCode String @unique @default(dbgenerated("floor(random() * 90000000 + 10000000)::text"))
userId String
user User @relation(fields: [userId], references: [id])
feedback Feedback[]
users User[] @relation("UserProjects")
feedback Feedback[]
UserProject UserProject[]
}
model UserProject {
userId String
projectId String
isOwner Boolean @default(false)
user User @relation(fields: [userId], references: [id])
project Project @relation(fields: [projectId], references: [id])
@@id([userId, projectId])
@@index([userId])
@@index([projectId])
}
model Feedback {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
message String
customData String
projectId String
project Project @relation(fields: [projectId], references: [id])
projectId String
project Project @relation(fields: [projectId], references: [id])
@@index([projectId])
}