Keep a split transaction's activity label on its parts

JA
5h ago
+368-15

Closes #5498

Fork pre-flight: jaysbeekay/sure#856, same branch (up/investment-totals-split-income), same commit (a2537ea19). Two commits on main 00d177cb0, then a merge of main 6aa8cf218. The merge resolves a conflict with #5377 in InvestmentFlowStatement#period_totals by keeping both filters (split parents left out, plus #5377's account_id filter); db/schema.rb takes main's later version, and the backfill migration keeps its 20261119090000 timestamp.

Full unit suite on a2537ea19: 18,151 runs, 0 failures; the 8 errors are ReleaseNotesWorkflowTest, which needs node, missing in the test image.

Problem

Splitting a Transaction labelled Dividend or Interest removed it from the investment totals. Entry#split! excluded the parent and gave the parts no investment_activity_label, and InvestmentStatement::Totals reads only non-excluded, labelled rows, so it counted neither. The mirror image hit the income statement: a split Sweep In, Sweep Out, Transfer or Exchange lost its label on the parts, so they counted as income or spending.

What changed

Option B from the issue's Triage Plan, plus the backfill (owner, 2026-11-16).

  • app/models/entry.rb #split!: each part gets investment_activity_label: entryable.try(:investment_activity_label) beside kind:. A part can still be relabelled afterwards.
  • db/migrate/20261119090000_backfill_split_part_activity_labels.rb: data migration, no schema change. Gives each existing unlabelled part its Transaction parent's label, unless the part's label is in locked_attributes. down is a no-op. db/schema.rb version bumped.
  • app/models/investment_flow_statement.rb #period_totals: leaves split parents out (Entry.excluding_split_parents). Not in the issue's plan; found while checking the blast radius. This reader doesn't filter excluded entries, so it counted a split Contribution's parent and skipped its unlabelled parts. With the parts now labelled it would count the money twice. Leaving split parents out keeps every unsplit entry as before; whether it should skip excluded entries generally is still out of scope, as the issue says.

Tests

TestBefore the fix
InvestmentStatementTest "a split Dividend transaction still counts in dividends"dividends 0 after the split
"a split Interest transaction still counts in interest"interest 0
"a split part relabelled away from Dividend leaves dividends"0, expected 60
"a split unlabelled transaction stays out of investment income"boundary, passes both ways
IncomeStatementTest "a split Sweep In transaction stays out of income"Expected: 1000, Actual: 1500
EntrySplitTest "split! gives each part the parent's activity label"Actual: [nil, nil]
EntrySplitTest "split! leaves the parts unlabelled when the parent has no activity label"boundary
InvestmentFlowStatementTest "a split contribution counts once"200 with the label copy and no parent guard
BackfillSplitPartActivityLabelsMigrationTest: backfills, keeps a part's own label, leaves an unlabelled parent's parts, leaves a locked labelfail without the backfill / lock guard

Mutations, one at a time:

  • Drop the label from split!: 5 failures (entry split, both totals tests, relabel, Sweep In).
  • Drop the split-parent guard in InvestmentFlowStatement: "a split contribution counts once" fails.
  • Skip the backfill: 2 migration tests fail.
  • Drop the lock guard: "leaves a part whose label the user locked" fails.

Runs: targeted suites (entry split, investment/income/flow statements, income statement folder, migration, splits controllers web and API, data importer, QIF import, rule split action, flow classifier, reports controller): 715 runs, 0 failures. Full unit suite: 17,546 runs, 0 failures; the 8 errors are ReleaseNotesWorkflowTest (no node in the test image). RuboCop: no offences. Brakeman: 0 warnings.

Blast radius

  • split! callers: split_once!, resplit! (web and API splits controllers), Rule::ActionExecutor::SplitTransaction, Family::DataImporter#import_split_lines!, QifImport. None passes a label, so each part takes the parent's. The data import reads no labels, so an imported split's parts are unlabelled, as before.
  • Label readers: InvestmentStatement::Totals and Portfolio::FlowClassifier skip excluded entries and now see the parts. IncomeStatement::Totals and DailyExpenseTotals skip excluded rows and now drop split internal movements. InvestmentFlowStatement is guarded as above. Trade-label readers (Holding, ReportsController, TradesController) are unaffected, since parts are always Transactions.

Overlap check (upstream, any author)

  • we-promise/sure#5070 and #5107 (Totals rewrite onto FlowClassifier) still filter entries.excluded = false and add no parent fallback or label copy: distinct. Whichever lands first, the other rebases; with this change the parts are labelled, so the rewrite needs no fallback.
  • we-promise/sure#3506 (draft) moves synced income to Trades: distinct, and manual or other-provider Transactions stay affected.
  • we-promise/sure#4467, #3428, #3728, #4966 and the merged #5134 touch split! without copying the label. #4467 edits the same Transaction.new block, so a textual conflict is possible.

Migration note

Data-only migration on transactions, updating unlabelled split parts in one statement. Safe to re-run; its down is a no-op.


Summary by cubic

Fixes #5498: splitting a labeled transaction no longer drops Dividend or Interest from investment totals, and no longer counts internal movements as income or spending. Split parts inherit the parent's activity label and stay relabelable.

  • Backfills existing unlabeled parts from their parent unless the part's label is locked; touched entry timestamps force cached totals to rebuild.
  • Excludes split parents from investment flow totals and lets a split of a confirmed transfer leg follow the counterpart rule: parts of a confirmed investment-to-investment transfer stay out of contributions, a depository-to-investment transfer still counts once, and with a pending match the part counts as before. Only standard, investment_contribution, and funds_movement parts are counted, scoped to confirmed legs in the family.

Summary by CodeRabbit

  • Bug Fixes
    • Split investment transactions now retain their activity labels on each part, keeping investment income and contribution totals accurate.
    • Investment flow totals correctly account for eligible split parts in confirmed transfers without double-counting the original transaction.
    • Existing split parts with missing labels are updated to match their parent when appropriate, while preserving manually set labels.
    • Split transaction exports include inherited investment activity labels.