From 841ec8849010f7652dbeddea47d32c037c597c89 Mon Sep 17 00:00:00 2001 From: ulferts Date: Wed, 25 May 2022 15:10:23 +0200 Subject: [PATCH] calculate duration for existing work packages --- ...525154549_add_duration_to_work_packages.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/db/migrate/20220525154549_add_duration_to_work_packages.rb b/db/migrate/20220525154549_add_duration_to_work_packages.rb index 2fc2660686..abfc20d6ee 100644 --- a/db/migrate/20220525154549_add_duration_to_work_packages.rb +++ b/db/migrate/20220525154549_add_duration_to_work_packages.rb @@ -3,5 +3,28 @@ class AddDurationToWorkPackages < ActiveRecord::Migration[6.1] add_column :work_packages, :duration, :integer add_column :work_package_journals, :duration, :integer + + reversible do |dir| + dir.up do + set_duration(:work_packages) + set_duration(:work_package_journals) + end + end + end + + private + + def set_duration(table) + execute <<~SQL.squish + UPDATE + #{table} + SET + duration = CASE + WHEN start_date IS NULL OR due_date IS NULL + THEN 1 + ELSE + due_date - start_date + 1 + END + SQL end end