Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions prisma/migrations/20250909132930_added_projects/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- CreateEnum
CREATE TYPE "public"."ProjectType" AS ENUM ('post', 'article', 'ebook', 'script');

-- CreateTable
CREATE TABLE "public"."projects" (
"id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"type" "public"."ProjectType" NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"userId" TEXT NOT NULL,

CONSTRAINT "projects_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "public"."projects" ADD CONSTRAINT "projects_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Comment on lines +11 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add an index on the foreign key for performance.

FK columns are common join/filter predicates; indexing avoids full scans and helps FK checks.

Apply after the table creation:

 ALTER TABLE "public"."projects" ADD CONSTRAINT "projects_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
+
+-- Indexes
+CREATE INDEX "projects_userId_idx" ON "public"."projects" ("userId");

If you frequently filter by user + type, consider a composite:

CREATE INDEX "projects_userId_type_idx" ON "public"."projects" ("userId","type");
🤖 Prompt for AI Agents
In prisma/migrations/20250909132930_added_projects/migration.sql around lines 11
to 17, the migration adds a foreign key on "userId" but does not create an index
for that FK; add a CREATE INDEX statement after the ALTER TABLE to index
"userId" (e.g. name it projects_userId_idx) to speed joins and FK checks, and if
you commonly query by user and type create a composite index
projects_userId_type_idx on ("userId","type") instead; ensure the statements are
added after the table creation/ALTER so the migration applies the indexes.

32 changes: 26 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ datasource db {
}

model User {
id String @id @default(cuid())
email String @unique
id String @id @default(cuid())
email String @unique
name String?
password String
role Role @default(USER)
tokens Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
role Role @default(USER)
tokens Int @default(0)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
projects Project[]

@@map("users")
}
Expand All @@ -31,3 +32,22 @@ enum Role {
USER
ADMIN
}

enum ProjectType {
post
article
ebook
script
}

model Project {
id String @id @default(uuid())
title String
type ProjectType
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String

@@map("projects")
}