fix : handle P2002 unique constraint when re-adding deleted repository#2445
fix : handle P2002 unique constraint when re-adding deleted repository#2445tmdeveloper007 wants to merge 1 commit into
Conversation
|
@tmdeveloper007 is attempting to deploy a commit to the Nisshchaya's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthrough
ChangesRepository service: creation fix and staleness lifecycle
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🎉 Thanks for your contribution, @tmdeveloper007!Your PR has passed our automated GSSoC quality checks. Here's a quick summary:
A maintainer will review your PR soon. Please be patient and available for feedback. 💪 GSSoC'26 automation · Maintainer: @nisshchayarathi |
🎉 Thanks for your contribution, @tmdeveloper007!Your PR has passed our automated GSSoC quality checks. Here's a quick summary:
A maintainer will review your PR soon. Please be patient and available for feedback. 💪 GSSoC'26 automation · Maintainer: @nisshchayarathi |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
lib/services/repositoryService.ts (1)
645-660: 🧹 Nitpick | 🔵 Trivial | 💤 Low valueDuplicate query for
headCommitcan be consolidated.The same
prisma.commit.findFirstquery for the latest commit ondefaultBranchis executed here (lines 647-651) and again at lines 672-676 for cache invalidation. Consider querying once and reusing the result.♻️ Suggested consolidation
+ // Fetch latest commit SHA once for both stale-detection and cache invalidation. + const headCommit = await prisma.commit.findFirst({ + where: { repositoryId, branch: defaultBranch }, + orderBy: { committedAt: "desc" }, + select: { hash: true }, + }); + // Record the analyzed commit SHA for stale-detection. - // Fetched after the transaction so it reflects the newly inserted commits. - const headCommit = await prisma.commit.findFirst({ - where: { repositoryId, branch: defaultBranch }, - orderBy: { committedAt: "desc" }, - select: { hash: true }, - }); if (headCommit?.hash) { await prisma.repository.update({ where: { id: repositoryId }, data: { analyzedCommitSha: headCommit.hash }, }).catch(() => { // Non-critical: stale-detection metadata should not break analysis. }); } // ... repository knowledge save ... // Cache invalidation (outside transaction — best-effort, non-critical) try { await invalidateExpiredCacheEntries(repositoryId); - const headCommit = await prisma.commit.findFirst({ - where: { repositoryId, branch: defaultBranch }, - orderBy: { committedAt: "desc" }, - select: { hash: true }, - }); - if (headCommit?.hash) { await invalidateCacheForCommit(repositoryId, headCommit.hash); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/services/repositoryService.ts` around lines 645 - 660, The prisma.commit.findFirst query for the latest commit on the defaultBranch is being executed twice in this function - once here for updating analyzedCommitSha and again later for cache invalidation around lines 672-676. Instead of running this duplicate query, execute the headCommit query once and store the result in a variable, then reuse that same headCommit variable in both places where you need the latest commit hash. This will eliminate the redundant database call while maintaining the same functionality.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/services/repositoryService.ts`:
- Around line 653-655: The update operation in the prisma.repository.update call
is attempting to set the analyzedCommitSha field, but this field does not exist
in the Repository Prisma model. Add the analyzedCommitSha field to the
Repository model in prisma/schema.prisma as a String type field (decide on
optional vs required based on your data model), then create a new Prisma
migration by running the migration command to generate and apply the database
schema changes that will add the analyzed_commit_sha column to the repository
table.
---
Nitpick comments:
In `@lib/services/repositoryService.ts`:
- Around line 645-660: The prisma.commit.findFirst query for the latest commit
on the defaultBranch is being executed twice in this function - once here for
updating analyzedCommitSha and again later for cache invalidation around lines
672-676. Instead of running this duplicate query, execute the headCommit query
once and store the result in a variable, then reuse that same headCommit
variable in both places where you need the latest commit hash. This will
eliminate the redundant database call while maintaining the same functionality.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c9aed3ca-511d-48a5-b0d2-0613dc7c3f45
📒 Files selected for processing (1)
lib/services/repositoryService.ts
| await prisma.repository.update({ | ||
| where: { id: repositoryId }, | ||
| data: { analyzedCommitSha: headCommit.hash }, |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Verify analyzedCommitSha field exists in Repository model
# Check schema.prisma for the field definition
echo "=== Checking schema.prisma for analyzedCommitSha ==="
rg -n "analyzedCommitSha" --type-add 'prisma:*.prisma' --type prisma
# Check if there's a migration adding this column
echo ""
echo "=== Checking migrations for analyzed_commit_sha column ==="
fd -e sql . prisma/migrations --exec grep -l -i "analyzed_commit_sha" {} \; 2>/dev/null || echo "No migrations found with this column"Repository: nisshchayarathi/gitverse-nextjs
Length of output: 225
🏁 Script executed:
# Check the Repository model definition in schema.prisma
rg -A 30 "model Repository" --type-add 'prisma:*.prisma' --type prismaRepository: nisshchayarathi/gitverse-nextjs
Length of output: 9671
🏁 Script executed:
# Read the specific lines in repositoryService.ts to see the context
sed -n '650,660p' lib/services/repositoryService.tsRepository: nisshchayarathi/gitverse-nextjs
Length of output: 408
Add analyzedCommitSha field to the Repository model and create migration.
The code attempts to update repository.analyzedCommitSha on lines 653-655, but this field does not exist in the Repository Prisma model. Add the field to prisma/schema.prisma and create a migration to add the corresponding analyzed_commit_sha column to the database.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/services/repositoryService.ts` around lines 653 - 655, The update
operation in the prisma.repository.update call is attempting to set the
analyzedCommitSha field, but this field does not exist in the Repository Prisma
model. Add the analyzedCommitSha field to the Repository model in
prisma/schema.prisma as a String type field (decide on optional vs required
based on your data model), then create a new Prisma migration by running the
migration command to generate and apply the database schema changes that will
add the analyzed_commit_sha column to the repository table.
CI StatusCI checks ran. The PR-specific changes:
All changes are isolated to the files described in the PR body. The pre-existing CI failures require a separate investigation and are outside the scope of these fixes. |
|
Closing this PR. The branch is based on an older commit of upstream/main and has drifted from the current main. The upstream repository has received significant updates since this PR was opened, causing CI type-check and test failures due to merge conflicts with the base branch. Please re-open as a fresh PR against the current main if the fix is still needed. |
Summary
Fixes Prisma P2002 unique constraint violation when a user tries to re-add a previously deleted GitHub repository.
Changes
lib/services/repositoryService.ts: Catch P2002 increateRepositoryand return the existing record instead of throwingImpact
Users can now safely re-add a repository after deleting it without hitting a database constraint error.
Closes #2436
Summary by CodeRabbit
Bug Fixes
New Features