Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix/child scheduling restrictions (#8584)

ulferts7e7b9132e383
diff --git a/app/models/relation.rb b/app/models/relation.rb
index b24ce482fb1..f8288f2b013 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -31,6 +31,10 @@
class Relation < ApplicationRecord
include VirtualAttribute
+ include ::Scopes::Scoped
+
+ scope_classes Relations::Scopes::FollowsNonManualAncestors
+
scope :of_work_package,
->(work_package) { where('from_id = ? OR to_id = ?', work_package, work_package) }
diff --git a/app/models/relations/scopes/follows_non_manual_ancestors.rb b/app/models/relations/scopes/follows_non_manual_ancestors.rb
new file mode 100644
index 00000000000..aad25c5bc8b
--- /dev/null
+++ b/app/models/relations/scopes/follows_non_manual_ancestors.rb
@@ -0,0 +1,57 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+# Returns all follows relationships of work package ancestors or work package unless
+# the ancestor or a work package between the ancestor and self is manually scheduled.
+module Relations::Scopes
+ class FollowsNonManualAncestors
+ class << self
+ def fetch(work_package)
+ ancestor_relations_non_manual = Relation
+ .hierarchy_or_reflexive
+ .where(to_id: work_package.id)
+ .where.not(from_id: from_manual_ancestors(work_package).select(:from_id))
+ Relation
+ .where(from_id: ancestor_relations_non_manual.select(:from_id))
+ .follows
+ end
+
+ private
+
+ def from_manual_ancestors(work_package)
+ manually_schedule_ancestors = work_package.ancestors.where(schedule_manually: true)
+
+ Relation
+ .hierarchy_or_reflexive
+ .where(to_id: manually_schedule_ancestors.select(:id))
+ end
+ end
+ end
+end
diff --git a/app/models/work_package/scheduling_rules.rb b/app/models/work_package/scheduling_rules.rb
index 1b6dd71a23d..d83a0862e70 100644
--- a/app/models/work_package/scheduling_rules.rb
+++ b/app/models/work_package/scheduling_rules.rb
@@ -55,18 +55,14 @@ module WorkPackage::SchedulingRules
# B is 2017/07/25
# A is 2017/07/25
def soonest_start
- # Using a hand crafted union here instead of the alternative
- # Relation.from_work_package_or_ancestors(self).follows
- # as the performance of the above would be several orders of magnitude worse on MySql
- sql = Relation.connection.unprepared_statement do
- "((#{ancestors_follows_relations.to_sql}) UNION (#{own_follows_relations.to_sql})) AS relations"
- end
-
+ # eager load `to` to avoid n+1 on successor_soonest_start
@soonest_start ||=
- Relation.from(sql)
- .map(&:successor_soonest_start)
- .compact
- .max
+ Relation
+ .follows_non_manual_ancestors(self)
+ .includes(:to)
+ .map(&:successor_soonest_start)
+ .compact
+ .max
end
# Returns the time scheduled for this work package.
@@ -83,14 +79,4 @@ module WorkPackage::SchedulingRules
1
end
end
-
- private
-
- def ancestors_follows_relations
- Relation.where(from_id: self.ancestors_relations.select(:from_id)).follows
- end
-
- def own_follows_relations
- Relation.where(from_id: self.id).follows
- end
end
diff --git a/spec/models/work_package/work_package_relations_spec.rb b/spec/models/work_package/work_package_relations_spec.rb
index 09531f94860..95f2469f041 100644
--- a/spec/models/work_package/work_package_relations_spec.rb
+++ b/spec/models/work_package/work_package_relations_spec.rb
@@ -32,28 +32,28 @@ describe WorkPackage, type: :model do
describe '#relation' do
let(:closed_state) do
FactoryBot.create(:status,
- is_closed: true)
+ is_closed: true)
end
describe '#duplicate' do
let(:original) { FactoryBot.create(:work_package) }
let(:dup_1) do
FactoryBot.create(:work_package,
- project: original.project,
- type: original.type,
- status: original.status)
+ project: original.project,
+ type: original.type,
+ status: original.status)
end
let(:relation_org_dup_1) do
FactoryBot.create(:relation,
- from: dup_1,
- to: original,
- relation_type: Relation::TYPE_DUPLICATES)
+ from: dup_1,
+ to: original,
+ relation_type: Relation::TYPE_DUPLICATES)
end
let(:workflow) do
FactoryBot.create(:workflow,
- old_status: original.status,
- new_status: closed_state,
- type_id: original.type_id)
+ old_status: original.status,
+ new_status: closed_state,
+ type_id: original.type_id)
end
let(:user) { FactoryBot.create(:user) }
@@ -66,22 +66,22 @@ describe WorkPackage, type: :model do
context 'closes duplicates' do
let(:dup_2) do
FactoryBot.create(:work_package,
- project: original.project,
- type: original.type,
- status: original.status)
+ project: original.project,
+ type: original.type,
+ status: original.status)
end
let(:relation_dup_1_dup_2) do
FactoryBot.create(:relation,
- from: dup_2,
- to: dup_1,
- relation_type: Relation::TYPE_DUPLICATES)
+ from: dup_2,
+ to: dup_1,
+ relation_type: Relation::TYPE_DUPLICATES)
end
# circular dependency
let(:relation_dup_2_org) do
FactoryBot.create(:relation,
- from: dup_2,
- to: original,
- relation_type: Relation::TYPE_DUPLICATES)
+ from: dup_2,
+ to: original,
+ relation_type: Relation::TYPE_DUPLICATES)
end
before do
@@ -119,34 +119,108 @@ describe WorkPackage, type: :model do
end
describe '#soonest_start' do
- let(:work_package_1) { FactoryBot.create(:work_package) }
- let(:work_package_2) do
+ let(:predecessor) do
FactoryBot.create(:work_package,
- project: work_package_1.project)
+ due_date: predecessor_due_date)
end
- let!(:work_package_2_1) do
+ let(:predecessor_due_date) { nil }
+ let(:successor) do
FactoryBot.create(:work_package,
- parent: work_package_2,
- project: work_package_1.project)
+ schedule_manually: successor_schedule_manually,
+ project: predecessor.project)
end
- let!(:relation_1) do
+ let(:successor_schedule_manually) { false }
+ let(:successor_child) do
+ FactoryBot.create(:work_package,
+ schedule_manually: successor_child_schedule_manually,
+ parent: successor,
+ project: predecessor.project)
+ end
+ let(:successor_child_schedule_manually) { false }
+ let(:successor_grandchild) do
+ FactoryBot.create(:work_package,
+ parent: successor_child,
+ project: predecessor.project)
+ end
+ let(:relation_successor) do
FactoryBot.create(:relation,
- from: work_package_1,
- to: work_package_2,
- relation_type: Relation::TYPE_PRECEDES)
+ from: predecessor,
+ to: successor,
+ relation_type: Relation::TYPE_PRECEDES)
end
+ let(:work_packages) { [predecessor, successor, successor_child] }
+ let(:relations) { [relation_successor] }
- context 'start date exists in related work packages' do
- before do
- work_package_1.due_date = Date.today
- work_package_1.save!
+ before do
+ work_packages
+ relations
+ end
+
+ context 'without a predecessor' do
+ let(:work_packages) { [successor] }
+ let(:relations) { [] }
+
+ it { expect(successor.soonest_start).to be_nil }
+ end
+
+ context 'with a predecessor' do
+ let(:work_packages) { [predecessor, successor] }
+
+ context 'start date exists in predecessor' do
+ let(:predecessor_due_date) { Date.today }
+
+ it { expect(successor_child.soonest_start).to eq(predecessor.due_date + 1) }
+ end
+
+ context 'no date in predecessor' do
+ it { expect(successor_child.soonest_start).to be_nil }
+ end
+ end
+
+ context 'with the parent having a predecessor' do
+ let(:work_packages) { [predecessor, successor, successor_child] }
+
+ context 'start date exists in predecessor' do
+ let(:predecessor_due_date) { Date.today }
+
+ it { expect(successor_child.soonest_start).to eq(predecessor.due_date + 1) }
+
+ context 'with the parent manually scheduled' do
+ let(:successor_schedule_manually) { true }
+
+ it { expect(successor_child.soonest_start).to be_nil }
+ end
end
- it { expect(work_package_2_1.soonest_start).to eq(work_package_1.due_date + 1) }
+ context 'no start date exists in related work packages' do
+ it { expect(successor_child.soonest_start).to be_nil }
+ end
end
- context 'no start date exists in related work packages' do
- it { expect(work_package_2_1.soonest_start).to be_nil }
+ context 'with the grandparent having a predecessor' do
+ let(:work_packages) { [predecessor, successor, successor_child, successor_grandchild] }
+
+ context 'start date exists in predecessor' do
+ let(:predecessor_due_date) { Date.today }
+
+ it { expect(successor_grandchild.soonest_start).to eq(predecessor.due_date + 1) }
+
+ context 'with the grandparent manually scheduled' do
+ let(:successor_schedule_manually) { true }
+
+ it { expect(successor_grandchild.soonest_start).to be_nil }
+ end
+
+ context 'with the parent manually scheduled' do
+ let(:successor_child_schedule_manually) { true }
+
+ it { expect(successor_grandchild.soonest_start).to be_nil }
+ end
+ end
+
+ context 'no start date exists in related work packages' do
+ it { expect(successor_grandchild.soonest_start).to be_nil }
+ end
end
end
end