fix: batch automation sync DB writes with Prisma transactions#343
Merged
Conversation
added 6 commits
June 9, 2026 07:24
…oderate advisories - Pin aquasecurity/trivy-action to SHA ed142fd (v0.36.0) instead of @master for reproducible CI security scanning - Fix .npmrc invalid omit config (omit= → omit=dev) - Tighten version ranges: next ^16.2.7, prisma ^7.8.0, @prisma/client ^7.8.0 - Document accepted risk for 5 moderate advisories in SECURITY-ACCEPTED-RISKS.md (next/postcss XSS and prisma/@hono/node-server bypass require major downgrades)
# Conflicts: # src/app/api/issues/untriaged.test.ts # src/app/api/issues/untriaged/route.ts
- Fix trivy-action SHA: ed142fd...36625 → ed142fd...36c25 (v0.36.0 tag) - Remove .npmrc (omit=dev skips devDeps, breaks lint step; main has no .npmrc)
Replace sequential upsert loops with prisma.$transaction batches for workflows, runs, jobs, releases, PRs, and packages. This reduces write amplification from O(n) individual round-trips to O(1) per entity type, significantly cutting DB latency during multi-repo sync runs. Refs #318
There was a problem hiding this comment.
AI Automated Review
Full PR review.
Analysis engine: review@https://litellm.jory.dev/v1 (openai)
Recommendation
Approve. This PR successfully addresses the write amplification issue identified in PR 318 by replacing sequential database operations with batched Prisma transactions. This significantly improves the efficiency of the automation sync process.
Change-by-Change Findings
src/app/api/automation/sync/route.ts
-
Batching Implementation: Replaced multiple
await prisma.<model>.upsert(...)calls within loops withprisma.$transactionwrapping arrays of upsert operations forgithubWorkflow,githubWorkflowRun,githubWorkflowJob,githubRelease,githubPullRequest, andgithubPackage. -
Efficiency Gains: Reduces the number of database round-trips from
$O(n)$ to$O(1)$ per entity type. -
Robustness:
- Added explicit timeouts (30s/60s) to transactions to prevent long-running hangs.
- Implemented a placeholder mechanism for
githubWorkflowto ensuregithubWorkflowRunupserts succeed even if the workflow name is not yet in the local cache. - Optimized job fetching by only querying for jobs on
completedruns.
-
Correctness: The logic for resolving workflow IDs via a
workflowMapbefore performing run upserts is sound and prevents foreign key violations.
.github/workflows/image.yaml
- Security: Pinned
aquasecurity/trivy-actionto a specific SHA (ed142fd0673e97e23eac54620cfb913e5ce36c25) instead of using@master. This is a best practice for reproducible and secure CI pipelines.
SECURITY-ACCEPTED-RISKS.md
- Documentation: Added a new file documenting accepted moderate-severity risks (Next.js/PostCSS XSS and Prisma/Hono middleware bypass). This provides necessary transparency for security audits.
src/app/api/health/route.ts & docs/smoke-checklist.md
- Consistency: Updated the health endpoint to use a dynamic versioning helper (
getAppVersion()) rather than a hardcoded string, ensuring the smoke checklist remains accurate.
package.json & package-lock.json
- Dependency Updates: Upgraded
prismaand@prisma/clientto^7.8.0to match the project's tech stack requirements.
Standards Compliance
- Code Standards: Follows the pattern of using transactions for bulk operations and provides meaningful error handling via timeouts.
- Security: Pinned CI actions and documented accepted risks as per repository security protocols.
- Prisma: Uses
prisma.$transactioncorrectly for batching, adhering to the requirement for efficient database operations.
Linked Issue Fit
- Issue PR 318: The PR directly implements the recommendation to "batch obvious independent writes" to reduce write amplification. It addresses the specific evidence provided in the issue regarding the loop in
src/app/api/automation/sync/route.ts.
Unknowns or Needs Verification
-
Performance: While
$O(1)$ round-trips is a significant improvement, very large batches might hit PostgreSQL transaction size limits or memory constraints. However, the implementation ofprs.slice(0, 50)for PRs suggests a reasonable approach to bounding work.
Change .filter(Boolean) to explicit type guard filter to properly narrow PrismaPromise types for $transaction() compatibility.
There was a problem hiding this comment.
AI Automated Review
Full PR review.
Analysis engine: review@https://litellm.jory.dev/v1 (openai)
Recommendation
Approve. This PR successfully addresses the write amplification issue identified in PR 318 by replacing sequential database upserts with batched Prisma transactions. This significantly improves the efficiency of the automation sync process.
Change-by-Change Findings
Automation Sync Optimization (src/app/api/automation/sync/route.ts)
- Workflow Upserts: Replaced the
for...ofloop with a singleprisma.$transactioncontaining all workflow upserts. Added a 30s timeout. - Run Upserts: Implemented a two-stage batching process:
- Resolves unknown workflow names by creating placeholder workflows in a transaction.
- Batches all
githubWorkflowRunupserts in a single transaction with a 60s timeout.
- Job Upserts: Optimized job ingestion by only fetching and batching jobs for runs that have a
completedstatus, using a single transaction per run. - Release/PR/Package Upserts: Converted sequential loops for releases, PRs (limited to 50), and packages into batched transactions.
Security & Maintenance
- Trivy Pinning: Pinned
aquasecurity/trivy-actionto a specific SHA (ed142fd) to ensure build reproducibility and prevent supply chain attacks. - Risk Documentation: Added
SECURITY-ACCEPTED-RISKS.mdto formally document and track moderate-severity vulnerabilities innextandprismathat cannot be fixed without breaking changes. - Health Endpoint: Refactored
src/app/api/health/route.tsto use a centralizedgetAppVersion()helper instead of relying on environment variables, ensuring consistency withpackage.json.
Dependency Updates
- Updated
prismaand@prisma/clientto^7.8.0inpackage.jsonandpackage-lock.json.
Standards Compliance
- Prisma Usage: Follows the recommendation to use transactions for multiple operations. The use of
upsertwithin transactions is appropriate for idempotent sync operations. - Error Handling: The implementation maintains existing error handling patterns and adds timeouts to long-running transactions to prevent database hangs.
- Code Style: Follows the repository's pattern of using
prisma.$transactionfor batching and maintains consistent error handling.
Linked Issue Fit
- Matches PR 318: The PR directly implements the recommendation to "batch obvious independent writes" to reduce write amplification during automation sync.
Unknowns or Needs Verification
- Transaction Timeouts: The PR introduces explicit timeouts (30s/60s) for transactions. While this prevents hangs, extremely large repository syncs might hit these limits. However, given the context of typical GitHub repo sizes, this is a reasonable safeguard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #318
Batch automation sync database writes using
prisma.$transaction()instead of sequential individual upserts. This reduces write amplification from O(n) round-trips to O(1) per entity type.Changes:
All transactions include explicit timeouts to prevent hangs.