summaryrefslogtreecommitdiff
path: root/lib/gitlab/issuable_metadata.rb
diff options
context:
space:
mode:
authorSean McGivern <sean@gitlab.com>2017-11-07 14:00:21 +0000
committerSean McGivern <sean@gitlab.com>2017-11-07 16:26:55 +0000
commit4d4ddb6004e6f7f56b337a49c6eedaad70d70862 (patch)
treec211bad025f8250f224f395a2f5687d71791cb13 /lib/gitlab/issuable_metadata.rb
parentdc1e6b436268c00bd1fdf3d15597a4656e029b95 (diff)
downloadgitlab-ce-4d4ddb6004e6f7f56b337a49c6eedaad70d70862.tar.gz
Fail when issuable_meta_data is called on an unlimited collectionfix-issues-api-list-performance
This method can be called with an array, or a relation: 1. Arrays always have a limited amount of values, so that's fine. 2. If the relation does not have a limit value applied, then we will load every single object in that collection, and prevent N+1 queries for the metadata for that. But that's wrong, because we should never call this without an explicit limit set. So we raise in that case, and this commit will see which specs fail. The only failing specs here were the issues API specs, and the specs for IssuableMetadata itself, and both have been addressed.
Diffstat (limited to 'lib/gitlab/issuable_metadata.rb')
-rw-r--r--lib/gitlab/issuable_metadata.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/gitlab/issuable_metadata.rb b/lib/gitlab/issuable_metadata.rb
index 977c05910d3..0c9de72329c 100644
--- a/lib/gitlab/issuable_metadata.rb
+++ b/lib/gitlab/issuable_metadata.rb
@@ -1,6 +1,14 @@
module Gitlab
module IssuableMetadata
def issuable_meta_data(issuable_collection, collection_type)
+ # ActiveRecord uses Object#extend for null relations.
+ if !(issuable_collection.singleton_class < ActiveRecord::NullRelation) &&
+ issuable_collection.respond_to?(:limit_value) &&
+ issuable_collection.limit_value.nil?
+
+ raise 'Collection must have a limit applied for preloading meta-data'
+ end
+
# map has to be used here since using pluck or select will
# throw an error when ordering issuables by priority which inserts
# a new order into the collection.