Project

General

Profile

Patch #31076 » 0001-introduces-IssueQuery-lazy_issues.patch

Jens Krämer, 2019-03-21 11:27

View differences:

app/models/issue_query.rb
293 293
      limit(options[:limit]).
294 294
      offset(options[:offset])
295 295

  
296
    return preload_issues scope
297
  rescue ::ActiveRecord::StatementInvalid => e
298
    raise StatementInvalid.new(e.message)
299
  end
300

  
301
  # Like #issues, but returns an enumerator that lazily loads batches of issues
302
  # Valid options are :order, :offset, :limit, :include, :conditions
303
  def lazy_issues(options={})
304
    issue_ids = self.issue_ids options
305

  
306
    return Enumerator.new(issue_ids.length) do |yielder|
307

  
308
      issue_ids.each_slice(500) do |chunked_ids|
309
        # The FIELD() function is MySQL-specific
310
        scope = Issue
311
          .where(id: chunked_ids)
312
          .order("FIELD(id,#{chunked_ids.join(',')})")
313

  
314
        preload_issues(scope).each do |issue|
315
          yielder << issue
316
        end
317
      end
318
    end
319
  end
320

  
321
  # takes a scope, returns an array
322
  def preload_issues(scope)
296 323
    scope = scope.preload([:tracker, :author, :assigned_to, :fixed_version, :category, :attachments] & columns.map(&:name))
297 324
    if has_custom_field_column?
298 325
      scope = scope.preload(:custom_values)
......
315 342
    if has_column?(:last_notes)
316 343
      Issue.load_visible_last_notes(issues)
317 344
    end
318
    issues
319
  rescue ::ActiveRecord::StatementInvalid => e
320
    raise StatementInvalid.new(e.message)
345
    return issues
321 346
  end
347
  private :preload_issues
322 348

  
323 349
  # Returns the issues ids
324 350
  def issue_ids(options={})
test/unit/query_test.rb
161 161

  
162 162
  def assert_query_result(expected, query)
163 163
    assert_nothing_raised do
164
      assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort
164
      assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort, 'issues wrong'
165
      assert_equal expected.map(&:id).sort, query.lazy_issues.map(&:id).sort, 'lazy issues wrong'
165 166
      assert_equal expected.size, query.issue_count
166 167
    end
167 168
  end
(1-1/2)