Skip to content

Conversation

ginzahatemi
Copy link
Collaborator

@ginzahatemi ginzahatemi commented Sep 15, 2025

Description:
This PR updates the Prisma schema to support hierarchical content by:

  • Adding a new Subpost model for child entries under a post.
  • Establishing a one-to-many relationship between Post and Subpost (each post can have multiple subposts).
  • Ensuring the Post model includes a title field.
  • Including timestamps (createdAt, updatedAt) for both models.

These changes enable posts to have associated subposts, supporting threaded or nested content structures.

Summary by CodeRabbit

  • New Features

    • Nested "subposts" supported under posts with content, sentiment, source, categories, and optional links.
    • Posts now surface aggregated metadata: total subposts and bullish/bearish/neutral summaries for richer displays.
  • Chores

    • Database migration applied to enable the new subpost model and post summaries; no user action required.

Copy link

coderabbitai bot commented Sep 15, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds a new Subpost model and Post→Subpost relation in the Prisma schema, restructures Post fields (drops content/categories and adds summary/total fields), removes two GIN indexes, and includes two SQL migrations: one creating public.Subpost with FK to public.Post and one altering Post/Subpost columns (with noted data-loss warnings).

Changes

Cohort / File(s) Summary
Prisma schema
prisma/schema.prisma
Adds Post updates (adds subposts: Subpost[], totalSubposts, bullishSummary, bearishSummary, neutralSummary), adds Subpost model (fields: id, content, sentiment, source, categories[], subcategories[], link?, postId, post relation, createdAt, updatedAt), removes title from Subpost, removes @@index([categories], type: Gin) and @@index([subcategories], type: Gin), minor whitespace tweak on User.email.
Migration — create Subpost
prisma/migrations/.../20250915081725_add_subpost_model/migration.sql
Creates table public.Subpost (id TEXT PK, content TEXT NOT NULL, postId TEXT NOT NULL, createdAt TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, updatedAt TIMESTAMP(3) NOT NULL) and FK Subpost_postId_fkeypublic.Post(id) (ON DELETE RESTRICT, ON UPDATE CASCADE).
Migration — update Post/Subpost
prisma/migrations/.../20250915090632_update_post_subpost_model/migration.sql
Alters public.Post: drops columns (categories, content, createdAt, sentiment, source, subcategories, updatedAt) and drops GIN indexes; adds bearishSummary, bullishSummary, neutralSummary (TEXT) and totalSubposts (INTEGER NOT NULL DEFAULT 0). Alters public.Subpost: adds categories (TEXT[] DEFAULT []), subcategories (TEXT[] DEFAULT []), sentiment (public.Sentiment NOT NULL), source (public.Source NOT NULL), and link (TEXT). Includes data-loss warnings for dropped Post columns and NOT NULL additions.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant API
  participant Prisma
  participant DB

  rect rgba(200,230,255,0.18)
  note right of Client: Create Subpost (new flow)
  Client->>API: POST /posts/:postId/subposts { content, sentiment, ... }
  API->>Prisma: prisma.subpost.create({ data: { content, sentiment, postId, ... } })
  Prisma->>DB: INSERT INTO public.Subpost (...)
  DB-->>Prisma: 201 Created (FK validated)
  Prisma-->>API: Subpost record
  API-->>Client: 201 Created (Subpost)
  end

  rect rgba(220,255,220,0.14)
  note right of Client: Fetch Post with summaries & subposts
  Client->>API: GET /posts/:postId?include=subposts
  API->>Prisma: prisma.post.findUnique({ include: { subposts: true }})
  Prisma->>DB: SELECT Post LEFT JOIN Subpost
  DB-->>Prisma: Post (with summary fields) + Subpost[]
  Prisma-->>API: Aggregated result
  API-->>Client: 200 OK
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • tasin2610

Poem

"I hop through schema rows tonight,
I plant a Subpost, small and bright.
Keys and timestamps in a tidy row,
I nibble migrations as they grow.
Hooray — another table takes a bite! 🥕"

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Add Subpost model and one-to-many relation to Post in Prisma schema" accurately and concisely describes the primary change — adding a Subpost model and linking it to Post in the Prisma schema — and matches the migration and schema updates in the changeset; it is specific, single-sentence, and free of extraneous noise. It gives a teammate scanning history a clear sense of the main intent without needing to list every detail. Therefore it aligns with the provided title guidelines.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 58d7c87 and 162cb0d.

📒 Files selected for processing (1)
  • prisma/migrations/20250915090632_update_post_subpost_model/migration.sql (1 hunks)

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
prisma/schema.prisma (1)

