Skip to content

(janitor/dedupe): consolidate Postgres unique-violation detection - #6787

Merged
kilo-code-bot[bot] merged 1 commit into
mainfrom
janitor/dedupe-postgres-unique-violation
Sep 28, 2026
Merged

kilo-code-bot[bot] merged 1 commit into
mainfrom
janitor/dedupe-postgres-unique-violation

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Consolidates the duplicated "is this a PostgreSQL unique-constraint violation (SQLSTATE 23505)" logic scattered across apps/web into a single module, apps/web/src/lib/db-errors.ts.

Problem

Seven local implementations had already drifted:

Site Behavior
webhook-triggers-router.ts top-level code only
kiloclaw/access-codes.ts top-level code only
manual-code-review-jobs.ts (getDatabaseErrorCode) top-level code only
credit-campaigns-router.ts top-level + one .cause level
quick-chat-router.ts top-level + one .cause level
verified-domain-service.ts top-level + recursive .cause
cloud-billing-skus-router.ts (postgresErrorCode) recursive .cause
deletion-queue handlers ×2 (postgresErrorCode) bounded .cause walk

Drizzle 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.ts now owns two functions:

  • getPostgresErrorCode(error) — walks a bounded (depth 5) .cause chain and returns the SQLSTATE, disambiguated from unrelated code fields (e.g. Node's ENOTFOUND) 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 describeDatabaseError are intentionally left untouched.

Verification

  • pnpm format on changed files — clean.
  • oxlint on changed files — 0 warnings/errors.
  • tsgo --noEmit (web) — exit 0.
  • Added 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 via tsx and all assertions pass.

Fixes the latent drift where wrapped unique violations were not detected in three call sites.

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.
@kilo-code-bot kilo-code-bot Bot added the janitor Automated dead-code/duplication cleanup label Sep 28, 2026
@kilo-code-bot
kilo-code-bot Bot enabled auto-merge (squash) September 28, 2026 07:25
@kilo-code-bot

kilo-code-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

The PR cleanly consolidates seven drifted Postgres unique-violation/error-code helpers into a single bounded, unit-tested apps/web/src/lib/db-errors.ts; all eleven changed call sites correctly preserve or improve prior detection behavior with no regressions found.

Files Reviewed (11 files)
  • apps/web/src/lib/db-errors.ts
  • apps/web/src/lib/db-errors.test.ts
  • apps/web/src/lib/code-reviews/manual-code-review-jobs.ts
  • apps/web/src/lib/kiloclaw/access-codes.ts
  • apps/web/src/lib/organizations/verified-domain-service.ts
  • apps/web/src/lib/user/deletion-queue/handlers/anonymize.ts
  • apps/web/src/lib/user/deletion-queue/handlers/usage-prompt-prefixes.ts
  • apps/web/src/routers/admin/cloud-billing-skus-router.ts
  • apps/web/src/routers/admin/credit-campaigns-router.ts
  • apps/web/src/routers/quick-chat-router.ts
  • apps/web/src/routers/webhook-triggers-router.ts

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

janitor Automated dead-code/duplication cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant