summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorHeinrich Lee Yu <hleeyu@gmail.com>2018-11-15 14:56:51 +0800
committerHeinrich Lee Yu <hleeyu@gmail.com>2018-12-31 11:00:29 +0800
commit12e01daec9f24fda742103f9e6c795c358693de5 (patch)
tree74eda7709f2803585f34cd8d969aefe69feaa782 /app/models
parentc07bf1abf2a5c3751e575ee5926e9a052fa0b341 (diff)
downloadgitlab-ce-12e01daec9f24fda742103f9e6c795c358693de5.tar.gz
Add group milestones in upcoming filter
Diffstat (limited to 'app/models')
-rw-r--r--app/models/milestone.rb28
1 files changed, 20 insertions, 8 deletions
diff --git a/app/models/milestone.rb b/app/models/milestone.rb
index 6dc0fca68e6..30b5884d405 100644
--- a/app/models/milestone.rb
+++ b/app/models/milestone.rb
@@ -40,6 +40,7 @@ class Milestone < ActiveRecord::Base
scope :for_projects_and_groups, -> (project_ids, group_ids) do
conditions = []
+
conditions << arel_table[:project_id].in(project_ids) if project_ids&.compact&.any?
conditions << arel_table[:group_id].in(group_ids) if group_ids&.compact&.any?
@@ -129,18 +130,29 @@ class Milestone < ActiveRecord::Base
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
end
- def self.upcoming_ids_by_projects(projects)
- rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now)
+ def self.upcoming_ids(projects, groups)
+ rel = unscoped
+ .for_projects_and_groups(projects&.map(&:id), groups&.map(&:id))
+ .active.where('milestones.due_date > NOW()')
if Gitlab::Database.postgresql?
- rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
+ rel.order(:project_id, :group_id, :due_date).select('DISTINCT ON (project_id, group_id) id')
else
+ # We need to use MySQL's NULL-safe comparison operator `<=>` here
+ # because one of `project_id` or `group_id` is always NULL
+ join_clause = <<~HEREDOC
+ LEFT OUTER JOIN milestones earlier_milestones
+ ON milestones.project_id <=> earlier_milestones.project_id
+ AND milestones.group_id <=> earlier_milestones.group_id
+ AND milestones.due_date > earlier_milestones.due_date
+ AND earlier_milestones.due_date > NOW()
+ AND earlier_milestones.state = 'active'
+ HEREDOC
+
rel
- .group(:project_id, :due_date, :id)
- .having('due_date = MIN(due_date)')
- .pluck(:id, :project_id, :due_date)
- .uniq(&:second)
- .map(&:first)
+ .joins(join_clause)
+ .where('earlier_milestones.id IS NULL')
+ .select(:id)
end
end