Skip to content

fix(api): only reverse the merge that is in effect - #522

Merged
setkyar merged 1 commit into
mainfrom
fix/unmerge-active-merge-event
Nov 23, 2026
Merged

setkyar merged 1 commit into
mainfrom
fix/unmerge-active-merge-event

Conversation

@setkyar

@setkyar setkyar commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #378.

Problem

unmergeContacts decided whether a merge was still in effect by checking that the source contact still pointed at the event's target. If you merge A→B, unmerge, and merge A→B again, there are two events with the same target. The older one has already been reversed, but it still passes that check. Undoing it revives A and orphans the newer merge, which then can never be reversed.

Fix

  • The contact records which merge is in effect: new column contacts.active_merge_event_id. It is set on merge and cleared on unmerge.
  • Unmerge checks that pointer under a row lock. An older, superseded event is rejected.
  • Merge history reports whether a merge can be reversed from the same pointer.
  • Purges that separate aliases clear the pointer. The foreign key is ON DELETE SET NULL, so a purged event can never be reversed.

Migration

Migration 118 (118_track_active_merge_event.ts) adds the column, foreign key and index for every existing tenant. It is additive. It backfills the newest matching event for contacts that are still merged. New tenants get the same columns through tenant-schema reconciliation.

It was written as 112, but main used 112–116 in the meantime and the #506–#509 stack uses 117, so it was renumbered to 118. If this merges before that stack, it still runs correctly because migrations run in file-name order.

Tests

  • Extended unmergeContacts integration case: after merge → unmerge → merge of the same pair, merge history reports only the newest event as reversible, reversing either older event is rejected with ValidationError and changes nothing (the source stays merged, the endpoint stays put), and the newest merge can still be reversed.
  • Rebased on main. Typecheck, biome, db:migrate on a fresh Postgres 18 and contact-merge.service.integration.test.ts all pass.

Release

Contains a migration: take a managed-Postgres backup before release. worker: skip.

unmergeContacts decided whether a merge was still in effect by checking
that the source still pointed at the event's target. Merging, unmerging
and merging the same pair again leaves two events with one target, so
the older, already-reversed event passed the check: undoing it revived
the source and orphaned the newer merge, which could then never be
reversed.

Record the event in effect on the contact (active_merge_event_id, set on
merge and cleared on unmerge), check it under a row lock, and report
reversibility in the merge history from the same pointer. Purges that
separate aliases clear it too, and the foreign key is ON DELETE SET
NULL, so a purged event can never be reversed.

Migration 118 adds the column, key and index to existing tenants and
backfills the newest matching event for contacts that are still merged;
new tenants get the same through tenant-schema reconciliation.

Fixes #378
@setkyar
setkyar force-pushed the fix/unmerge-active-merge-event branch from 17d7763 to fb555f6 Compare November 23, 2026 18:36
@setkyar
setkyar merged commit fc6d96c into main Nov 23, 2026
15 of 17 checks passed
@setkyar
setkyar deleted the fix/unmerge-active-merge-event branch November 23, 2026 18:55
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.

[Detail Bug] Contacts: Unmerging a stale merge event succeeds after re-merging the same source/target pair, orphaning the newer merge event

1 participant