Count every duplicate group in the dedupe summary

JA
4h ago
+511-132

Closes #5548

Stacks on #5618, which rewrites the same group body. Its branch lives on my fork, so this PR targets main and, until #5618 merges, its diff also shows #5618's four commits; only 2847d6b91 belongs to this PR.
Fork pre-flight: jaysbeekay/sure#1036 (pull/1036) (same branch, same commit 2847d6b91).

Problem

rake securities:deduplicate ends with "Total duplicate sets: N", where N comes from the outer duplicates local. The ticker + MIC loop reassigns that local to each group's own duplicates (duplicates = securities[1..]), since a block shares its enclosing scope's locals, so the summary prints the duplicate count of whichever group ran last. "Found N sets" is printed before the loop and is right.

Resulting behaviour

  • The group body uses its own group_duplicates local at the assignment and every reader: the Duplicates line, the holdings, trades, prices, lots and reference counts, and Security::Merger.merge_group!. The outer list keeps its uses in "Found N sets", the each and the summary.
  • The summary shows the number of groups. Nothing else in the output or the merge changes.

Not fixed here

Pre-existing, left for follow-ups:

  • dry_run = args[:dry_run].present? treats "false" as a dry run, so securities:deduplicate[false] changes nothing.
  • The summary leaves out the first pass, duplicates without a MIC: its totals count only the ticker + MIC groups.

Validation

  • Red first on a6f8cbcfc (#5618's head): three groups of 2, 2 and 5 securities in a dry run (invoke("true")) printed Total duplicate sets: 1, the last group processed (pluck with group has no order), and the test expects /^ Total duplicate sets: 3$/.
  • A live run (bare invoke) merges each group into its earliest security: its holding, trade and price re-point and only the others are deleted, and the summary reads 3 sets, 3 holdings, 3 trades, 3 prices.
  • Boundary check, not coverage of the fix: with no groups the summary prints Total duplicate sets: 0, which also passes before the fix, since the loop never runs.
  • Mutations, each caught: the outer local reassigned again; the holdings and trades counts reading the outer list; merge_group! given the outer list; the Duplicates line reading it.
  • Rubocop and Brakeman clean. Full suite on ab631eaad (upstream main merged in): 19,799 runs, 0 failures, 8 errors, all ReleaseNotesWorkflowTest needing node, which the test image lacks.

Summary by cubic

Both duplicate-security tasks now merge each group through Security::Merger in one transaction, and the dedupe summary counts every ticker + MIC group instead of echoing the last group's duplicate count.

  • Each group merges whole or not at all: holdings, lots, splits, tags, trades, and prices move with the duplicate, which is then deleted; previously a duplicate's lots were cascade-deleted.
  • A colliding holding keeps the more recently written row, and moved holdings keep their original write time.
  • Accounts touched by a merge are synced once, after the last group, and also when the task stops partway; failed sync dispatches are logged and captured in the debug log.
  • The task output prints merged counts (holdings moved/removed, trades, lots, references) and the collision rule.
  • Security#move_taggings_to! is removed; its behavior is now covered in the merger tests.
  • The stale dry_run flag parsing is fixed: "false" now runs a live merge instead of a dry run.
  • Merged upstream main, resolving conflicts in lib/tasks/securities.rake and its test.

Closes #5548.

Summary by CodeRabbit

  • Bug Fixes
    • Improved duplicate security cleanup by consolidating related holdings, lots, trades, prices, references, and tags into the retained security.
    • When duplicate securities have conflicting holdings, the most recently updated holding is retained.
    • Affected accounts are refreshed after merges, with repeated refreshes avoided when multiple duplicate groups affect the same account. Refreshes continue independently if one fails and are attempted for completed merges even if cleanup is interrupted.
  • Improvements
    • Cleanup summaries now include counts for lots and references alongside other merged records.
    • Duplicate groups are merged atomically, preventing partial changes within a group.