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

[59539] Migration: set lag for follows relations

Christophe Bliard35f463f4bdf0
diff --git a/app/workers/work_packages/automatic_mode/migrate_values_job.rb b/app/workers/work_packages/automatic_mode/migrate_values_job.rb
index 80e732487af..3b6b03c32ad 100644
--- a/app/workers/work_packages/automatic_mode/migrate_values_job.rb
+++ b/app/workers/work_packages/automatic_mode/migrate_values_job.rb
@@ -32,6 +32,7 @@ class WorkPackages::AutomaticMode::MigrateValuesJob < ApplicationJob
change_scheduling_mode_to_manual
change_scheduling_mode_to_automatic_for_followers
change_scheduling_mode_to_automatic_for_parents
+ set_lags_for_follows_relations
copy_values_to_work_packages_and_update_journals
end
end
@@ -97,6 +98,19 @@ class WorkPackages::AutomaticMode::MigrateValuesJob < ApplicationJob
SQL
end
+ def set_lags_for_follows_relations
+ execute(<<~SQL.squish)
+ UPDATE relations
+ SET lag = COALESCE(wp_succ.start_date, wp_succ.due_date) - COALESCE(wp_pred.due_date, wp_pred.start_date) - 1
+ FROM work_packages wp_pred, work_packages wp_succ
+ WHERE relations.relation_type = 'follows'
+ AND relations.to_id = wp_pred.id
+ AND relations.from_id = wp_succ.id
+ AND COALESCE(wp_succ.start_date, wp_succ.due_date) IS NOT NULL
+ AND COALESCE(wp_pred.due_date, wp_pred.start_date) IS NOT NULL
+ SQL
+ end
+
def copy_values_to_work_packages_and_update_journals
updated_work_package_ids = copy_values_to_work_packages
create_journals_for_updated_work_packages(updated_work_package_ids)
diff --git a/spec/migrations/update_scheduling_mode_and_lags_spec.rb b/spec/migrations/update_scheduling_mode_and_lags_spec.rb
index 0f58a4e0a16..a66904e2d06 100644
--- a/spec/migrations/update_scheduling_mode_and_lags_spec.rb
+++ b/spec/migrations/update_scheduling_mode_and_lags_spec.rb
@@ -154,4 +154,53 @@ RSpec.describe UpdateSchedulingModeAndLags, type: :model do
expect(child).to be_schedule_manually
end
end
+
+ context "for 2 work packages following each other with distant dates" do
+ let_work_packages(<<~TABLE)
+ subject | MTWTFSS | properties
+ predecessor 1 | XX |
+ follower 1 | XX | follows predecessor 1
+
+ # only start dates
+ predecessor 2 | [ |
+ follower 2 | [ | follows predecessor 2
+
+ # only due dates
+ predecessor 3 | ] |
+ follower 3 | ] | follows predecessor 3 with lag 1
+ TABLE
+
+ it "sets a lag to the relation to ensure the distance is kept" do
+ run_migration
+
+ expect(follower1).to be_schedule_automatically
+ relations = _table.relations.map(&:reload)
+ expect(relations.map(&:lag)).to all(eq(2))
+ end
+ end
+
+ context "for 2 work packages following each other with missing dates" do
+ let_work_packages(<<~TABLE)
+ subject | MTWTFSS | properties
+ # only predecessor has dates
+ predecessor 1 | XX |
+ follower 1 | | follows predecessor 1
+
+ # only successor has dates
+ predecessor 2 | |
+ follower 2 | XX | follows predecessor 2
+
+ # none have dates
+ predecessor 3 | |
+ follower 3 | | follows predecessor 3 with lag 1
+ TABLE
+
+ it "does not change the existing lag" do
+ run_migration
+
+ expect(follower1).to be_schedule_automatically
+ relations = _table.relations.map(&:reload)
+ expect(relations.map(&:lag)).to eq([0, 0, 1])
+ end
+ end
end
diff --git a/spec/support/table_helpers/column_type/properties.rb b/spec/support/table_helpers/column_type/properties.rb
index 2a550c8f1b0..dbe0e919c97 100644
--- a/spec/support/table_helpers/column_type/properties.rb
+++ b/spec/support/table_helpers/column_type/properties.rb
@@ -73,7 +73,7 @@ module TableHelpers
def parse_property(property)
case property
- when /^follows (\w+)(?: with lag (\d+))?/
+ when /^follows (.+?)(?: with lag (\d+))?\s*$/
{
relations: {
raw: property,
diff --git a/spec/support/table_helpers/table.rb b/spec/support/table_helpers/table.rb
index 1b5e241fd8f..7ad26d05076 100644
--- a/spec/support/table_helpers/table.rb
+++ b/spec/support/table_helpers/table.rb
@@ -28,8 +28,9 @@
module TableHelpers
class Table
- def initialize(work_packages_by_identifier)
+ def initialize(work_packages_by_identifier, relations)
@work_packages_by_identifier = work_packages_by_identifier
+ @relations = relations
end
def work_package(name)
@@ -41,6 +42,10 @@ module TableHelpers
@work_packages_by_identifier.values
end
+ def relations
+ @relations
+ end
+
private
def normalize_name(name)
diff --git a/spec/support/table_helpers/table_data.rb b/spec/support/table_helpers/table_data.rb
index 136ba7752fb..2885dba4e48 100644
--- a/spec/support/table_helpers/table_data.rb
+++ b/spec/support/table_helpers/table_data.rb
@@ -79,8 +79,8 @@ module TableHelpers
end
def create_work_packages
- work_packages_by_identifier = Factory.new(self).create
- Table.new(work_packages_by_identifier)
+ work_packages_by_identifier, relations = Factory.new(self).create
+ Table.new(work_packages_by_identifier, relations)
end
def order_like!(other_table)
@@ -93,11 +93,14 @@ module TableHelpers
end
class Factory
- attr_reader :table_data, :work_packages_by_identifier
+ include Identifier
+
+ attr_reader :table_data, :work_packages_by_identifier, :relations
def initialize(table_data)
@table_data = table_data
@work_packages_by_identifier = {}
+ @relations = []
end
def create
@@ -108,7 +111,7 @@ module TableHelpers
table_data.work_package_identifiers.each do |identifier| # rubocop:disable Style/CombinableLoops
create_follows_relations(identifier)
end
- work_packages_by_identifier
+ [work_packages_by_identifier, relations]
end
def create_work_package(identifier)
@@ -123,11 +126,10 @@ module TableHelpers
end
def create_follows_relations(identifier)
- relations = work_package_relations(identifier)
- relations.each do |relation|
- predecessor = work_packages_by_identifier[relation[:predecessor].to_sym]
+ work_package_relations(identifier).each do |relation|
+ predecessor = find_work_package_by_name(relation[:predecessor])
follower = work_packages_by_identifier[identifier]
- FactoryBot.create(
+ relations << FactoryBot.create(
:follows_relation,
from: follower,
to: predecessor,
@@ -136,6 +138,16 @@ module TableHelpers
end
end
+ def find_work_package_by_name(name)
+ identifier = to_identifier(name)
+ work_package = work_packages_by_identifier[identifier]
+ if work_package.nil?
+ raise "Work package with name #{name.inspect} (identifier: #{identifier.inspect}) not found. " \
+ "Available work package identifiers: #{work_packages_by_identifier.keys}."
+ end
+ work_package
+ end
+
def lookup_parent(identifier)
if identifier
@work_packages_by_identifier[identifier] || create_work_package(identifier)