Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(sync): resolve N+1 query in child finalization (#2693)

sentry[bot]7b6636d8ea61
diff --git a/app/models/sync.rb b/app/models/sync.rb
index eb0cd4c845..6d34c7ecfd 100644
--- a/app/models/sync.rb
+++ b/app/models/sync.rb
@@ -162,6 +162,10 @@ class Sync < ApplicationRecord
Sync.transaction do
lock!
+ # Eagerly load children once so that all_children_finalized? and
+ # has_failed_children? can filter in-memory without additional DB queries.
+ children.load
+
# If this is the "parent" and there are still children running, don't finalize.
return unless all_children_finalized?
@@ -213,11 +217,11 @@ class Sync < ApplicationRecord
end
def has_failed_children?
- children.failed.any?
+ children.any?(&:failed?)
end
def all_children_finalized?
- children.incomplete.empty?
+ children.none? { |child| child.pending? || child.syncing? }
end
def perform_post_sync