Skip to content

fix(data-drains): convert unique-name violations to 409 on POST/PUT#4471

Merged
waleedlatif1 merged 1 commit intostagingfrom
waleedlatif1/bozeman-v2
May 6, 2026
Merged

fix(data-drains): convert unique-name violations to 409 on POST/PUT#4471
waleedlatif1 merged 1 commit intostagingfrom
waleedlatif1/bozeman-v2

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Catch Postgres 23505 on data-drain INSERT/UPDATE so concurrent name conflicts return a clean 409 instead of a 500
  • The data_drains_org_name_unique index already prevents duplicate rows — this is a UX/error-shape fix, not a data-integrity fix

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 6, 2026 6:40am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 6, 2026

PR Summary

Low Risk
Low risk: only changes API error handling for data-drains create/update to map Postgres unique-constraint violations (23505) to a 409 response, without altering auth or data model.

Overview
Prevents POST/PUT data-drains requests from returning 500s on concurrent duplicate-name races by catching Postgres unique-violation errors (23505) during INSERT/UPDATE and returning a consistent 409 conflict response with a user-friendly message.

Reviewed by Cursor Bugbot for commit 4f02445. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 6, 2026

Greptile Summary

This PR wraps the INSERT and UPDATE Drizzle calls for data drains in try/catch blocks that intercept Postgres error code 23505 (unique_violation) and convert it to a clean 409 JSON response instead of propagating an unhandled 500.

  • POST (route.ts): adds a try/catch around the insert and a defensive if (!inserted) guard (previously typed as non-optional without a guard); the pre-check SELECT for duplicates is kept as an optimistic fast path.
  • PUT ([drainId]/route.ts): adds a try/catch around the update, matching the pattern already present for the pre-check conflict guard earlier in the same handler.

Confidence Score: 5/5

Safe to merge — the change is a focused UX fix that converts an unhandled DB exception into a clean HTTP 409 without touching any data-integrity logic.

Both handlers correctly handle the race window between the pre-check SELECT and the actual write: non-23505 errors are re-thrown so they still surface as 500s via the route handler wrapper, the null guard on the POST return row was added as requested in the prior review thread, and the getPostgresErrorCode utility already walks cause chains to handle all common Drizzle/postgres driver shapes.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/api/organizations/[id]/data-drains/route.ts Wraps the INSERT in a try/catch for 23505 → 409 and adds a defensive null guard on the returned row; pre-check SELECT is retained as a fast path for the common case.
apps/sim/app/api/organizations/[id]/data-drains/[drainId]/route.ts Wraps the UPDATE in a try/catch for 23505 → 409; the existing pre-check and the if (!updated) 404 guard are both kept intact.

Sequence Diagram

sequenceDiagram
    participant Client
    participant POST/PUT Handler
    participant Pre-check SELECT
    participant DB INSERT/UPDATE
    participant Postgres

    Client->>POST/PUT Handler: Request with drain name
    POST/PUT Handler->>Pre-check SELECT: SELECT WHERE name = ? (fast path)
    alt Name already exists (common case)
        Pre-check SELECT-->>POST/PUT Handler: row found
        POST/PUT Handler-->>Client: 409 Conflict
    else Name is free
        Pre-check SELECT-->>POST/PUT Handler: no row
        POST/PUT Handler->>DB INSERT/UPDATE: Execute write
        DB INSERT/UPDATE->>Postgres: SQL
        alt Concurrent insert won the race (23505)
            Postgres-->>DB INSERT/UPDATE: unique_violation error
            DB INSERT/UPDATE-->>POST/PUT Handler: throw error
            POST/PUT Handler-->>Client: 409 Conflict
        else Write succeeds
            Postgres-->>DB INSERT/UPDATE: returning row
            DB INSERT/UPDATE-->>POST/PUT Handler: inserted/updated row
            POST/PUT Handler-->>Client: 201/200 with drain
        end
    end
Loading

Reviews (2): Last reviewed commit: "fix(data-drains): convert unique-name vi..." | Re-trigger Greptile

Comment thread apps/sim/app/api/organizations/[id]/data-drains/route.ts Outdated
@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/bozeman-v2 branch from 4f02445 to 4e3cfa1 Compare May 6, 2026 06:38
Catch Postgres 23505 on insert/update so concurrent name conflicts
return a clean 409 instead of a 500. The data_drains_org_name_unique
index already prevents duplicate rows; this just improves the UX.
@waleedlatif1 waleedlatif1 force-pushed the waleedlatif1/bozeman-v2 branch from 4e3cfa1 to 331a52f Compare May 6, 2026 06:40
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 331a52f. Configure here.

@waleedlatif1 waleedlatif1 merged commit ae87481 into staging May 6, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/bozeman-v2 branch May 6, 2026 06:45
waleedlatif1 added a commit that referenced this pull request May 7, 2026
…4471)

Catch Postgres 23505 on insert/update so concurrent name conflicts
return a clean 409 instead of a 500. The data_drains_org_name_unique
index already prevents duplicate rows; this just improves the UX.
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.

1 participant