Files
sure/test/models/syncable_test.rb
William Wei Ming f210d1ca4c Perf/accounts controller index optimization (#1926)
* fix Prism issue - assigned but unused variable plaid_item

* measurement to improve accounts_controller__index performance based on skylight

* fix FEEDBACK - Stop eager-loading full sync histories on accounts index

* fix FEEDBACK - Remove sticky memoization from SimplefinItem#accounts

* fix lint error in PR review

* fix FEEDBACK - Avoid returning map misses as authoritative results

* fix test error

* fix FEEDBACK - Avoid redundant query by using already-loaded @manual_accounts

* Address maintainer review on accounts index sync preloading
Memoize SimplefinItem#accounts, align syncing? with Sync#visible?,
and add fallback regression tests.

* Add composite index for sync DISTINCT ON queries
Support latest_by_syncable ordering with syncable_type, syncable_id,
created_at DESC, and id DESC.

* fix error in dockerfile.preview file

* Suppress Pipelock false positives on CI database fixtures.
Pipelock 2.7.0 full-repo audit flags ephemeral postgres:// URLs in
workflow env blocks. Re-apply inline suppressions lost in the main merge.

* Address review: guard partial Current sync maps and drop unrelated diff
Add key? checks so partially populated Current sync maps fall back to DB
queries. Revert Dockerfile.preview and pipelock.yml changes unrelated to
the accounts index N+1 optimization.

* fix(sync): fully populate Current sync maps for all preloaded syncables

* fix(ci): skip scheduled preview cleanup on forks
Only run the hourly Cloudflare preview cleanup on we-promise/sure,
where the required secrets exist.

* Remove schema.rb Postgres-version churn, keep only new syncs index

Reset db/schema.rb to upstream/main and re-add only the
index_syncs_on_syncable_and_created_at_and_id index. The prior diff
included ~30 lines of noise (check-constraint reformatting, virtual
column reformat, and column reorderings) caused by dumping under a
different PostgreSQL version, which obscured the single intended
schema change and invited merge conflicts.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-25 03:35:35 +02:00

104 lines
2.6 KiB
Ruby

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