Repository navigation
fix(api): only reverse the merge that is in effect - #522
Merged
Merged
Conversation
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
force-pushed
the
fix/unmerge-active-merge-event
branch
from
November 23, 2026 18:36
17d7763 to
fb555f6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #378.
Problem
unmergeContactsdecided 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
contacts.active_merge_event_id. It is set on merge and cleared on unmerge.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
mainused 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
unmergeContactsintegration case: after merge → unmerge → merge of the same pair, merge history reports only the newest event as reversible, reversing either older event is rejected withValidationErrorand changes nothing (the source stays merged, the endpoint stays put), and the newest merge can still be reversed.main. Typecheck, biome,db:migrateon a fresh Postgres 18 andcontact-merge.service.integration.test.tsall pass.Release
Contains a migration: take a managed-Postgres backup before release.
worker: skip.