The personal finance app for everyone (by everyone)
Ruby
Select manual-account holdings in one pass, not per row (#4156)
wps2606bc278b07ea0
diff --git a/app/models/holding/current_for_investment_accounts.rb b/app/models/holding/current_for_investment_accounts.rbindex 8e0ae533c3..bc98343acd 100644--- a/app/models/holding/current_for_investment_accounts.rb+++ b/app/models/holding/current_for_investment_accounts.rb@@ -4,7 +4,8 @@ class Holding::CurrentForInvestmentAccounts# Correlated, it rescans the account's provider holdings per row, which is# quadratic in the number of provider rows an account keeps. The grouped# subquery is limited to the requested accounts so its cost follows them,- # not the whole table.+ # not the whole table. Accounts without provider holdings likewise take each+ # security's latest row in one DISTINCT ON pass, not one lookup per row.CURRENT_HOLDINGS_SQL = <<~SQL.squish.freeze(holdings.account_provider_id IS NOT NULL@@ -15,27 +16,20 @@ class Holding::CurrentForInvestmentAccountsAND provider_holdings.account_provider_id IS NOT NULLGROUP BY provider_holdings.account_id)- ) OR (- NOT EXISTS (- SELECT 1- FROM holdings provider_holdings- WHERE provider_holdings.account_id = holdings.account_id- AND provider_holdings.account_provider_id IS NOT NULL- )- AND holdings.currency = (- SELECT accounts.currency- FROM accounts- WHERE accounts.id = holdings.account_id- )- AND holdings.id = (- SELECT latest_holdings.id- FROM holdings latest_holdings- WHERE latest_holdings.account_id = holdings.account_id- AND latest_holdings.security_id = holdings.security_id- AND latest_holdings.currency = holdings.currency- ORDER BY latest_holdings.date DESC- LIMIT 1- )+ ) OR holdings.id IN (+ SELECT DISTINCT ON (latest_holdings.account_id, latest_holdings.security_id) latest_holdings.id+ FROM holdings latest_holdings+ INNER JOIN accounts ON accounts.id = latest_holdings.account_id+ WHERE latest_holdings.account_id IN (:account_ids)+ AND accounts.id IN (:account_ids)+ AND latest_holdings.currency = accounts.currency+ AND latest_holdings.account_id NOT IN (+ SELECT provider_holdings.account_id+ FROM holdings provider_holdings+ WHERE provider_holdings.account_id IN (:account_ids)+ AND provider_holdings.account_provider_id IS NOT NULL+ )+ ORDER BY latest_holdings.account_id, latest_holdings.security_id, latest_holdings.date DESC)SQLdiff --git a/test/models/holding/current_for_investment_accounts_test.rb b/test/models/holding/current_for_investment_accounts_test.rbindex 4d3c811a55..02e857d64d 100644--- a/test/models/holding/current_for_investment_accounts_test.rb+++ b/test/models/holding/current_for_investment_accounts_test.rb@@ -67,24 +67,85 @@ class Holding::CurrentForInvestmentAccountsTest < ActiveSupport::TestCaseassert_equal [ current_first.id, current_second.id ].sort, relation.pluck(:id).sortend+ test "selects provider and manual holdings independently when queried together" do+ coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")+ coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")+ account_provider = AccountProvider.create!(account: @account, provider: coinstats_account)+ provider_holding = create_holding("AAPL", date: Date.current, account_provider: account_provider)++ manual_account = create_manual_account+ security = Security.create!(ticker: "MSFT", name: "MSFT")+ create_holding_for_security(security, date: 1.day.ago.to_date, account: manual_account)+ manual_latest = create_holding_for_security(security, date: Date.current, account: manual_account)++ assert_equal [ provider_holding.id, manual_latest.id ].sort,+ described_query([ @account.id, manual_account.id ]).pluck(:id).sort+ end++ test "excludes foreign-currency holdings for a manual account" do+ security = Security.create!(ticker: "AAPL", name: "Apple")+ usd_holding = create_holding_for_security(security, date: 1.day.ago.to_date)+ create_holding_for_security(security, date: Date.current, currency: "EUR")++ assert_equal [ usd_holding.id ], described_query.pluck(:id)+ end++ test "omits a security whose latest holding has zero quantity" do+ security = Security.create!(ticker: "AAPL", name: "Apple")+ create_holding_for_security(security, date: 1.day.ago.to_date, qty: 5)+ create_holding_for_security(security, date: Date.current, qty: 0)++ assert_empty described_query.pluck(:id)+ end++ test "a linked account's calculated rows neither move nor join its latest provider day" do+ coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")+ account_provider = AccountProvider.create!(+ account: @account,+ provider: coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")+ )++ provider_holding = create_holding("AAPL", date: 1.day.ago.to_date, account_provider: account_provider)+ provider_holding.update_columns(updated_at: 1.day.ago)++ # Reverse-calculated history, rewritten today and on the import day respectively.+ create_holding("OLD1", date: 3.days.ago.to_date)+ create_holding("OLD2", date: 3.days.ago.to_date).update_columns(updated_at: 1.day.ago)++ assert_equal [ provider_holding.id ], described_query.pluck(:id)+ end+private- def described_query- Holding::CurrentForInvestmentAccounts.new([ @account.id ]).relation+ # Defaults to the setup account.+ def described_query(account_ids = [ @account.id ])+ Holding::CurrentForInvestmentAccounts.new(account_ids).relationend+ # A USD investment account with no provider link.+ def create_manual_account+ @family.accounts.create!(+ name: "Manual investment",+ balance: 1000,+ currency: "USD",+ accountable: Investment.new+ )+ end++ # Creates a new security for ticker, then a holding of it.def create_holding(ticker, date:, account_provider: nil)security = Security.create!(ticker: ticker, name: ticker)create_holding_for_security(security, date: date, account_provider: account_provider)end- def create_holding_for_security(security, date:, qty: 1, account_provider: nil)- @account.holdings.create!(+ # Holding priced at 100, so amount = qty * 100.+ def create_holding_for_security(security, date:, qty: 1, currency: "USD", account: @account, account_provider: nil)+ account.holdings.create!(security: security,date: date,qty: qty,price: 100,amount: qty * 100,- currency: "USD",+ currency: currency,account_provider: account_provider)end