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

add constraint to work package start/due date

ulferts0878a1c7cfd5
diff --git a/db/migrate/20220926124435_constrain_work_package_dates.rb b/db/migrate/20220926124435_constrain_work_package_dates.rb
new file mode 100644
index 00000000000..2c5fac0d831
--- /dev/null
+++ b/db/migrate/20220926124435_constrain_work_package_dates.rb
@@ -0,0 +1,18 @@
+class ConstrainWorkPackageDates < ActiveRecord::Migration[7.0]
+ def change
+ reversible do |dir|
+ dir.up do
+ WorkPackage.where("start_date > due_date").in_batches.each_record do |work_package|
+ User.execute_as(User.system) do
+ work_package.start_date = work_package.due_date
+ work_package.duration = 1
+ work_package.journal_notes = '_Resetting the start date automatically to fix inconsistent dates._'
+ work_package.save!(validate: false)
+ end
+ end
+ end
+ end
+
+ add_check_constraint :work_packages, 'due_date >= start_date', name: 'work_packages_due_larger_start_date'
+ end
+end