Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Delete shadowed calculated holdings one security at a time (#4797)

Yong Cong Sin7c7f78c90298
diff --git a/app/models/holding/materializer.rb b/app/models/holding/materializer.rb
index adaf4c67a7..fa14e45cff 100644
--- a/app/models/holding/materializer.rb
+++ b/app/models/holding/materializer.rb
@@ -23,7 +23,7 @@ class Holding::Materializer
end
# Clean up only calculated holdings that are directly shadowed by a provider snapshot
- # on the same date/security/currency. Historical calculated rows for provider-linked
+ # on the same date and security (in any currency). Historical calculated rows for provider-linked
# securities are still needed to derive sane balance charts between sync snapshots.
cleanup_shadowed_calculated_holdings
@@ -185,20 +185,23 @@ class Holding::Materializer
# for the same security and date. This preserves reverse-calculated history for linked accounts.
# Currency is not part of the match: providers can report a position in its own currency while
# calculated rows are always in the account currency, and keeping both counts the position twice.
+ #
+ # The provider rows are read first and the calculated rows deleted one security at a time.
+ # Matching each calculated row against the account's rows in one correlated query is planned
+ # from statistics that predate the rows this sync just inserted, so on a first sync of a long
+ # history the planner can choose a nested loop over the whole account for every row.
def cleanup_shadowed_calculated_holdings
- deleted_count = account.holdings
- .where(account_provider_id: nil)
- .where(<<~SQL)
- EXISTS (
- SELECT 1
- FROM holdings provider_holdings
- WHERE provider_holdings.account_id = holdings.account_id
- AND provider_holdings.security_id = holdings.security_id
- AND provider_holdings.date = holdings.date
- AND provider_holdings.account_provider_id IS NOT NULL
- )
- SQL
- .delete_all
+ provider_dates = account.holdings
+ .where.not(account_provider_id: nil)
+ .distinct
+ .pluck(:security_id, :date)
+ .group_by(&:first)
+
+ deleted_count = provider_dates.sum do |security_id, rows|
+ account.holdings
+ .where(account_provider_id: nil, security_id: security_id, date: rows.map(&:last))
+ .delete_all
+ end
Rails.logger.info("Cleaned up #{deleted_count} calculated holdings shadowed by provider snapshots") if deleted_count > 0
end
diff --git a/test/models/holding/materializer_test.rb b/test/models/holding/materializer_test.rb
index 87130c6290..557a7da422 100644
--- a/test/models/holding/materializer_test.rb
+++ b/test/models/holding/materializer_test.rb
@@ -300,6 +300,51 @@ class Holding::MaterializerTest < ActiveSupport::TestCase
assert_equal [ account_provider.id ], yesterday_holdings.pluck(:account_provider_id)
end
+ test "cleans up calculated holdings for each provider security only on its own snapshot dates" do
+ [ Date.yesterday, Date.current ].each do |date|
+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: date, rate: 1.2)
+ end
+
+ 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_rows = { @aapl => [ Date.yesterday, Date.current ], @msft => [ Date.current ] }
+ provider_rows.each do |security, dates|
+ dates.each do |date|
+ Holding.create!(
+ account: @account,
+ security: security,
+ qty: 10,
+ price: 200,
+ amount: 2000,
+ currency: "EUR",
+ date: date,
+ account_provider: account_provider
+ )
+ end
+ end
+
+ # Calculated rows from an earlier sync: AAPL and MSFT on both days.
+ [ @aapl, @msft ].product([ Date.yesterday, Date.current ]).each do |security, date|
+ Holding.create!(account: @account, security: security, qty: 10, price: 240, amount: 2400, currency: "USD", date: date)
+ end
+
+ Holding::Materializer.new(@account, strategy: :reverse).materialize_holdings
+
+ provider_rows.each do |security, dates|
+ dates.each do |date|
+ assert_equal [ account_provider.id ], @account.holdings.where(security: security, date: date).pluck(:account_provider_id)
+ end
+ end
+
+ msft_yesterday = @account.holdings.where(security: @msft, date: Date.yesterday)
+ assert_equal [ [ "USD", nil ] ], msft_yesterday.pluck(:currency, :account_provider_id)
+ end
+
test "carries forward provider cost_basis to calculated rows past the provider snapshot date" do
coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")