The personal finance app for everyone (by everyone)
Ruby
Group the latest provider import day once in current holdings (#4118)
mrvrujcbc754556580
diff --git a/app/models/holding/current_for_investment_accounts.rb b/app/models/holding/current_for_investment_accounts.rbindex ee96168e20..8e0ae533c3 100644--- a/app/models/holding/current_for_investment_accounts.rb+++ b/app/models/holding/current_for_investment_accounts.rb@@ -1,12 +1,19 @@class Holding::CurrentForInvestmentAccounts+ # Each requested account's latest provider import day is computed once,+ # grouped by account, rather than as a subquery correlated to every row.+ # 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.CURRENT_HOLDINGS_SQL = <<~SQL.squish.freeze(holdings.account_provider_id IS NOT NULL- AND holdings.updated_at::date = (- SELECT MAX(provider_holdings.updated_at::date)+ AND (holdings.account_id, holdings.updated_at::date) IN (+ SELECT provider_holdings.account_id, MAX(provider_holdings.updated_at::date)FROM holdings provider_holdings- WHERE provider_holdings.account_id = holdings.account_id+ WHERE provider_holdings.account_id IN (:account_ids)AND provider_holdings.account_provider_id IS NOT NULL+ GROUP BY provider_holdings.account_id)) OR (NOT EXISTS (@@ -42,7 +49,7 @@ class Holding::CurrentForInvestmentAccountsHolding.where(account_id: account_ids).where.not(qty: 0)- .where(Arel.sql(CURRENT_HOLDINGS_SQL))+ .where(CURRENT_HOLDINGS_SQL, account_ids: account_ids)endprivatediff --git a/test/models/holding/current_for_investment_accounts_test.rb b/test/models/holding/current_for_investment_accounts_test.rbindex 6e93c063b3..4d3c811a55 100644--- a/test/models/holding/current_for_investment_accounts_test.rb+++ b/test/models/holding/current_for_investment_accounts_test.rb@@ -33,6 +33,40 @@ class Holding::CurrentForInvestmentAccountsTest < ActiveSupport::TestCaseassert_equal [ latest.id ], described_query.pluck(:id)end+ test "resolves the latest provider import day per account" do+ other_account = @family.accounts.create!(+ name: "Second investment",+ balance: 100,+ currency: "USD",+ accountable: Investment.new+ )+ coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")+ first_provider = AccountProvider.create!(+ account: @account,+ provider: coinstats_item.coinstats_accounts.create!(name: "First", currency: "USD")+ )+ second_provider = AccountProvider.create!(+ account: other_account,+ provider: coinstats_item.coinstats_accounts.create!(name: "Second", currency: "USD")+ )++ current_first = create_holding("AAPL", date: Date.current, account_provider: first_provider)+ stale_first = create_holding("STALE", date: 1.day.ago.to_date, account_provider: first_provider)+ stale_first.update_columns(updated_at: 1.day.ago)++ # The second account last imported yesterday; that day is still its latest.+ current_second = other_account.holdings.create!(+ security: Security.create!(ticker: "MSFT", name: "MSFT"),+ date: 1.day.ago.to_date, qty: 1, price: 100, amount: 100, currency: "USD",+ account_provider: second_provider+ )+ current_second.update_columns(updated_at: 1.day.ago)++ relation = Holding::CurrentForInvestmentAccounts.new([ @account.id, other_account.id ]).relation++ assert_equal [ current_first.id, current_second.id ].sort, relation.pluck(:id).sort+ end+privatedef described_queryHolding::CurrentForInvestmentAccounts.new([ @account.id ]).relation