fix(db): prevent duplicate member segment affiliations#3984
fix(db): prevent duplicate member segment affiliations#3984prembharne wants to merge 1 commit intolinuxfoundation:mainfrom
Conversation
Adds a pre-insert SELECT check in TypeScript and a concrete UNIQUE index migration on memberSegmentAffiliations robustly resolving duplicate affiliation logging problems.
|
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. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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) |
There was a problem hiding this comment.
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.


Fixes #2840
Description:
This PR introduces a strict deduplication migration for the
memberSegmentAffiliationstable and enforces aUNIQUEindex that handlesNULLdate values natively usingCOALESCE.Additionally, we added a robust pre-insert
SELECTcheck inmemberSegmentAffiliationRepository.ts'screateOrUpdatefunction. This prevents unhandled 500 Constraint Errors and updates seamlessly when exact matches are triggered, ensuring consistency across segments.Changes:
.sqlcleanup and indexing migration.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 lowestid) and then adds a unique index that treatsNULLorganizationId/dateStart/dateEndas equal viaCOALESCEto prevent future duplicates.Updates
MemberSegmentAffiliationRepository.createOrUpdateto firstSELECTan exact-match affiliation usingIS NOT DISTINCT FROM(NULL-safe equality) and, if found, reuse it (while still callingupdateAffiliation) 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.