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

increase robustness of journable_type migration

ulfertsef6f7243b118
diff --git a/modules/costs/db/migrate/20210726065912_rename_cost_object_type.rb b/modules/costs/db/migrate/20210726065912_rename_cost_object_type.rb
index 917570765d3..011a5e11923 100644
--- a/modules/costs/db/migrate/20210726065912_rename_cost_object_type.rb
+++ b/modules/costs/db/migrate/20210726065912_rename_cost_object_type.rb
@@ -1,5 +1,24 @@
class RenameCostObjectType < ActiveRecord::Migration[6.1]
def up
+ execute <<~SQL.squish
+ UPDATE
+ journals
+ SET
+ version = version + max_journal.max_version
+ FROM
+ (SELECT
+ journable_id, MAX(version) max_version
+ FROM
+ journals
+ WHERE
+ journable_type = 'CostObject'
+ GROUP BY journable_id) max_journal
+ WHERE
+ max_journal.journable_id = journals.journable_id
+ AND
+ journals.journable_type = 'Budget'
+ SQL
+
Journal
.where(journable_type: 'CostObject')
.update_all(journable_type: 'Budget')