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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix wrong join with historic relation
Klaus Zanders4056a6c6bcba
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rbindex 970fdd28976..55769fb827a 100644--- a/app/models/journable/historic_active_record_relation.rb+++ b/app/models/journable/historic_active_record_relation.rb@@ -352,12 +352,7 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relationgsub_table_names_in_sql_string!(node.left)elsif node.kind_of?(Arel::Nodes::Join) and node.right.kind_of?(Arel::Nodes::On)[node.right.expr.left, node.right.expr.right].each do |attribute|- if attribute.respond_to? :relation and- (attribute.relation == journal_class.arel_table) and- (attribute.name == "id")- attribute.relation = Journal.arel_table- attribute.name = "journable_id"- end+ modify_conditions(attribute)endendend@@ -439,6 +434,13 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation# expr, which is a NodeExpression itself[node.expr.left, node.expr.right].each { |child| modify_conditions(child) }elsif node.kind_of?(Arel::Attributes::Attribute)+ # With the replacement of the table ivar we might already have switched the base relation ... so if the ID is used+ # in a join, we should also replace it with the journable+ if node.relation == model.journal_class.arel_table && node.name == "id"+ node.relation = Journal.arel_table+ node.name = "journable_id"+ end+# We find an attribute, figure out if it is the model's tableif node.relation == model.arel_tableif node.name == "id"diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex 57dd294c6d4..a1ff7016931 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -59,7 +59,7 @@ class WorkPackage < ApplicationRecordbelongs_to :priority, class_name: "IssuePriority"belongs_to :category, class_name: "Category", optional: true- has_many :time_entries, dependent: :delete_all, inverse_of: :entity+ has_many :time_entries, dependent: :delete_all, as: :entityhas_many :file_links, dependent: :delete_all, class_name: "Storages::FileLink", as: :containerdiff --git a/app/models/work_package/time_entries_cleaner.rb b/app/models/work_package/time_entries_cleaner.rbindex 654818a3ea8..59c8a044d0d 100644--- a/app/models/work_package/time_entries_cleaner.rb+++ b/app/models/work_package/time_entries_cleaner.rb@@ -56,7 +56,7 @@ module WorkPackage::TimeEntriesCleanerenddef update_time_entries(work_packages, action)- TimeEntry.where(["work_package_id IN (?)", work_packages.map(&:id)]).update_all(action)+ TimeEntry.where(["entity_type = ? AND entity_id IN (?)", "WorkPackage", work_packages.map(&:id)]).update_all(action)enddef reassign_time_entries_before_destruction_of(work_packages, user, ids)@@ -73,7 +73,7 @@ module WorkPackage::TimeEntriesCleanerfalseelse- condition = "work_package_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"+ condition = "entity_type = 'WorkPackage', entity_id = #{reassign_to.id}, project_id = #{reassign_to.project_id}"WorkPackage.update_time_entries(work_packages, condition)endenddiff --git a/modules/meeting/app/models/meeting.rb b/modules/meeting/app/models/meeting.rbindex 109e4b9255e..8afabfef1c6 100644--- a/modules/meeting/app/models/meeting.rb+++ b/modules/meeting/app/models/meeting.rb@@ -42,6 +42,8 @@ class Meeting < ApplicationRecordbelongs_to :recurring_meeting, optional: truehas_one :scheduled_meeting, inverse_of: :meeting+ has_many :time_entries, dependent: :delete_all, inverse_of: :entity, as: :entity+# Legacy association to minutes, agendas, contents# to be removed in 17.0has_one :agenda, dependent: :destroy, class_name: "MeetingAgenda"diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rbindex 502628326a6..ed42f57311a 100644--- a/spec/models/journable/historic_active_record_relation_spec.rb+++ b/spec/models/journable/historic_active_record_relation_spec.rb@@ -560,8 +560,11 @@ RSpec.describe Journable::HistoricActiveRecordRelation dobefore { work_package.time_entries << create(:time_entry) }it "transforms the table name" do- expect(subject.to_sql).to include \- "JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""+ expect(subject.to_sql).to include <<~SQL.squish+ INNER JOIN "time_entries"+ ON "time_entries"."entity_type" = 'WorkPackage'+ AND "time_entries"."entity_id" = "journals"."journable_id"+ SQLendit "returns the requested work package" do@@ -572,7 +575,7 @@ RSpec.describe Journable::HistoricActiveRecordRelation dodescribe "using a manual sql expression" do# This is used in the manual-sorting feature.let(:relation) do- WorkPackage \+ WorkPackage.joins("LEFT OUTER JOIN ordered_work_packages ON ordered_work_packages.work_package_id = work_packages.id")enddiff --git a/spec/models/journable/timestamps_spec.rb b/spec/models/journable/timestamps_spec.rbindex 10370ba2d15..c10768bf433 100644--- a/spec/models/journable/timestamps_spec.rb+++ b/spec/models/journable/timestamps_spec.rb@@ -274,10 +274,16 @@ RSpec.describe Journable::Timestamps dosubject { relation.at_timestamp(friday) }it "joins the journable table rather than the journal-data table" do- expect(subject.to_sql).not_to include \- "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"work_package_journals\".\"id\""- expect(subject.to_sql).to include \- "INNER JOIN \"time_entries\" ON \"time_entries\".\"work_package_id\" = \"journals\".\"journable_id\""+ expect(subject.to_sql).not_to include <<~SQL.squish+ INNER JOIN "time_entries"+ ON "time_entries"."entity_type" = 'WorkPackage'+ AND "time_entries"."entity_id" = "work_package_journals"."id"+ SQL+ expect(subject.to_sql).to include <<~SQL.squish+ INNER JOIN "time_entries"+ ON "time_entries"."entity_type" = 'WorkPackage'+ AND "time_entries"."entity_id" = "journals"."journable_id"+ SQLendit "returns the matching records in their historic states" do@@ -502,8 +508,8 @@ RSpec.describe Journable::Timestamps dodescribe "when plucking the id as sub query (workaround)" dosubject do- sub_query_ids = WorkPackage.at_timestamp(monday) \- .where(description: "The work package as it has been on Monday") \+ sub_query_ids = WorkPackage.at_timestamp(monday)+ .where(description: "The work package as it has been on Monday").pluck(:id)WorkPackage.where(id: sub_query_ids)end