Skip to content

fix(db): collapse segment memberships per merge group in normalize_contact_emails migration - #593

Open
pcbeingused333 wants to merge 1 commit into
useplunk:nextfrom
pcbeingused333:fix/normalize-contact-emails-segment-memberships
Open

pcbeingused333 wants to merge 1 commit into
useplunk:nextfrom
pcbeingused333:fix/normalize-contact-emails-segment-memberships

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #436

Description

Root cause. In 20260615120000_normalize_contact_emails, the segment-membership dedupe DELETE only removes a duplicate's row when the survivor already holds one for that segment. If two duplicates share a segment the survivor is not in, neither row is deleted, the following UPDATE rewrites both to (survivor, segment) and violates segment_memberships_pkey, so prisma migrate deploy fails and the container does not start. The same DELETE also keeps the survivor's row even when it is exited (exitedAt set) and deletes active duplicate rows, silently dropping the merged contact from the segment.

Fix. Replace the DELETE with one that keeps a single row per (survivor, segment) across the whole merge group (survivor included), preferring an active membership (exitedAt IS NULL), then the oldest enteredAt. The existing reassignment UPDATE is then collision-free. With an empty merge map the join matches nothing, so databases without duplicates are untouched. The SQL is the one @hanamizuki proposed in the issue.

Type of Change

  • feat: New feature (MINOR version bump)
  • fix: Bug fix (PATCH version bump)
  • feat!: Breaking change - new feature (MAJOR version bump)
  • fix!: Breaking change - bug fix (MAJOR version bump)
  • docs: Documentation update (no version bump)
  • chore: Maintenance/dependencies (no version bump)
  • refactor: Code refactoring (no version bump)
  • test: Adding tests (no version bump)
  • perf: Performance improvement (PATCH version bump)

Testing

The migration file was run against PostgreSQL with the reproduction from #436 (3 contacts in one merge group; segA: survivor exited, both duplicates active; segB: survivor absent, both duplicates present). Before the change it aborts with duplicate key value violates unique constraint "segment_memberships_pkey"; after it commits and the survivor holds exactly one active membership in both segA and segB. The repo's own test suite was not run (SQL-only change).

Checklist

  • PR title follows conventional commits format
  • Code builds successfully
  • Tests pass locally
  • Documentation updated (if needed)

This PR was prepared with AI assistance (Claude Code) and submitted by a human contributor who is responsible for it.

…ntact_emails migration

The dedupe DELETE only removed a duplicate's membership when the survivor
already held one, so two duplicates sharing a segment the survivor was not
in both got rewritten to the same (contactId, segmentId) and hit the
primary key, aborting the upgrade. It also kept an exited survivor row
over active duplicate rows, silently dropping the merged contact from the
segment.

Keep one row per (survivor, segment) across the whole group, preferring an
active membership.
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.

Migration 20260615120000 can abort the upgrade or silently drop static-segment memberships

1 participant