The personal finance app for everyone (by everyone)
Ruby
fix(transactions): cache only IDs of upcoming recurring transactions (#3907)
GFR97fa8a2eda5d
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex c17fc06244..57bb87131b 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -79,16 +79,23 @@ class TransactionsController < ApplicationControllerCurrent.accessible_entries.uncategorized_transactions.countend- # Load projected recurring transactions for next 10 days- @projected_recurring = Rails.cache.fetch(projected_recurring_cache_key, expires_in: 1.day) do+ # Load projected recurring transactions for next 10 days. Only the IDs are+ # cached: cached ActiveRecord objects outlive schema changes across+ # upgrades and raise MissingAttributeError for columns added since.+ projected_recurring_ids = Rails.cache.fetch(projected_recurring_cache_key, expires_in: 1.day) doCurrent.family.recurring_transactions.accessible_by(Current.user).active.where("next_expected_date <= ? AND next_expected_date >= ?",10.days.from_now.to_date,Date.current)- .includes(:merchant)- .to_a+ .pluck(:id)+ end+ @projected_recurring = if projected_recurring_ids.empty?+ []+ else+ Current.family.recurring_transactions.accessible_by(Current.user)+ .where(id: projected_recurring_ids).includes(:merchant).to_aend@breadcrumbs = [ [ t("breadcrumbs.home"), root_path ], [ t("breadcrumbs.transactions"), nil ] ]@@ -523,7 +530,7 @@ class TransactionsController < ApplicationController# name/logo, but editing a FamilyMerchant or a shared ProviderMerchant# doesn't touch `recurring_transactions`.def projected_recurring_cache_key- "transactions_projected_recurring/v5/#{Current.family.id}/#{Current.user.id}/#{Date.current}/" \+ "transactions_projected_recurring/v6/#{Current.family.id}/#{Current.user.id}/#{Date.current}/" \"#{Current.family.recurring_transactions_version}/#{Current.family.accounts_status_version}/" \"#{Current.family.recurring_transaction_merchants_version}/#{Current.account_share_version}"enddiff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 31889c6262..6d84e7c2fe 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -1398,6 +1398,44 @@ endRails.cache = original_cacheend+ test "index renders when the projected_recurring cache holds records from an older schema" do+ # Regression: the cache used to hold whole RecurringTransaction objects. After+ # an upgrade that added columns (e.g. payment_url in 0.7.5), the entry written+ # by the previous version was still served and rendering raised+ # ActiveModel::MissingAttributeError until the key rolled over the next day.+ original_cache = Rails.cache+ written_keys = []+ Rails.cache = Class.new(ActiveSupport::Cache::MemoryStore) {+ define_method(:write_entry) do |key, entry, **options|+ written_keys << key+ super(key, entry, **options)+ end+ }.new++ recurring = recurring_transactions(:netflix_subscription)++ get transactions_url+ assert_response :success++ cache_keys = written_keys.grep(/transactions_projected_recurring/).uniq+ assert_not_empty cache_keys, "the first request should populate the projected-recurring cache"++ # Write the Marshal payload the previous version produced: the record's+ # attributes without the columns that version did not have yet.+ stale_payload = [ recurring.attributes_for_database.except("payment_url", "autopay", "notes"), false, [ [ :merchant, recurring.merchant ] ] ]+ stale_record = RecurringTransaction.allocate+ stale_record.define_singleton_method(:marshal_dump) { stale_payload }+ cache_keys.each { |key| Rails.cache.write(key, [ stale_record ]) }+ assert_raises(ActiveModel::MissingAttributeError) { Rails.cache.read(cache_keys.first).first.payment_url }++ get transactions_url+ assert_response :success+ assert_match(/#{Regexp.escape(recurring.merchant.name)}/, response.body,+ "the projected recurring transaction should still render from fresh records")+ ensure+ Rails.cache = original_cache+ end+test "index uncategorized_count cache reflects new transactions immediately" dooriginal_cache = Rails.cacheRails.cache = ActiveSupport::Cache::MemoryStore.new