The personal finance app for everyone (by everyone)
Ruby
Perf/accounts controller index optimization (#1926)
William Wei Mingf210d1ca4c43
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex a50f601379..d5bdde8bf0 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -10,25 +10,28 @@ class AccountsController < ApplicationController@manual_accounts = family.accounts.listable_manual.where(id: @accessible_account_ids)+ .with_attached_logo.includes(:accountable, :account_providers, :plaid_account, :simplefin_account).order(:name)- @plaid_items = visible_provider_items(family.plaid_items.ordered.includes(:syncs, :plaid_accounts))- @simplefin_items = visible_provider_items(family.simplefin_items.ordered.includes(:syncs))- @lunchflow_items = visible_provider_items(family.lunchflow_items.ordered.includes(:syncs, :lunchflow_accounts))- @akahu_items = visible_provider_items(family.akahu_items.ordered.includes(:syncs, :akahu_accounts))- @up_items = visible_provider_items(family.up_items.ordered.includes(:syncs, :up_accounts))- @enable_banking_items = visible_provider_items(family.enable_banking_items.ordered.includes(:syncs))- @coinstats_items = visible_provider_items(family.coinstats_items.ordered.includes(:coinstats_accounts, :accounts, :syncs))- @mercury_items = visible_provider_items(family.mercury_items.ordered.includes(:syncs, :mercury_accounts))- @brex_items = visible_provider_items(family.brex_items.ordered.includes(:accounts, :syncs, brex_accounts: :account_provider))- @coinbase_items = visible_provider_items(family.coinbase_items.ordered.includes(:coinbase_accounts, :accounts, :syncs))- @snaptrade_items = visible_provider_items(family.snaptrade_items.ordered.includes(:syncs, :snaptrade_accounts))- @ibkr_items = visible_provider_items(family.ibkr_items.ordered.includes(:syncs, :ibkr_accounts))- @indexa_capital_items = visible_provider_items(family.indexa_capital_items.ordered.includes(:syncs, :indexa_capital_accounts))- @sophtron_items = visible_provider_items(family.sophtron_items.ordered.includes(:syncs, :sophtron_accounts))- @binance_items = visible_provider_items(family.binance_items.ordered.includes(:binance_accounts, :accounts, :syncs))- @questrade_items = visible_provider_items(family.questrade_items.ordered.includes(:syncs, questrade_accounts: :account_provider))- @wise_items = visible_provider_items(family.wise_items.ordered.includes(:syncs, :wise_accounts))+ @plaid_items = visible_provider_items(family.plaid_items.ordered.with_attached_logo.includes(:plaid_accounts))+ @simplefin_items = visible_provider_items(family.simplefin_items.ordered.with_attached_logo)+ @lunchflow_items = visible_provider_items(family.lunchflow_items.ordered.with_attached_logo.includes(:lunchflow_accounts))+ @akahu_items = visible_provider_items(family.akahu_items.ordered.with_attached_logo.includes(:akahu_accounts))+ @up_items = visible_provider_items(family.up_items.ordered.with_attached_logo.includes(:up_accounts))+ @enable_banking_items = visible_provider_items(family.enable_banking_items.ordered.with_attached_logo)+ @coinstats_items = visible_provider_items(family.coinstats_items.ordered.with_attached_logo.includes(:coinstats_accounts, :accounts))+ @mercury_items = visible_provider_items(family.mercury_items.ordered.with_attached_logo.includes(:mercury_accounts))+ @brex_items = visible_provider_items(family.brex_items.ordered.with_attached_logo.includes(:accounts, brex_accounts: :account_provider))+ @coinbase_items = visible_provider_items(family.coinbase_items.ordered.with_attached_logo.includes(:coinbase_accounts, :accounts))+ @snaptrade_items = visible_provider_items(family.snaptrade_items.ordered.with_attached_logo.includes(:snaptrade_accounts))+ @ibkr_items = visible_provider_items(family.ibkr_items.ordered.with_attached_logo.includes(:ibkr_accounts))+ @indexa_capital_items = visible_provider_items(family.indexa_capital_items.ordered.with_attached_logo.includes(:indexa_capital_accounts))+ @sophtron_items = visible_provider_items(family.sophtron_items.ordered.with_attached_logo.includes(:sophtron_accounts))+ @binance_items = visible_provider_items(family.binance_items.ordered.with_attached_logo.includes(:binance_accounts, :accounts))+ @questrade_items = visible_provider_items(family.questrade_items.ordered.with_attached_logo.includes(:accounts, questrade_accounts: :account_provider))+ @wise_items = visible_provider_items(family.wise_items.ordered.includes(:wise_accounts, :accounts))++ preload_latest_sync_metadata_for_index!# Build sync stats maps for all providersbuild_sync_stats_maps@@ -254,6 +257,41 @@ class AccountsController < ApplicationControllerendend+ def preload_latest_sync_metadata_for_index!+ items = [+ @plaid_items,+ @simplefin_items,+ @lunchflow_items,+ @akahu_items,+ @up_items,+ @enable_banking_items,+ @coinstats_items,+ @mercury_items,+ @brex_items,+ @coinbase_items,+ @snaptrade_items,+ @ibkr_items,+ @indexa_capital_items,+ @sophtron_items,+ @binance_items,+ @questrade_items,+ @wise_items+ ].flatten.compact++ accounts = @manual_accounts.to_a+ items.each do |item|+ next unless item.respond_to?(:accounts)+ accounts.concat(item.accounts)+ end+ accounts = accounts.uniq { |account| account.id }++ syncables = items + accounts++ Current.latest_sync_by_syncable = Sync.latest_by_syncable(syncables)+ Current.latest_completed_sync_by_syncable = Sync.latest_completed_by_syncable(syncables)+ Current.syncing_by_syncable = Sync.syncing_by_syncable(syncables)+ end+def build_statement_tab_datareturn unless statement_tab_active?@@ -289,6 +327,8 @@ class AccountsController < ApplicationController# Builds sync stats maps for all provider types to avoid N+1 queries in viewsdef build_sync_stats_maps+ manual_accounts_exist = @manual_accounts.any?+# SimpleFIN sync stats@simplefin_sync_stats_map = {}@simplefin_has_unlinked_map = {}@@ -296,22 +336,37 @@ class AccountsController < ApplicationController@simplefin_show_relink_map = {}@simplefin_duplicate_only_map = {}+ simplefin_item_ids = @simplefin_items.map(&:id)+ simplefin_accounts_counts_by_item_id =+ if simplefin_item_ids.any?+ SimplefinAccount.where(simplefin_item_id: simplefin_item_ids).group(:simplefin_item_id).count+ else+ {}+ end+ simplefin_unlinked_counts_by_item_id =+ if simplefin_item_ids.any?+ SimplefinAccount.where(simplefin_item_id: simplefin_item_ids)+ .left_joins(:account, :account_provider)+ .where(accounts: { id: nil }, account_providers: { id: nil })+ .group(:simplefin_item_id)+ .count+ else+ {}+ end+@simplefin_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_recordstats = latest_sync&.sync_stats || {}@simplefin_sync_stats_map[item.id] = stats- @simplefin_has_unlinked_map[item.id] = item.family.accounts.listable_manual.exists?+ @simplefin_has_unlinked_map[item.id] = manual_accounts_exist# Count unlinked accounts- count = item.simplefin_accounts- .left_joins(:account, :account_provider)- .where(accounts: { id: nil }, account_providers: { id: nil })- .count+ count = simplefin_unlinked_counts_by_item_id[item.id].to_i@simplefin_unlinked_count_map[item.id] = count# CTA visibilitymanuals_exist = @simplefin_has_unlinked_map[item.id]- sfa_any = item.simplefin_accounts.loaded? ? item.simplefin_accounts.any? : item.simplefin_accounts.exists?+ sfa_any = simplefin_accounts_counts_by_item_id[item.id].to_i > 0@simplefin_show_relink_map[item.id] = (count.to_i == 0 && manuals_exist && sfa_any)# Check if all errors are duplicate-skips@@ -327,28 +382,28 @@ class AccountsController < ApplicationController# Plaid sync stats@plaid_sync_stats_map = {}@plaid_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@plaid_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end# Lunchflow sync stats@lunchflow_sync_stats_map = {}@lunchflow_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@lunchflow_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end# Akahu sync stats@akahu_sync_stats_map = {}@akahu_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@akahu_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end# Up sync stats@up_sync_stats_map = {}@up_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@up_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end@@ -356,7 +411,7 @@ class AccountsController < ApplicationController@enable_banking_sync_stats_map = {}@enable_banking_latest_sync_error_map = {}@enable_banking_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@enable_banking_sync_stats_map[item.id] = latest_sync&.sync_stats || {}@enable_banking_latest_sync_error_map[item.id] = latest_sync&.errorend@@ -364,21 +419,21 @@ class AccountsController < ApplicationController# CoinStats sync stats@coinstats_sync_stats_map = {}@coinstats_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@coinstats_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end# Sophtron sync stats@sophtron_sync_stats_map = {}@sophtron_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@sophtron_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end# Mercury sync stats@mercury_sync_stats_map = {}@mercury_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@mercury_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end@@ -387,7 +442,7 @@ class AccountsController < ApplicationController@brex_account_counts_map = {}@brex_institutions_count_map = {}@brex_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@brex_sync_stats_map[item.id] = latest_sync&.sync_stats || {}brex_accounts = item.brex_accounts.to_alinked_count = brex_accounts.count { |brex_account| brex_account.account_provider.present? }@@ -406,22 +461,31 @@ class AccountsController < ApplicationController# Coinbase sync stats@coinbase_sync_stats_map = {}@coinbase_unlinked_count_map = {}++ coinbase_item_ids = @coinbase_items.map(&:id)+ coinbase_unlinked_counts_by_item_id =+ if coinbase_item_ids.any?+ CoinbaseAccount.where(coinbase_item_id: coinbase_item_ids)+ .left_joins(:account_provider)+ .where(account_providers: { id: nil })+ .group(:coinbase_item_id)+ .count+ else+ {}+ end+@coinbase_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@coinbase_sync_stats_map[item.id] = latest_sync&.sync_stats || {}# Count unlinked accounts- count = item.coinbase_accounts- .left_joins(:account_provider)- .where(account_providers: { id: nil })- .count- @coinbase_unlinked_count_map[item.id] = count+ @coinbase_unlinked_count_map[item.id] = coinbase_unlinked_counts_by_item_id[item.id].to_iend# IndexaCapital sync stats@indexa_capital_sync_stats_map = {}@indexa_capital_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@indexa_capital_sync_stats_map[item.id] = latest_sync&.sync_stats || {}end@@ -429,7 +493,7 @@ class AccountsController < ApplicationController@binance_sync_stats_map = {}@binance_unlinked_count_map = {}@binance_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@binance_sync_stats_map[item.id] = latest_sync&.sync_stats || {}# Count unlinked accounts@@ -444,7 +508,7 @@ class AccountsController < ApplicationController@questrade_sync_stats_map = {}@questrade_account_counts_map = {}@questrade_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@questrade_sync_stats_map[item.id] = latest_sync&.sync_stats || {}accounts = item.questrade_accounts.to_alinked = accounts.count { |a| a.account_provider.present? }@@ -456,7 +520,7 @@ class AccountsController < ApplicationController# Wise sync stats@wise_sync_stats_map = {}@wise_items.each do |item|- latest_sync = item.syncs.ordered.first+ latest_sync = item.latest_sync_record@wise_sync_stats_map[item.id] = latest_sync&.sync_stats || {}endenddiff --git a/app/models/concerns/syncable.rb b/app/models/concerns/syncable.rbindex ff47e0c6a2..90809b4d12 100644--- a/app/models/concerns/syncable.rb+++ b/app/models/concerns/syncable.rb@@ -6,7 +6,42 @@ module Syncableenddef syncing?- syncs.visible.any?+ if Current.respond_to?(:syncing_by_syncable) && (syncing_by_syncable = Current.syncing_by_syncable)+ key = [ self.class.base_class.name, id ]+ return !!syncing_by_syncable[key] if syncing_by_syncable.key?(key)+ end++ if association(:syncs).loaded?+ syncs.any?(&:visible?)+ else+ syncs.visible.any?+ end+ end++ def latest_sync_record+ if Current.respond_to?(:latest_sync_by_syncable) && (latest_sync_by_syncable = Current.latest_sync_by_syncable)+ key = [ self.class.base_class.name, id ]+ return latest_sync_by_syncable[key] if latest_sync_by_syncable.key?(key)+ end++ if association(:syncs).loaded?+ syncs.max_by { |sync| [ sync.created_at, sync.id ] }+ else+ syncs.ordered.first+ end+ end++ def latest_completed_sync_record+ if Current.respond_to?(:latest_completed_sync_by_syncable) && (latest_completed_sync_by_syncable = Current.latest_completed_sync_by_syncable)+ key = [ self.class.base_class.name, id ]+ return latest_completed_sync_by_syncable[key] if latest_completed_sync_by_syncable.key?(key)+ end++ if association(:syncs).loaded?+ syncs.select(&:completed?).max_by { |sync| [ sync.created_at, sync.id ] }+ else+ syncs.completed.ordered.first+ endend# Schedules a sync for syncable. If there is an existing sync pending/syncing for this syncable,@@ -56,24 +91,24 @@ module Syncableenddef sync_error- latest_sync&.error || latest_sync&.children&.map(&:error)&.compact&.first+ latest_sync_record&.error || latest_sync_record&.children&.map(&:error)&.compact&.firstenddef last_synced_at- latest_completed_sync&.completed_at+ latest_completed_sync_record&.completed_atenddef last_sync_created_at- latest_sync&.created_at+ latest_sync_record&.created_atendprivatedef latest_sync- syncs.ordered.first+ latest_sync_recordenddef latest_completed_sync- syncs.completed.ordered.first+ latest_completed_sync_recordenddef syncerdiff --git a/app/models/current.rb b/app/models/current.rbindex 612c2af768..af9f14b770 100644--- a/app/models/current.rb+++ b/app/models/current.rb@@ -2,6 +2,7 @@ class Current < ActiveSupport::CurrentAttributesattribute :user_agent, :ip_addressattribute :session+ attribute :latest_sync_by_syncable, :latest_completed_sync_by_syncable, :syncing_by_syncabledelegate :family, to: :user, allow_nil: truediff --git a/app/models/ibkr_item.rb b/app/models/ibkr_item.rbindex aca60d1b22..923debb1e1 100644--- a/app/models/ibkr_item.rb+++ b/app/models/ibkr_item.rb@@ -81,7 +81,7 @@ class IbkrItem < ApplicationRecordenddef accounts- ibkr_accounts.includes(account_provider: :account).filter_map(&:current_account).uniq+ @accounts ||= ibkr_accounts.includes(account_provider: :account).filter_map(&:current_account).uniqenddef linked_ibkr_accountsdiff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rbindex deef9039e5..c10e987405 100644--- a/app/models/plaid_item.rb+++ b/app/models/plaid_item.rb@@ -29,8 +29,7 @@ class PlaidItem < ApplicationRecord# Get accounts from both new and legacy systemsdef accounts- # Preload associations to avoid N+1 queries- plaid_accounts+ @accounts ||= plaid_accounts.includes(:account, account_provider: :account).map(&:current_account).compactdiff --git a/app/models/simplefin_item.rb b/app/models/simplefin_item.rbindex d45b0579ba..ee09a86294 100644--- a/app/models/simplefin_item.rb+++ b/app/models/simplefin_item.rb@@ -32,8 +32,7 @@ class SimplefinItem < ApplicationRecord# Get accounts from both new and legacy systemsdef accounts- # Preload associations to avoid N+1 queries- simplefin_accounts+ @accounts ||= simplefin_accounts.includes(:account, account_provider: :account).map(&:current_account).compactdiff --git a/app/models/snaptrade_item.rb b/app/models/snaptrade_item.rbindex 4c055337e9..158b76a50a 100644--- a/app/models/snaptrade_item.rb+++ b/app/models/snaptrade_item.rb@@ -205,7 +205,7 @@ class SnaptradeItem < ApplicationRecord# Get all Sure accounts linked to this SnapTrade itemdef accounts- snaptrade_accounts+ @accounts ||= snaptrade_accounts.includes(account_provider: :account).filter_map { |sa| sa.current_account }.uniqdiff --git a/app/models/sync.rb b/app/models/sync.rbindex 9c53990289..310c43e55b 100644--- a/app/models/sync.rb+++ b/app/models/sync.rb@@ -73,6 +73,57 @@ class Sync < ApplicationRecordqueryend+ def for_syncables(syncables)+ syncables = Array(syncables).compact+ return none if syncables.empty?++ scope = none+ syncables.group_by { |record| record.class.base_class.name }.each do |type, records|+ ids = records.map(&:id)+ scope = scope.or(where(syncable_type: type, syncable_id: ids))+ end+ scope+ end++ def latest_by_syncable(syncables)+ keyed_syncables = syncable_keys(syncables)+ return {} if keyed_syncables.empty?++ latest = for_syncables(syncables)+ .select("DISTINCT ON (syncable_type, syncable_id) syncs.*")+ .order("syncable_type, syncable_id, created_at DESC, id DESC")+ .includes(:children)+ .index_by { |sync| [ sync.syncable_type, sync.syncable_id ] }++ keyed_syncables.index_with { |key| latest[key] }+ end++ def latest_completed_by_syncable(syncables)+ keyed_syncables = syncable_keys(syncables)+ return {} if keyed_syncables.empty?++ latest = for_syncables(syncables)+ .completed+ .select("DISTINCT ON (syncable_type, syncable_id) syncs.*")+ .order("syncable_type, syncable_id, created_at DESC, id DESC")+ .index_by { |sync| [ sync.syncable_type, sync.syncable_id ] }++ keyed_syncables.index_with { |key| latest[key] }+ end++ def syncing_by_syncable(syncables)+ keyed_syncables = syncable_keys(syncables)+ return {} if keyed_syncables.empty?++ syncing_keys = for_syncables(syncables)+ .visible+ .distinct+ .pluck(:syncable_type, :syncable_id)+ .to_set++ keyed_syncables.index_with { |key| syncing_keys.include?(key) }+ end+# True iff the family has any pending/syncing Sync — across its own row,# its accounts, and every Syncable provider `*_items` association. Built# on `for_family` so new provider integrations are picked up automatically@@ -82,6 +133,11 @@ class Sync < ApplicationRecordendprivate+ def syncable_keys(syncables)+ Array(syncables).compact.uniq { |record| [ record.class.base_class.name, record.id ] }+ .map { |record| [ record.class.base_class.name, record.id ] }+ end+def account_syncable_ids(family, resource_owner)(resource_owner ? resource_owner.accessible_accounts : family.accounts).where(family_id: family.id)@@ -102,6 +158,11 @@ class Sync < ApplicationRecordpending? || syncing?end+ # Mirrors the `visible` scope for in-memory checks on preloaded syncs.+ def visible?+ in_progress? && created_at > VISIBLE_FOR.ago+ end+def terminal?completed? || failed? || stale?enddiff --git a/app/views/accounts/index/_account_groups.erb b/app/views/accounts/index/_account_groups.erbindex 79709142e2..2f1ecef360 100644--- a/app/views/accounts/index/_account_groups.erb+++ b/app/views/accounts/index/_account_groups.erb@@ -3,7 +3,14 @@<% if accounts.any? %><% ActiveRecord::Associations::Preloader.new(records: accounts,- associations: [ :account_shares, :accountable, :plaid_account, :simplefin_account, { account_providers: :provider } ]+ associations: [+ :account_shares,+ :accountable,+ :plaid_account,+ :simplefin_account,+ { account_providers: :provider },+ { logo_attachment: :blob }+ ]).call %><% end %><% accounts.group_by(&:accountable_type).sort_by { |group, _| group }.each do |group, accounts| %>diff --git a/app/views/simplefin_items/_simplefin_item.html.erb b/app/views/simplefin_items/_simplefin_item.html.erbindex d07044d10d..93dc2dcfe4 100644--- a/app/views/simplefin_items/_simplefin_item.html.erb+++ b/app/views/simplefin_items/_simplefin_item.html.erb@@ -1,6 +1,7 @@<%# locals: (simplefin_item:) %><%= tag.div id: dom_id(simplefin_item) do %>+ <% accounts = simplefin_item.accounts %><%# Compute unlinked count early so it's available for both menu and bottom section %><% unlinked_count = if defined?(@simplefin_unlinked_count_map) && @simplefin_unlinked_count_map@simplefin_unlinked_count_map[simplefin_item.id] || 0@@ -40,7 +41,7 @@<p class="text-destructive text-sm animate-pulse"><%= t(".deletion_in_progress") %></p><% end %></div>- <% if simplefin_item.accounts.any? %>+ <% if accounts.any? %><p class="text-xs text-secondary"><%= simplefin_item.institution_summary %></p>@@ -200,8 +201,8 @@suggestions: suggestions %><% end %>- <% if simplefin_item.accounts.any? %>- <%= render "accounts/index/account_groups", accounts: simplefin_item.accounts %>+ <% if accounts.any? %>+ <%= render "accounts/index/account_groups", accounts: accounts %><% end %><%# Sync summary (collapsible) - using shared ProviderSyncSummary component@@ -219,7 +220,7 @@institutions_count: simplefin_item.connected_institutions.size) %>- <% if unlinked_count.to_i > 0 && simplefin_item.accounts.empty? %>+ <% if unlinked_count.to_i > 0 && accounts.empty? %><%# No accounts imported yet - show prominent setup prompt %><div class="p-4 flex flex-col gap-3 items-center justify-center"><p class="text-primary font-medium text-sm"><%= t(".setup_needed") %></p>@@ -232,7 +233,7 @@frame: :modal) %></div>- <% elsif simplefin_item.accounts.empty? %>+ <% elsif accounts.empty? %><div class="p-4 flex flex-col gap-3 items-center justify-center"><p class="text-primary font-medium text-sm"><%= t(".no_accounts_title") %></p><p class="text-secondary text-sm"><%= t(".no_accounts_description") %></p>diff --git a/db/migrate/20260611120000_add_syncs_distinct_on_index.rb b/db/migrate/20260611120000_add_syncs_distinct_on_index.rbnew file mode 100644index 0000000000..c12b1de80b--- /dev/null+++ b/db/migrate/20260611120000_add_syncs_distinct_on_index.rb@@ -0,0 +1,8 @@+class AddSyncsDistinctOnIndex < ActiveRecord::Migration[7.2]+ def change+ add_index :syncs,+ [ :syncable_type, :syncable_id, :created_at, :id ],+ order: { created_at: :desc, id: :desc },+ name: "index_syncs_on_syncable_and_created_at_and_id"+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 7d4095647a..7d858b70bd 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -1983,6 +1983,7 @@ ActiveRecord::Schema[7.2].define(version: 2026_07_14_120000) dot.datetime "cancel_requested_at"t.index ["parent_id"], name: "index_syncs_on_parent_id"t.index ["status"], name: "index_syncs_on_status"+ t.index ["syncable_type", "syncable_id", "created_at", "id"], name: "index_syncs_on_syncable_and_created_at_and_id", order: { created_at: :desc, id: :desc }t.index ["syncable_type", "syncable_id"], name: "index_syncs_on_syncable"enddiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex a62ead4ac3..a3858f305e 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -204,7 +204,6 @@ class AccountsControllerTest < ActionDispatch::IntegrationTesttest "syncing linked account triggers sync for all provider items" doplaid_account = plaid_accounts(:one)- plaid_item = plaid_account.plaid_itemAccountProvider.create!(account: @account, provider: plaid_account)# Reload to ensure the account has the provider association loadeddiff --git a/test/models/simplefin_item_test.rb b/test/models/simplefin_item_test.rbindex e5b05e1017..0c689c1b51 100644--- a/test/models/simplefin_item_test.rb+++ b/test/models/simplefin_item_test.rb@@ -123,6 +123,15 @@ class SimplefinItemTest < ActiveSupport::TestCaseassert_respond_to @simplefin_item, :syncing?end+ test "memoizes accounts across repeated calls" do+ queries = capture_sql_queries do+ @simplefin_item.accounts+ @simplefin_item.accounts+ end++ assert_equal 1, queries.grep(/FROM "simplefin_accounts"/).size+ end+test "scopes work correctly" do# Create one for deletionitem_for_deletion = SimplefinItem.create!(@@ -225,4 +234,25 @@ class SimplefinItemTest < ActiveSupport::TestCase)assert_equal "2 institutions", @simplefin_item.institution_summaryend++ private+ def capture_sql_queries+ queries = []++ callback = lambda do |_name, _start, _finish, _message_id, payload|+ sql = payload[:sql].to_s+ name = payload[:name].to_s++ next if name == "SCHEMA"+ next if sql.match?(/\A(?:BEGIN|COMMIT|ROLLBACK)\b/i)++ queries << sql+ end++ ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do+ yield+ end++ queries+ endenddiff --git a/test/models/sync_test.rb b/test/models/sync_test.rbindex 08fa5aa2d5..747f46ff23 100644--- a/test/models/sync_test.rb+++ b/test/models/sync_test.rb@@ -397,6 +397,20 @@ class SyncTest < ActiveSupport::TestCaseassert Sync.any_incomplete_for?(family)end+ test "visible? matches visible scope for in-progress syncs" do+ sync = Sync.create!(syncable: accounts(:depository), status: :syncing)++ assert sync.visible?+ assert_includes Sync.visible, sync+ end++ test "visible? excludes stale in-progress syncs outside VISIBLE_FOR window" do+ sync = Sync.create!(syncable: accounts(:depository), status: :syncing, created_at: Sync::VISIBLE_FOR.ago - 1.minute)++ refute sync.visible?+ refute_includes Sync.visible, sync+ end+test "api error payload is present for failed syncs without raw error text" dosync = Sync.create!(syncable: accounts(:depository), status: :failed)diff --git a/test/models/syncable_test.rb b/test/models/syncable_test.rbnew file mode 100644index 0000000000..79136bfd69--- /dev/null+++ b/test/models/syncable_test.rb@@ -0,0 +1,103 @@+require "test_helper"++class SyncableTest < ActiveSupport::TestCase+ def test_current_sync_maps_avoid_additional_queries+ account = Account.find(accounts(:depository).id)++ sync = Sync.create!(syncable: account)+ sync.start!++ key = [ account.class.base_class.name, account.id ]+ Current.latest_sync_by_syncable = { key => sync }+ Current.latest_completed_sync_by_syncable = { key => nil }+ Current.syncing_by_syncable = { key => true }++ queries = capture_sql_queries do+ account.syncing?+ account.last_sync_created_at+ account.last_synced_at+ end++ assert_equal [], queries+ ensure+ Current.reset+ end++ def test_syncing_without_current_maps_queries_database+ account = Account.find(accounts(:depository).id)+ Current.reset++ sync = Sync.create!(syncable: account)+ sync.start!++ queries = capture_sql_queries do+ assert account.syncing?+ end++ assert queries.grep(/FROM "syncs"/).any?,+ "Expected syncing? to query syncs when Current maps are absent"+ ensure+ Current.reset+ end++ def test_latest_completed_sync_without_current_maps_queries_database+ account = Account.find(accounts(:depository).id)+ Current.reset++ sync = Sync.create!(syncable: account, status: :completed)+ sync.update_column(:completed_at, Time.current)++ queries = capture_sql_queries do+ account.last_synced_at+ end++ assert queries.grep(/FROM "syncs"/).any?,+ "Expected latest_completed_sync_record to query syncs when Current maps are absent"+ ensure+ Current.reset+ end++ def test_partial_current_sync_maps_fall_back_to_database+ account = Account.find(accounts(:depository).id)+ Current.reset++ sync = Sync.create!(syncable: account)+ sync.start!++ key = [ account.class.base_class.name, account.id ]+ Current.latest_sync_by_syncable = {}+ Current.latest_completed_sync_by_syncable = {}+ Current.syncing_by_syncable = {}++ queries = capture_sql_queries do+ assert account.syncing?+ assert_equal sync.created_at, account.last_sync_created_at+ end++ assert queries.grep(/FROM "syncs"/).any?,+ "Expected partial Current maps to fall back to database queries"+ ensure+ Current.reset+ end++ private+ def capture_sql_queries+ queries = []++ callback = lambda do |_name, _start, _finish, _message_id, payload|+ sql = payload[:sql].to_s+ name = payload[:name].to_s++ next if name == "SCHEMA"+ next if sql.match?(/\A(?:BEGIN|COMMIT|ROLLBACK)\b/i)++ queries << sql+ end++ ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do+ yield+ end++ queries+ end+end