(janitor/dedupe): consolidate Postgres unique-violation detection - #6787
Merged
kilo-code-bot[bot] merged 1 commit intoSep 28, 2026
Merged
Conversation
Extract the duplicated "is this a PostgreSQL unique-constraint violation (SQLSTATE 23505)" checks into a single helper, and the generic SQLSTATE extraction it builds on, in lib/db-errors.ts. Replaces seven local implementations that had already drifted: some walked the Drizzle-wrapped `.cause` chain while others only inspected the top-level error, so wrapped violations went undetected in a few call sites. All call sites now share one bounded cause-chain walk.
Contributor
Author
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe PR cleanly consolidates seven drifted Postgres unique-violation/error-code helpers into a single bounded, unit-tested Files Reviewed (11 files)
Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0 Review guidance: REVIEW.md from base branch |
pandemicsyn
approved these changes
Sep 28, 2026
kilo-code-bot
Bot
deleted the
janitor/dedupe-postgres-unique-violation
branch
September 28, 2026 17:28
This was referenced Sep 27, 2026
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.
Summary
Consolidates the duplicated "is this a PostgreSQL unique-constraint violation (SQLSTATE 23505)" logic scattered across
apps/webinto a single module,apps/web/src/lib/db-errors.ts.Problem
Seven local implementations had already drifted:
webhook-triggers-router.tscodeonlykiloclaw/access-codes.tscodeonlymanual-code-review-jobs.ts(getDatabaseErrorCode)codeonlycredit-campaigns-router.ts.causelevelquick-chat-router.ts.causelevelverified-domain-service.ts.causecloud-billing-skus-router.ts(postgresErrorCode).causepostgresErrorCode).causewalkDrizzle wraps the driver error in a
DrizzleQueryError, so the SQLSTATE sits on.cause. The three "top-level only" sites silently missed wrapped violations; the one-level sites only handled a fixed depth.Change
db-errors.tsnow owns two functions:getPostgresErrorCode(error)— walks a bounded (depth 5).causechain and returns the SQLSTATE, disambiguated from unrelatedcodefields (e.g. Node'sENOTFOUND) via a[0-9A-Z]{5}shape check.isUniqueViolation(error)—getPostgresErrorCode(error) === '23505'.All nine call sites import from the shared module instead of defining a local copy. Constraint-name-specific checks (linear/slack/user-deployments/webhook-events) and the richer redaction-oriented
describeDatabaseErrorare intentionally left untouched.Verification
pnpm formaton changed files — clean.oxlinton changed files — 0 warnings/errors.tsgo --noEmit(web) — exit 0.db-errors.test.ts(pure-function unit test; does not require Postgres). Note: the Jest global setup in this environment requires Postgres, so the suite could not be executed here, but the helper logic was exercised directly viatsxand all assertions pass.Fixes the latent drift where wrapped unique violations were not detected in three call sites.