Commit be4b01a8 authored by Mark Chao's avatar Mark Chao

Bulk update epics when milestone date changes

parent ed0c9424
......@@ -170,8 +170,6 @@ module EE
super.merge(label_url_method: :group_epics_url)
end
private
def fetch_milestone_date_data
sql = <<~SQL
SELECT milestones.id, milestones.start_date, milestones.due_date FROM milestones
......
......@@ -9,12 +9,42 @@ module EE
def execute(milestone)
super
::Epic.joins(:issues).where(issues: { milestone_id: milestone.id }).each do |epic|
epic.update_dates
if milestone.previous_changes.key?(:start_date) || milestone.previous_changes.key?(:due_date)
update_epics(milestone)
end
milestone
end
private
def update_epics(milestone)
groups = ::Epic.joins(:issues).includes(:issues).where(issues: { milestone_id: milestone.id }).group_by do |epic|
milestone_ids = epic.issues.map(&:milestone_id)
milestone_ids.compact!
milestone_ids.uniq!
milestone_ids
end
groups.each do |milestone_ids, epics|
data = epics.first.fetch_milestone_date_data
::Epic.where(id: epics.map(&:id)).update_all(
[
%{
start_date = CASE WHEN start_date_is_fixed = true THEN start_date ELSE ? END,
start_date_sourcing_milestone_id = ?,
end_date = CASE WHEN due_date_is_fixed = true THEN end_date ELSE ? END,
due_date_sourcing_milestone_id = ?
},
data[:start_date],
data[:start_date_sourcing_milestone_id],
data[:due_date],
data[:due_date_sourcing_milestone_id]
]
)
end
end
end
end
end
......@@ -17,7 +17,10 @@ describe Milestones::UpdateService do
epic.reload
expect(epic.start_date).to eq(nil)
expect(epic.start_date_sourcing_milestone).to eq(nil)
expect(epic.due_date).to eq(due_date)
expect(epic.due_date_sourcing_milestone).to eq(milestone)
end
end
end
......
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