The personal finance app for everyone (by everyone)
Ruby
Key exchange-rate caches on sub-second rate updates (#5168)
GFR611db4c8760b
diff --git a/app/models/balance_sheet/net_worth_breakdown_series_builder.rb b/app/models/balance_sheet/net_worth_breakdown_series_builder.rbindex f0ac9ea3a2..bd8b6b5904 100644--- a/app/models/balance_sheet/net_worth_breakdown_series_builder.rb+++ b/app/models/balance_sheet/net_worth_breakdown_series_builder.rb@@ -137,7 +137,7 @@ class BalanceSheet::NetWorthBreakdownSeriesBuilderCACHE_VERSION,ExchangeRate::CONVERSION_CACHE_VERSION,# A rate stored later can bring back an account the series left out.- ExchangeRate.maximum(:updated_at)&.to_i,+ ExchangeRate.updated_at_version,user&.id,shares_version,period.start_date,diff --git a/app/models/balance_sheet/net_worth_series_builder.rb b/app/models/balance_sheet/net_worth_series_builder.rbindex f303041918..d0e8f77727 100644--- a/app/models/balance_sheet/net_worth_series_builder.rb+++ b/app/models/balance_sheet/net_worth_series_builder.rb@@ -56,7 +56,7 @@ class BalanceSheet::NetWorthSeriesBuilder"balance_sheet_net_worth_series_historical",ExchangeRate::CONVERSION_CACHE_VERSION,# A rate stored later can bring back an account the series left out.- ExchangeRate.maximum(:updated_at)&.to_i,+ ExchangeRate.updated_at_version,user&.id,shares_version,accounts_digest,diff --git a/app/models/budget/rollover_calculator.rb b/app/models/budget/rollover_calculator.rbindex b014a8f865..720e3b9b1f 100644--- a/app/models/budget/rollover_calculator.rb+++ b/app/models/budget/rollover_calculator.rb@@ -139,7 +139,7 @@ class Budget::RolloverCalculatordigest_of(AccountShare.where(account_id: accounts.select(:id)).select(:id, :account_id, :user_id, :include_in_finances)),family.tax_advantaged_account_ids.sort.join(","),family.entries_cache_version,- ExchangeRate.where(to_currency: family.currency).maximum(:updated_at)&.to_f,+ ExchangeRate.where(to_currency: family.currency).updated_at_version,family.updated_at.to_f].join("|")enddiff --git a/app/models/exchange_rate.rb b/app/models/exchange_rate.rbindex 98f4dd3ba3..ac3bf3e14c 100644--- a/app/models/exchange_rate.rb+++ b/app/models/exchange_rate.rb@@ -24,6 +24,17 @@ class ExchangeRate < ApplicationRecordfalseend+ # Part of the cache key of figures that depend on which rates are stored: a+ # rate stored later can complete a total that left an amount out. Keeps the+ # fractional seconds so a rate stored within the same second as the previous+ # one still changes the key. Works on a scope too+ # (`where(to_currency: ...).updated_at_version`). It is deliberately not named+ # `cache_version`: on a relation that name resolves to Rails' collection+ # version, which also counts every matching row.+ def self.updated_at_version+ maximum(:updated_at)&.to_f+ end+privatedef rate_must_convert_amountserrors.add(:rate, :greater_than, count: 0) unless self.class.valid_rate?(rate)diff --git a/app/models/income_statement.rb b/app/models/income_statement.rbindex 95b92ecd8b..66a4dcb9c9 100644--- a/app/models/income_statement.rb+++ b/app/models/income_statement.rb@@ -351,7 +351,7 @@ class IncomeStatementfamily.tags_version,family.accounts_status_version,family.currency,- ExchangeRate.maximum(:updated_at)&.to_i,+ ExchangeRate.updated_at_version,family.investment_contributions_as_spending?]enddiff --git a/app/models/investment_statement.rb b/app/models/investment_statement.rbindex 35fad18d38..ae3d59d0fb 100644--- a/app/models/investment_statement.rb+++ b/app/models/investment_statement.rb@@ -815,7 +815,7 @@ class InvestmentStatement"investment_statement", "totals_query/v2", ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, family.currency,account_ids_hash, date_range.begin, date_range.end, family.entries_cache_version,# A rate stored later can complete totals that left an amount out.- ExchangeRate.maximum(:updated_at)&.to_i+ ExchangeRate.updated_at_version]) { Totals.new(family, account_ids: account_ids, date_range: date_range).call }enddiff --git a/app/models/transaction/search.rb b/app/models/transaction/search.rbindex 091b2fe802..dd37988cd7 100644--- a/app/models/transaction/search.rb+++ b/app/models/transaction/search.rb@@ -284,7 +284,7 @@ class Transaction::Searchfamily.currency,Digest::SHA256.hexdigest(attributes.sort.to_h.to_json), # cached by filtersfamily.entries_cache_version,- ExchangeRate.maximum(:updated_at)&.to_i, # a rate stored later can complete the totals+ ExchangeRate.updated_at_version, # a rate stored later can complete the totalsDigest::SHA256.hexdigest(family.tax_advantaged_account_ids.sort.to_json), # stable across processesaccessible_account_ids ? Digest::SHA256.hexdigest(accessible_account_ids.sort.to_json) : "all",Date.current # totals exclude scheduled entries as of "today" -- must roll over dailydiff --git a/test/models/exchange_rate_test.rb b/test/models/exchange_rate_test.rbindex 755580395f..279124dffd 100644--- a/test/models/exchange_rate_test.rb+++ b/test/models/exchange_rate_test.rb@@ -276,6 +276,33 @@ class ExchangeRateTest < ActiveSupport::TestCase"index_exchange_rates_on_to_currency_and_updated_at"end+ test "updated_at_version changes when a rate is stored within the same second as the previous one" do+ second = Time.zone.parse("2026-11-16 10:00:00")+ ExchangeRate.update_all(updated_at: second - 1.day)+ first = ExchangeRate.create!(from_currency: "KRW", to_currency: "USD", date: Date.current, rate: 0.00075)+ first.update_columns(updated_at: second + 0.1)+ before = ExchangeRate.updated_at_version++ later = ExchangeRate.create!(from_currency: "JPY", to_currency: "USD", date: Date.current, rate: 0.0065)+ later.update_columns(updated_at: second + 0.6)++ assert_not_equal before, ExchangeRate.updated_at_version+ end++ test "updated_at_version on a scope is that scope's newest update, read without counting rows" do+ eur = ExchangeRate.create!(from_currency: "KRW", to_currency: "EUR", date: Date.current, rate: 0.00065)+ eur.update_columns(updated_at: Time.zone.parse("2026-11-16 10:00:00.25"))+ ExchangeRate.create!(from_currency: "KRW", to_currency: "GBP", date: Date.current, rate: 0.00055)+ scope = ExchangeRate.where(to_currency: "EUR")++ version = nil+ assert_no_queries_match(/COUNT/i) do+ version = scope.updated_at_version+ end++ assert_equal scope.maximum(:updated_at).to_f, version+ end+private# With sequential scans priced out, the planner uses an index whenever one# can answer the query; without one it still falls back to a sequential scan.