Skip to content

fix: undo the merge in effect when unmerging - #5507

Open
themarolt wants to merge 2 commits into
mainfrom
fix/issue-1288-merge-backup-action
Open

themarolt wants to merge 2 commits into
mainfrom
fix/issue-1288-merge-backup-action

Conversation

@themarolt

@themarolt themarolt commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When an organization or member is unmerged, findMergeBackup picks the merge action to undo. It had no ORDER BY, so when several merge actions of the primary hold the identity, it restored from whichever row Postgres returned first. It now prefers a finished merge and, among those, the latest.

Changes

  • ORDER BY (ma.state = 'merged') DESC, ma."updatedAt" DESC in the three findMergeBackup queries: organization and member branches in backend/.../mergeActionsRepository.ts, and the member query in data-access-layer/src/mergeActions/repo.ts.
  • An organization unmerge writes a new action row for the restored organization and leaves the original merge action merged, with its backup. After merge → unmerge → re-merge, both actions match; the latest is the merge in effect. Before, the older one could be picked, restoring stale data and passing the old secondaryId to restoreAfterUnmerge, so the restored organization did not get its slugs back.
  • A member unmerge restores the secondary with its old id, so re-merging that pair reuses its action row: createdAt stays, updatedAt is refreshed by the upsert in addMergeAction. That is why the order uses updatedAt.
  • services/libs/data-access-layer/src/mergeActions/repo.test.ts: four cases on the member query (latest of two merges, a pair re-merged after an unmerge, merged over a newer error, error as fallback). The re-merge case fails with createdAt ordering. The organization query is in the Sequelize repository in backend/, which has no Postgres test harness; it uses the same clause.
  • error actions still count, but only when no merged one matches. Some of them did merge (13 organization actions are error at step merge-done), so filtering them out would stop those from being reverted.

Production, read-only: 11,836 (primary, identity) pairs match more than one organization merge action. 6 are merge → unmerge → re-merge and 65 mix a merged and an error action; this picks the right one for both. The rest are several merged-away organizations that shared the identity (e.g. a domain); those now resolve to the newest merge instead of an arbitrary one, and choosing between them stays open on the issue. EXPLAIN on production: same primaryId index scan, plus a sort of the few matching rows.

Type of change

  • Bug fix

Related issue

Refs linuxfoundation/lfx-cdp-insights-ops#1288

@themarolt
themarolt requested review from a team and lfx-one as code owners December 3, 2026 09:58
Copilot AI balanced review requested due to automatic review settings December 3, 2026 09:58
@cursor

cursor Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes unmerge/restore selection for member and org merges across backend and DAL; incorrect behavior previously caused wrong backups, but ordering logic affects production data restoration paths.

Overview
findMergeBackup used to return an arbitrary matching mergeActions row when several backups for the same primary held the same identity, which could undo the wrong merge (e.g. stale org data after merge → unmerge → re-merge).

Both member and org lookup SQL in mergeActionsRepository, plus the DAL findMergeBackup query, now ORDER BY (state = 'merged') DESC, "updatedAt" DESC so the first row prefers a finished merge and, among those, the latest (including when the same pair is merged again). Failed/error actions are only chosen when no merged row matches.

New repo.test.ts integration cases cover latest-of-multiple-merges, re-merge after unmerge, merged-over-error preference, and error-only fallback.

Reviewed by Cursor Bugbot for commit 11d73b3. Bugbot is set up for automated code reviews on this repo. Configure here.

@lfx-one

lfx-one commented Dec 3, 2026 •

Copy link
Copy Markdown

@themarolt I'm starting review 1 of this pull request now (started 2026-12-03 09:58 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Hi @themarolt, thanks for the clear write-up and the production numbers.

Summary

This pull request adds a deterministic ORDER BY to the three findMergeBackup queries, so an unmerge undoes the newest finished merge instead of an arbitrary row. The change is small and correct. The main concern is that no spec covers it.

Decision

✅ Approved with minor comments

🟡 1 minor

📝 Documentation reminder: changed findMergeBackup unmerge backup selection. Please check product pages and external docs; the author and maintainers own those updates.

Details

Overall impression: The ORDER BY (ma.state = 'merged') DESC, ma."createdAt" DESC clause is applied the same way in the organization and member queries in backend/src/database/repositories/mergeActionsRepository.ts and in the member query in services/libs/data-access-layer/src/mergeActions/repo.ts. Putting merged first and the newest first matches the merge → unmerge → re-merge case and keeps error rows as a fallback. The added SQL comments are 2 lines each, within the limit in AGENTS.md.

🟡 Minor: 1 issue: No spec covers the new backup ordering

