Merge duplicate securities with Security::Merger in both dedupe tasks

JA
5h ago
+454-127

Closes jaysbeekay/sure#863

Upstream PR: we-promise/sure#5618 (same branch, same commit)

Independent, against mirror/upstream-main. Builds on we-promise/sure#5436's Security::Merger and includes a small fix to it (below). we-promise/sure#4333 can drop its rake-task hunk (moving splits in these tasks), since the merger already moves them.

Problem

securities:deduplicate and data_migration:migrate_duplicate_securities re-pointed a duplicate's holdings, trades and prices by hand, then destroyed it. Its holding lots were not moved, so the foreign key's cascade deleted them: a cost basis set by hand and per-lot edits were lost. Other lots whose origin_security_id was the duplicate were nulled.

Change

  • Both tasks merge each group with Security::Merger.merge_group!, which runs merge! for every duplicate in one transaction, so a group merges whole or not at all; the no-MIC pass hands it one duplicate at a time. Each task collects the accounts its merges touched and syncs each one once, after its last merge, with Security::Merger.sync_accounts: a sync queued between merges could be absorbed by one already running, since sync_later reuses a visible sync and expand_window_if_needed only changes a pending one. A dispatch that raises is logged and the remaining accounts are still synced.
  • merge! moves holdings, trades, lots (and other lots' origin_security_id), prices, splits and tags, and deletes the duplicate. It now records counts (holdings moved and removed, trades, lots, re-pointed references) and the accounts it touched, and takes sync: false for a caller that syncs them itself.
  • The tasks print the counts. The dry run also counts lots and the rows that point at a duplicate (lot origins, remapped holdings' provider security).
  • Small fix to we-promise/sure#5436's merger: it copied a missing name onto the target with update! while the source still existed, so the case-insensitive ticker validation failed for every same-ticker-and-MIC group with an unnamed keeper. The name is now copied after the source is deleted, without re-validating, since another duplicate in the group can still exist.
  • Security#move_taggings_to!, used only by the task, is removed with its unit test; merger tests now cover other families' tags and a tag the target already carries.

Behaviour changes

  • A colliding holding keeps the more recently written row, and the task output says so. The no-MIC path used to fail on the unique index; the data-migration path deleted the duplicate's row.
  • Prices and splits the canonical already has win.
  • Affected accounts are synced once per task, after every merge has landed; a failed dispatch for one account is logged and does not stop the others, and a task stopped partway (an interrupt, or an error outside the per-group rescue) still syncs the merges that committed.

Validation

  • Red first: lots deleted, a collision aborting the merge, no sync, no lot count in the dry run, and an unnamed keeper failing validation.
  • Mutation per changed line, each caught.
  • Full suite on e48448a3e (upstream main merged in): 19,796 runs, 0 failures; 8 errors, all ReleaseNotesWorkflowTest needing node, which the test image lacks.
  • The migration task test loads every rake task, as the other data_migration task tests do, not just data_migration.rake through RakeTaskTestHelper. Loading that file alone left clear_stale_contribution_categories_test.rb without an environment task when it ran second (5 errors), and a later load_tasks would have defined the file's tasks twice (its batch test then failed under --seed 1). Checked with seeds 1–5 against both tests and rules_rake_test.rb.
  • rubocop clean on the Ruby and rake files; checked against upstream AGENTS.md.

Merge note (2026-11-29): this PR and we-promise/sure#4333 both add tests at the same place in test/models/security/merger_test.rb, so the second to land on upstream main conflicts there. Whoever resolves it must keep both sets of tests:

  • from we-promise/sure#4333: "a merge keeps the source's manual split over the target's provider split, and logs the dropped terms", "a split whose source is not listed still moves onto the target", "a split moved onto the target rebuilds an account that only held the target"
  • from we-promise/sure#5618: "counts what it moved and can leave the syncs to its caller", "a keeper with no name takes the duplicate's while another duplicate remains", "a group merges whole or not at all, and leaves the syncs to its caller", "a group keeps the most recently written holding when two sources collide", "moved and repointed holdings keep their original write time", "keeps every family's tags, and one tagging where the target already has the tag"

The edge build keeps both. On that tree the merger and both dedupe task tests pass (32 runs, 0 failures).