The personal finance app for everyone (by everyone)
Ruby
Fix income statement cache compatibility during upgrades (#5434)
Sure Admin (bot)19b8718f35ef
diff --git a/app/models/income_statement.rb b/app/models/income_statement.rbindex 86ccc794a3..736c6648a7 100644--- a/app/models/income_statement.rb+++ b/app/models/income_statement.rb@@ -419,7 +419,7 @@ class IncomeStatementsql_hash = Digest::MD5.hexdigest(transactions_scope.to_sql)Rails.cache.fetch([- "income_statement", "totals_query", "v2", ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, included_account_ids_hash, sql_hash, date_range.begin, date_range.end, *cache_freshness_key, *freshness+ "income_statement", "totals_query", Totals::CACHE_VERSION, ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, included_account_ids_hash, sql_hash, date_range.begin, date_range.end, *cache_freshness_key, *freshness]) { Totals.new(family, transactions_scope: transactions_scope, date_range: date_range, included_account_ids: included_account_ids).call }end@@ -427,7 +427,7 @@ class IncomeStatementsql_hash = Digest::MD5.hexdigest(transactions_scope.to_sql)Rails.cache.fetch([- "income_statement", "monthly_totals_query", "v2", ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, included_account_ids_hash, sql_hash, date_range.begin, date_range.end, *cache_freshness_key, family.accounts.maximum(:updated_at)&.to_i+ "income_statement", "monthly_totals_query", Totals::CACHE_VERSION, ExchangeRate::CONVERSION_CACHE_VERSION, family.id, user&.id, included_account_ids_hash, sql_hash, date_range.begin, date_range.end, *cache_freshness_key, family.accounts.maximum(:updated_at)&.to_i]) { Totals.new(family, transactions_scope: transactions_scope, date_range: date_range, included_account_ids: included_account_ids, group_by_month: true).call }enddiff --git a/app/models/income_statement/totals.rb b/app/models/income_statement/totals.rbindex 4df69ea9d8..6519937f41 100644--- a/app/models/income_statement/totals.rb+++ b/app/models/income_statement/totals.rb@@ -1,4 +1,7 @@class IncomeStatement::Totals+ # Bump when TotalsRow changes: persisted rows must deserialize with this shape.+ CACHE_VERSION = "v3"+include IncomeStatement::ScopedTransactionsQuerydef initialize(family, transactions_scope:, date_range:, include_trades: true, included_account_ids: nil, group_by_month: false)diff --git a/test/models/income_statement_test.rb b/test/models/income_statement_test.rbindex 445a73501e..bd1195a805 100644--- a/test/models/income_statement_test.rb+++ b/test/models/income_statement_test.rb@@ -213,6 +213,47 @@ class IncomeStatementTest < ActiveSupport::TestCaseIncomeStatement.new(@family).daily_expense_series(period: period, category_ids: [ @food_category.id ])end+ test "upgrading skips serialized totals from before the month field was added" do+ cache = ActiveSupport::Cache::MemoryStore.new(coder: Marshal)+ Rails.stubs(:cache).returns(cache)+ statement = IncomeStatement.new(@family)+ date_range = Period.last_30_days.date_range+ scope = @family.transactions.visible.excluding_pending.merge(Entry.excluding_scheduled)+ legacy_key = [+ "income_statement", "totals_query", "v2", ExchangeRate::CONVERSION_CACHE_VERSION,+ @family.id, statement.user&.id, statement.send(:included_account_ids_hash),+ Digest::MD5.hexdigest(scope.to_sql), date_range.begin, date_range.end,+ *statement.send(:cache_freshness_key)+ ]++ # Serialize the actual pre-upgrade shape under its original constant name.+ current_row = IncomeStatement::Totals.const_get(:TotalsRow)+ begin+ IncomeStatement::Totals.send(:remove_const, :TotalsRow)+ legacy_row = IncomeStatement::Totals.const_set(:TotalsRow, Data.define(+ :parent_category_id, :category_id, :classification, :total,+ :transactions_count, :is_uncategorized_investment+ ))+ cache.write(legacy_key, [ legacy_row.new(+ parent_category_id: nil, category_id: nil, classification: "expense",+ total: 999, transactions_count: 1, is_uncategorized_investment: false+ ) ])+ ensure+ IncomeStatement::Totals.send(:remove_const, :TotalsRow)+ IncomeStatement::Totals.const_set(:TotalsRow, current_row)+ IncomeStatement::Totals.private_constant :TotalsRow+ end++ assert_raises(TypeError) { cache.read(legacy_key) }++ 2.times do+ totals = IncomeStatement.new(@family).totals(date_range: date_range)+ assert_equal Money.new(1000, @family.currency), totals.income_money+ assert_equal Money.new(900, @family.currency), totals.expense_money+ assert_equal 4, totals.transactions_count+ end+ end+test "calculates totals for transactions" doincome_statement = IncomeStatement.new(@family)totals = income_statement.totals(date_range: Period.last_30_days.date_range)