67-74: Index postId and encode referential actions explicitly in schema.

  • Add an index on postId (and optionally (postId, createdAt DESC)) to speed up common lookups.
  • The migration sets ON DELETE RESTRICT ON UPDATE CASCADE, but the schema omits referential actions. Encode them explicitly to prevent drift.

Apply:

 model Subpost {
   id        String   @id @default(uuid())
   content   String
   postId    String
-  post      Post     @relation(fields: [postId], references: [id])
+  post      Post     @relation(fields: [postId], references: [id], onDelete: Restrict, onUpdate: Cascade)
   createdAt DateTime @default(now())
   updatedAt DateTime @updatedAt
+
+  @@index([postId])
+  // If you frequently fetch latest-first:
+  // @@index([postId, createdAt(sort: Desc)])
 }

Additional notes:

  • If you intend parent deletion to remove children, switch onDelete to Cascade (and update the migration accordingly). Otherwise keep Restrict.
  • If your app relies on absolute UTC semantics, consider DateTime @db.Timestamptz for createdAt/updatedAt in both Post and Subpost in a follow-up migration.
prisma/migrations/20250915081725_add_subpost_model/migration.sql (2)

1-10: Add index on Subpost.postId (and optionally on postId,createdAt).

PostgreSQL doesn’t auto-index FKs; without it, fetching subposts by post can degrade under load.

If this migration hasn’t been applied anywhere yet, append:

 CREATE TABLE "public"."Subpost" (
   "id" TEXT NOT NULL,
   "content" TEXT NOT NULL,
   "postId" TEXT NOT NULL,
   "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
   "updatedAt" TIMESTAMP(3) NOT NULL,
 
   CONSTRAINT "Subpost_pkey" PRIMARY KEY ("id")
 );
+
+-- Speed up lookups by parent and recency
+CREATE INDEX "Subpost_postId_idx" ON "public"."Subpost"("postId");
+-- Optional if you list recent subposts per post frequently:
+-- CREATE INDEX "Subpost_postId_createdAt_desc_idx" ON "public"."Subpost"("postId", "createdAt" DESC);

If it’s already applied, create a new migration with the index statements instead.


12-13: Validate delete semantics (Restrict vs Cascade).

ON DELETE RESTRICT blocks deleting a Post with children. If product expects parent deletion to also remove subposts, switch to ON DELETE CASCADE and mirror it in the Prisma schema (onDelete: Cascade). Otherwise, keep as-is.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b9abda0 and f7ab5be.

📒 Files selected for processing (2)
  • prisma/migrations/20250915081725_add_subpost_model/migration.sql (1 hunks)
  • prisma/schema.prisma (2 hunks)
🔇 Additional comments (3)
prisma/schema.prisma (3)

19-19: No-op whitespace change.

Nothing to do here.


61-61: Relation field looks good; watch for N+1 at call sites.

The subposts: Subpost[] addition is correct. Ensure query layers load subposts via include or a batched loader to avoid N+1.


67-74: Resolved — Post.title already created in migrations.

prisma/migrations/20250904104634_add_post_model/migration.sql creates the Post table with "title" TEXT NOT NULL (lines 5–9), so no separate title backfill/migration is required.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
prisma/schema.prisma (1)

61-73: Make FK actions explicit and index postId; consider restoring array GIN indexes.

Explicitly align referential actions with the migration and add an index for join performance. Re-add GIN indexes if you query array containment.

 model Subpost {
   id            String    @id @default(uuid())
   content       String
   sentiment     Sentiment
   source        Source
   categories    String[]  @default([])
   subcategories String[]  @default([])
   link          String?
   postId        String
-  post          Post      @relation(fields: [postId], references: [id])
+  post          Post      @relation(fields: [postId], references: [id], onDelete: Restrict, onUpdate: Cascade)
   createdAt     DateTime  @default(now())
   updatedAt     DateTime  @updatedAt
+  @@index([postId])
+  // If you frequently query by array containment, consider:
+  // @@index([categories], type: Gin)
+  // @@index([subcategories], type: Gin)
 }

If you actually want parent deletion to remove subposts, switch onDelete: Cascade. Confirm desired behavior.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f7ab5be and 58d7c87.

📒 Files selected for processing (1)
  • prisma/schema.prisma (2 hunks)
🔇 Additional comments (1)
prisma/schema.prisma (1)

19-19: No-op change on User.email — OK to keep.

Semantics unchanged; no action needed.

@tasin2610 tasin2610 merged commit 098b81b into main Sep 15, 2025
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants