Skip to content

fix(db): prevent duplicate member segment affiliations#3984

Closed
prembharne wants to merge 1 commit intolinuxfoundation:mainfrom
prembharne:main
Closed

fix(db): prevent duplicate member segment affiliations#3984
prembharne wants to merge 1 commit intolinuxfoundation:mainfrom
prembharne:main

Conversation

@prembharne
Copy link
Copy Markdown

@prembharne prembharne commented Mar 31, 2026

Fixes #2840

Description:
This PR introduces a strict deduplication migration for the memberSegmentAffiliations table and enforces a UNIQUE index that handles NULL date values natively using COALESCE.

Additionally, we added a robust pre-insert SELECT check in memberSegmentAffiliationRepository.ts's createOrUpdate function. This prevents unhandled 500 Constraint Errors and updates seamlessly when exact matches are triggered, ensuring consistency across segments.

Changes:

  • Added .sql cleanup and indexing migration.
  • Graceful TypeScript fail-safes in backend integration preventing blind inserts.

Note

Medium Risk
Adds a data-cleanup migration and a new unique index on an existing table, which can impact large datasets and fail if assumptions about NULL/default coercion don’t hold. Runtime changes are small but affect write-path behavior in createOrUpdate.

Overview
Introduces a migration that removes exact duplicate rows in memberSegmentAffiliations (keeping the lowest id) and then adds a unique index that treats NULL organizationId/dateStart/dateEnd as equal via COALESCE to prevent future duplicates.

Updates MemberSegmentAffiliationRepository.createOrUpdate to first SELECT an exact-match affiliation using IS NOT DISTINCT FROM (NULL-safe equality) and, if found, reuse it (while still calling updateAffiliation) instead of blindly inserting and risking a uniqueness/constraint error.

Written by Cursor Bugbot for commit cf6d9bd. This will update automatically on new commits. Configure here.

Adds a pre-insert SELECT check in TypeScript and a concrete UNIQUE index migration on memberSegmentAffiliations robustly resolving duplicate affiliation logging problems.
@prembharne prembharne closed this Mar 31, 2026
@prembharne prembharne reopened this Mar 31, 2026
@prembharne prembharne closed this Mar 31, 2026
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


GSoC DB Fixer seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

"segmentId",
COALESCE("organizationId", '00000000-0000-0000-0000-000000000000'::uuid),
COALESCE("dateStart", '1970-01-01T00:00:00Z'::timestamp),
COALESCE("dateEnd", '1970-01-01T00:00:00Z'::timestamp)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Timestamp type mismatch will cause migration to fail

High Severity

The dateStart and dateEnd columns are TIMESTAMP WITH TIME ZONE (per migration V1691658076), but the COALESCE sentinel values use ::timestamp (without time zone). Mixing timestamptz and timestamp in COALESCE triggers an implicit cast that PostgreSQL considers non-immutable (it depends on session timezone). Since expression indexes require all functions to be IMMUTABLE, the CREATE UNIQUE INDEX statement will fail at runtime, blocking the migration entirely. The cast needs to be ::timestamptz instead.

Fix in Cursor Fix in Web

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.

membersegmentaffiliations allows duplicated affiliations

2 participants