Other reviewers and CI checks: Cursor Bugbot posted a summary only, with no findings to reconcile. The required DCO check passed on 18fa4b0.

Documentation reminder: changed findMergeBackup unmerge backup selection. Please check product pages and external docs; the author and maintainers own those updates.

Rule files: read AGENTS.md, CONTRIBUTING.md, .claude/rules/commit-workflow.md; skipped .claude/rules/adr-format.md and .claude/rules/skill-guidance.md (not relevant to this change, not read)

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

lfx-one
lfx-one previously approved these changes Dec 3, 2026

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The ordering change is correct and consistent across all three findMergeBackup queries. One minor gap: the bug fix has no spec.

Documentation

This pull request changes behavior that may also be described outside this repository, such as product pages or an external docs site. I can't read those, so please check them; the author and maintainers own those updates. Changed here:

  • findMergeBackup unmerge backup selection (behavior now prefers the newest merged action)

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; the remaining organization test coverage is non-blocking.

Summary

findMergeBackup now uses updatedAt to choose the latest matching merge while still preferring finished merges.

  • Unmerge restores from the latest completed matching merge.

No new issues found. Tests were not run.

Reviews (2) · Last reviewed commit: "fix: pick the latest merge when a pair i..." · Reviewed by Greptile

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical ordering bug can select an older backup after members are re-merged because the reused merge-action row retains its original timestamp, also affecting eligibility checks.

1 open issue
What changed in this PR

Makes unmerge backup selection deterministic for members and organizations, aiming to avoid restoring stale data after re-merges.

Changes:

  • Prioritizes completed merges over other matching actions.
  • Orders matching actions by creation time, newest first, across all three lookups.
File Description
services/​libs/​data-access-layer/​src/​mergeActions/​repo.ts Adds ordering to the shared member backup lookup.
backend/​src/​database/​repositories/​mergeActionsRepository.ts Adds matching ordering to backend member and organization lookups.

Review effort: Balanced

A member unmerge restores the secondary with its old id, so merging that
pair again reuses the merge action row and only refreshes updatedAt. Order
by updatedAt and cover the selection with tests.

Refs linuxfoundation/lfx-cdp-insights-ops#1288

Signed-off-by: Uroš Marolt <uros@marolt.me>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 10:13
@lfx-one

lfx-one commented Dec 3, 2026 •

Copy link
Copy Markdown

@themarolt I'm starting review 2 of this pull request now (started 2026-12-03 10:13 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Hi Uros Marolt, thanks for the quick follow-up: you fixed the earlier feedback in one commit.

Summary

This round moves the findMergeBackup ordering from createdAt to updatedAt in all three queries, so a pair that is merged again after an unmerge counts as the latest merge. It also adds a Postgres-backed spec with four member cases. I found no new issues.

Decision

✅ Approved

Details

Overall impression: The change is small and well targeted. addMergeAction upserts on (tenantId, type, primaryId, secondaryId) and sets updatedAt = now(), so a re-merge of the same pair keeps createdAt and refreshes updatedAt. Ordering by (ma.state = 'merged') DESC, ma."updatedAt" DESC therefore picks the merge in effect, and error rows stay a fallback. The new SQL comments are 2 lines each, within the limit in AGENTS.md. The organization query has no spec, because the backend has no Postgres test harness; it uses the same clause as the tested member query, and the PR description says so.

👏 Nice work: You traced the Copilot finding to the upsert in addMergeAction, then added a re-merge test that fails with createdAt ordering and passes with updatedAt. You also applied the fix to the backend member branch that the finding mentioned.

  • ✅ Resolved: "No spec covers the new backup ordering" (minor). services/libs/data-access-layer/src/mergeActions/repo.test.ts covers the newer merged action, a pair re-merged after an unmerge, merged over a newer error, and error as the fallback. Fixed in 11d73b3.

Other reviewers and CI checks: Copilot's "re-merge reuses a stale merge action backup" finding is fixed by the updatedAt ordering, and the author replied on that thread. Greptile's test-coverage finding is covered by the new spec; the organization lookup has no direct spec, as noted above. The required DCO check passed on 11d73b3.

Rule files: read AGENTS.md, CONTRIBUTING.md; skipped .claude/rules/adr-format.md, .claude/rules/commit-workflow.md and .claude/rules/skill-guidance.md (not relevant to this change, not read)

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new commit switches all three findMergeBackup queries to updatedAt ordering, which matches how addMergeAction re-merges a pair (the upsert refreshes updatedAt and keeps createdAt). It also adds repo.test.ts with four cases on the member query. I found no new issues.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Domain-owner validation is needed because newest-backup precedence for organizations sharing an identity remains unresolved and controls restored attributes, affiliations, and slug ownership.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

3 participants