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

Store journal entry for the progress values migration

Christophe Bliard7ee2d78a635f
diff --git a/app/workers/work_packages/update_progress_job.rb b/app/workers/work_packages/update_progress_job.rb
index c017b131674..96a7f7fa5d9 100644
--- a/app/workers/work_packages/update_progress_job.rb
+++ b/app/workers/work_packages/update_progress_job.rb
@@ -26,10 +26,12 @@
# See COPYRIGHT and LICENSE files for more details.
#++
+# rubocop:disable Rails/SquishedSQLHeredocs
class WorkPackages::UpdateProgressJob < ApplicationJob
queue_with_priority :default
def perform(previous_mode: nil)
+ create_temporary_progress_table
if previous_mode == "disabled"
unset_all_percent_complete_values
end
@@ -41,22 +43,43 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
derive_unset_work_from_remaining_work_and_p_complete
derive_p_complete_from_work_and_remaining_work
- create_journals_for_updated_work_packages
+ updated_work_package_ids = copy_progress_values_to_work_packages
+ create_journals_for_updated_work_packages(updated_work_package_ids)
+ ensure
+ drop_temporary_progress_table
end
private
+ def create_temporary_progress_table
+ execute(<<~SQL)
+ CREATE UNLOGGED TABLE temp_wp_progress_values
+ AS SELECT
+ id,
+ estimated_hours,
+ remaining_hours,
+ done_ratio
+ FROM work_packages
+ SQL
+ end
+
+ def drop_temporary_progress_table
+ execute(<<~SQL)
+ DROP TABLE temp_wp_progress_values
+ SQL
+ end
+
def unset_all_percent_complete_values
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET done_ratio = NULL
WHERE done_ratio IS NOT NULL
SQL
end
def fix_remaining_work_set_with_100p_complete
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET estimated_hours = remaining_hours,
remaining_hours = 0
WHERE estimated_hours IS NULL
@@ -68,20 +91,20 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
def fix_remaining_work_exceeding_work
# avoid a division by zero when work and remaining work are both zero in
# `#derive_p_complete_from_work_and_remaining_work` method.
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET done_ratio = 0
WHERE remaining_hours = estimated_hours
SQL
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET remaining_hours = estimated_hours,
done_ratio = 0
WHERE remaining_hours > estimated_hours
AND done_ratio IS NULL
SQL
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
WHERE remaining_hours > estimated_hours
AND done_ratio IS NOT NULL
@@ -89,8 +112,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
end
def fix_only_work_being_set
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET remaining_hours = estimated_hours,
done_ratio = 0
WHERE estimated_hours IS NOT NULL
@@ -100,8 +123,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
end
def fix_only_remaining_work_being_set
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET estimated_hours = remaining_hours,
done_ratio = 0
WHERE estimated_hours IS NULL
@@ -111,8 +134,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
end
def derive_unset_remaining_work_from_work_and_p_complete
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET remaining_hours = ROUND((estimated_hours - (estimated_hours * done_ratio / 100.0))::numeric, 2)
WHERE estimated_hours IS NOT NULL
AND remaining_hours IS NULL
@@ -121,8 +144,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
end
def derive_unset_work_from_remaining_work_and_p_complete
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET estimated_hours = ROUND((remaining_hours * 100 / (100 - done_ratio))::numeric, 2)
WHERE estimated_hours IS NULL
AND remaining_hours IS NOT NULL
@@ -131,8 +154,8 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
end
def derive_p_complete_from_work_and_remaining_work
- execute_sql_update(<<~SQL.squish)
- UPDATE work_packages
+ execute(<<~SQL.squish)
+ UPDATE temp_wp_progress_values
SET done_ratio = (estimated_hours - remaining_hours) * 100 / estimated_hours
WHERE estimated_hours IS NOT NULL
AND remaining_hours IS NOT NULL
@@ -142,23 +165,40 @@ class WorkPackages::UpdateProgressJob < ApplicationJob
SQL
end
- def create_journals_for_updated_work_packages
+ def copy_progress_values_to_work_packages
+ results = execute(<<~SQL)
+ UPDATE work_packages
+ SET estimated_hours = temp_wp_progress_values.estimated_hours,
+ remaining_hours = temp_wp_progress_values.remaining_hours,
+ done_ratio = temp_wp_progress_values.done_ratio,
+ lock_version = lock_version + 1,
+ updated_at = NOW()
+ FROM temp_wp_progress_values
+ WHERE work_packages.id = temp_wp_progress_values.id
+ AND (
+ work_packages.estimated_hours IS DISTINCT FROM temp_wp_progress_values.estimated_hours
+ OR work_packages.remaining_hours IS DISTINCT FROM temp_wp_progress_values.remaining_hours
+ OR work_packages.done_ratio IS DISTINCT FROM temp_wp_progress_values.done_ratio
+ )
+ RETURNING work_packages.id
+ SQL
+ results.column_values(0)
+ end
+
+ def create_journals_for_updated_work_packages(updated_work_package_ids)
WorkPackage.where(id: updated_work_package_ids).find_each do |work_package|
Journals::CreateService.new(work_package, system_user)
.call(cause: { type: "system_update", feature: "progress_calculation_changed" })
end
end
- def execute_sql_update(sql)
- result = ActiveRecord::Base.connection.execute("#{sql} RETURNING id")
- updated_work_package_ids.merge(result.field_values("id"))
- end
-
- def updated_work_package_ids
- @updated_work_package_ids ||= Set.new
+ # Executes an sql statement, shorter.
+ def execute(sql)
+ ActiveRecord::Base.connection.execute(sql)
end
def system_user
@system_user ||= User.system
end
end
+# rubocop:enable Rails/SquishedSQLHeredocs
diff --git a/spec/migrations/update_progress_calculation_spec.rb b/spec/migrations/update_progress_calculation_spec.rb
index a6293ddb861..b6d7340a946 100644
--- a/spec/migrations/update_progress_calculation_spec.rb
+++ b/spec/migrations/update_progress_calculation_spec.rb
@@ -73,6 +73,73 @@ RSpec.describe UpdateProgressCalculation, type: :model do
table.work_packages
end
+ describe "journal creation" do
+ before do
+ Setting.work_package_done_ratio = "field"
+ end
+
+ context "when a work package progress values are not changed" do
+ let_work_packages(<<~TABLE)
+ subject | work | remaining work | % complete
+ wp all unset | | |
+ wp only pc set | | | 60%
+ wp all set consistent | 10h | 4h | 60%
+ TABLE
+
+ before do
+ run_migration
+ end
+
+ it "does not create a journal entry" do
+ table_work_packages.each do |wp|
+ expect(wp.journals.count).to eq(1)
+ end
+ end
+ end
+
+ context "when some work package progress values are changed" do
+ let_work_packages(<<~TABLE)
+ hierarchy | work | remaining work | % complete
+ wp only w set | 10h | |
+ wp only rw set | | 4h |
+ wp both w and pc set | 10h | | 60%
+ wp all set inconsistent | 10h | 1h | 10%
+ TABLE
+
+ before do
+ run_migration
+ end
+
+ it "creates one and only one additional journal entry" do
+ table_work_packages.each do |wp|
+ expect(wp.journals.count).to eq(2)
+ end
+ end
+
+ it "the journal author is the system user" do
+ journal = WorkPackage.last.last_journal
+ expect(journal.user).to eq(User.system)
+ end
+
+ it "changes the lock_version of the work package" do
+ previous_lock_version = wp_only_w_set.lock_version
+ wp_only_w_set.reload
+ expect(wp_only_w_set.lock_version).not_to eq(previous_lock_version)
+ end
+
+ it "changes the updated_at of the work package" do
+ wp_only_w_set.reload
+ expect(wp_only_w_set.updated_at).not_to eq(wp_only_w_set.created_at)
+ first_journal = wp_only_w_set.journals.first
+ expect(wp_only_w_set.updated_at).not_to eq(first_journal.updated_at)
+
+ expect(wp_only_w_set.updated_at).to be > wp_only_w_set.created_at
+ last_journal = wp_only_w_set.journals.last
+ expect(wp_only_w_set.updated_at).to eq(last_journal.updated_at)
+ end
+ end
+ end
+
context "when in disabled mode" do
before do
Setting.work_package_done_ratio = "disabled"
diff --git a/spec/support/table_helpers/let_work_packages.rb b/spec/support/table_helpers/let_work_packages.rb
index cf783b9297e..c7982c267a7 100644
--- a/spec/support/table_helpers/let_work_packages.rb
+++ b/spec/support/table_helpers/let_work_packages.rb
@@ -49,6 +49,9 @@ module TableHelpers
# let!(:_table) do
# create_table(table_representation)
# end
+ # let!(:table_work_packages) do
+ # _table.work_packages
+ # end
# let(:parent) do
# _table.work_package(:parent)
# end
@@ -62,6 +65,7 @@ module TableHelpers
let!(:_table) { create_table(table_representation) }
table_data = TableData.for(table_representation)
+ let(:table_work_packages) { _table.work_packages }
table_data.work_package_identifiers.each do |identifier|
let(identifier) { _table.work_package(identifier) }
end