Commit e8058bd2 authored by Sean McGivern's avatar Sean McGivern

Return a relation with Postgres

Postgres only needs to select a single column, so that can used as a
sub-query where `Milestone.upcoming_ids_by_projects` is actually used in
`IssuableFinder`.

MySQL needs to select the `due_date` column because it's used in the
`HAVING` clause, so it has to return an array of IDs.
parent 750b2ff0
...@@ -71,7 +71,7 @@ class Milestone < ActiveRecord::Base ...@@ -71,7 +71,7 @@ class Milestone < ActiveRecord::Base
rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now) rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now)
if Gitlab::Database.postgresql? if Gitlab::Database.postgresql?
rel.order(:project_id, :due_date).pluck('DISTINCT ON (project_id) id') rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
else else
rel. rel.
group(:project_id). group(:project_id).
......
...@@ -221,7 +221,9 @@ describe Milestone, models: true do ...@@ -221,7 +221,9 @@ describe Milestone, models: true do
let!(:past_milestone_project_3) { create(:milestone, project: project_3, due_date: Time.now - 1.day) } let!(:past_milestone_project_3) { create(:milestone, project: project_3, due_date: Time.now - 1.day) }
let(:milestone_ids) { Milestone.upcoming_ids_by_projects(projects) } # The call to `#try` is because this returns a relation with a Postgres DB,
# and an array of IDs with a MySQL DB.
let(:milestone_ids) { Milestone.upcoming_ids_by_projects(projects).map { |id| id.try(:id) || id } }
it 'returns the next upcoming open milestone ID for each project' do it 'returns the next upcoming open milestone ID for each project' do
expect(milestone_ids).to contain_exactly(current_milestone_project_1.id, current_milestone_project_2.id) expect(milestone_ids).to contain_exactly(current_milestone_project_1.id, current_milestone_project_2.id)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment